4b18cbcbdb
no issue - When jobs are performing CPU intensive tasks they block main process' event loop. They also can cause memory leaks or unexpected crashes effectively crashing the parent proccess. To address these issues jobs need to be performed off of main process. Worker Threads (https://nodejs.org/dist/latest-v12.x/docs/api/worker_threads.html) are the best candidate for such work. - These changes introduce an integration on top of bree (https://github.com/breejs/bree/) which allows to run recurring jobs in worker thereads. It falls back to child process execution for Node v10 running without `--experimental-worker` flag. - bree was chosen not only because it gives a polyfill for older Node versions. It has support for some of the future use-cases Ghost is looking to implement, like scheduled jobs. - This changeset also includes a complete example of job running on an interval with a possibility for graceful shutdown
33 lines
667 B
JavaScript
33 lines
667 B
JavaScript
const isCronExpression = require('./is-cron-expression');
|
|
|
|
const assemble = (when, job, data, name) => {
|
|
const breeJob = {
|
|
name: name,
|
|
// NOTE: both function and path syntaxes work with 'path' parameter
|
|
path: job,
|
|
outputWorkerMetadata: true
|
|
};
|
|
|
|
if (data) {
|
|
Object.assign(breeJob, {
|
|
worker: {
|
|
workerData: data
|
|
}
|
|
});
|
|
}
|
|
|
|
if (isCronExpression(when)) {
|
|
Object.assign(breeJob, {
|
|
cron: when
|
|
});
|
|
} else {
|
|
Object.assign(breeJob, {
|
|
interval: when
|
|
});
|
|
}
|
|
|
|
return breeJob;
|
|
};
|
|
|
|
module.exports = assemble;
|