forked from mtusi2026/mtusi2026lab5
49 lines
1.3 KiB
JavaScript
49 lines
1.3 KiB
JavaScript
const { writeSync } = require('node:fs');
|
|
const { Kafka } = require('kafkajs');
|
|
const { AttachmentWorker } = require('./dist/Attachment/attachment-worker.js');
|
|
|
|
function marker(event) {
|
|
writeSync(2, JSON.stringify(event) + '\n');
|
|
}
|
|
|
|
const consumer = Kafka.prototype.consumer;
|
|
Kafka.prototype.consumer = function (options) {
|
|
const instance = consumer.call(this, options);
|
|
const { run } = instance;
|
|
instance.run = (settings) => {
|
|
marker({
|
|
event: 'track1_worker_settings',
|
|
concurrency: settings.partitionsConsumedConcurrently ?? 1,
|
|
prefetch: options.maxBytesPerPartition ?? 1048576,
|
|
});
|
|
return run.call(instance, settings);
|
|
};
|
|
return instance;
|
|
};
|
|
|
|
const processJob = AttachmentWorker.prototype.process;
|
|
let active = 0;
|
|
let peakActive = 0;
|
|
AttachmentWorker.prototype.process = async function (job, ...args) {
|
|
const started = performance.now();
|
|
active++;
|
|
peakActive = Math.max(active, peakActive);
|
|
try {
|
|
const completed = await processJob.call(this, job, ...args);
|
|
if (completed) marker({
|
|
event: 'track1_job_finished', id: job.id, started, finished: performance.now(),
|
|
});
|
|
return completed;
|
|
} finally {
|
|
active--;
|
|
}
|
|
};
|
|
|
|
process.on('exit', () => {
|
|
marker({
|
|
event: 'track1_worker_rss',
|
|
peakBytes: process.resourceUsage().maxRSS * 1024,
|
|
peakActive,
|
|
});
|
|
});
|