bigbluebutton-Github/bbb-export-annotations/lib/utils/worker-starter.js
Daniel Petri Rocha c786164d05 Refactor worker kickoff
Removes duplicated code across workers.
2022-11-11 17:51:30 +01:00

33 lines
988 B
JavaScript

const {Worker} = require('worker_threads');
const path = require('path');
const WorkerTypes = Object.freeze({
Collector: 'collector',
Process: 'process',
Notifier: 'notifier',
});
const kickOffWorker = (workerType, workerData) => {
return new Promise((resolve, reject) => {
const workerPath = path.join(__dirname, '..', '..', 'workers', `${workerType}.js`);
const worker = new Worker(workerPath, {workerData});
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0) {
reject(new Error(`Worker '${workerType}' stopped with exit code ${code}`));
}
});
});
};
module.exports = class WorkerStarter {
constructor(workerData) {
this.workerData = workerData;
}
collect = () => kickOffWorker(WorkerTypes.Collector, this.workerData);
process = () => kickOffWorker(WorkerTypes.Process, this.workerData);
notify = () => kickOffWorker(WorkerTypes.Notifier, this.workerData);
};