2020-11-19 07:58:41 +03:00
|
|
|
const path = require('path');
|
2020-08-11 23:13:31 +03:00
|
|
|
const fastq = require('fastq');
|
2020-11-05 07:36:29 +03:00
|
|
|
const later = require('@breejs/later');
|
2020-11-19 07:58:41 +03:00
|
|
|
const Bree = require('bree');
|
2020-08-11 23:13:31 +03:00
|
|
|
const pWaitFor = require('p-wait-for');
|
2020-11-10 07:11:24 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-11-05 07:36:29 +03:00
|
|
|
const isCronExpression = require('./is-cron-expression');
|
2020-11-19 07:58:41 +03:00
|
|
|
const assembleBreeJob = require('./assemble-bree-job');
|
2020-08-11 23:13:31 +03:00
|
|
|
|
|
|
|
const worker = async (task, callback) => {
|
|
|
|
try {
|
|
|
|
let result = await task();
|
|
|
|
callback(null, result);
|
|
|
|
} catch (error) {
|
|
|
|
callback(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handler = (error, result) => {
|
|
|
|
if (error) {
|
2020-11-10 07:11:24 +03:00
|
|
|
// TODO: this handler should not be throwing as this blocks the queue
|
|
|
|
// throw error;
|
2020-08-11 23:13:31 +03:00
|
|
|
}
|
|
|
|
// Can potentially standardise the result here
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
class JobManager {
|
2020-12-07 06:50:41 +03:00
|
|
|
/**
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {Object} [options.logging] - custom logging handler, defaults to console
|
|
|
|
* @param {Function} [options.errorHandler] - custom job error handler
|
|
|
|
*/
|
|
|
|
constructor({logging, errorHandler}) {
|
2020-08-11 23:13:31 +03:00
|
|
|
this.queue = fastq(this, worker, 1);
|
2020-11-19 07:58:41 +03:00
|
|
|
|
|
|
|
this.bree = new Bree({
|
|
|
|
root: false, // set this to `false` to prevent requiring a root directory of jobs
|
2021-01-06 03:45:23 +03:00
|
|
|
hasSeconds: true, // precission is needed to avoid task ovelaps after immediate execution
|
2020-11-19 08:15:52 +03:00
|
|
|
outputWorkerMetadata: true,
|
2020-12-07 06:50:41 +03:00
|
|
|
logger: logging,
|
|
|
|
errorHandler: errorHandler
|
2020-11-19 07:58:41 +03:00
|
|
|
});
|
|
|
|
|
2020-08-11 23:13:31 +03:00
|
|
|
this.logging = logging;
|
|
|
|
}
|
|
|
|
|
2020-11-05 06:41:16 +03:00
|
|
|
/**
|
2021-01-06 03:45:23 +03:00
|
|
|
* By default schedules an "offloaded" job. If `offloaded: true` parameter is set,
|
|
|
|
* puts an "inline" immediate job into the queue.
|
2020-11-05 06:41:16 +03:00
|
|
|
*
|
2021-01-06 03:45:23 +03:00
|
|
|
* @param {Object} GhostJob - job options
|
|
|
|
* @prop {Function | String} GhostJob.job - function or path to a module defining a job
|
|
|
|
* @prop {String} [GhostJob.name] - unique job name, if not provided takes function name or job script filename
|
|
|
|
* @prop {String | Date} [GhostJob.at] - Date, cron or human readable schedule format. Manage will do immediate execution if not specified. Not supported for "inline" jobs
|
|
|
|
* @prop {Object} [GhostJob.data] - data to be passed into the job
|
|
|
|
* @prop {Boolean} [GhostJob.offloaded] - creates an "offloaded" job running in a worker thread by default. If set to "false" runs an "inline" job on the same event loop
|
2020-11-05 06:41:16 +03:00
|
|
|
*/
|
2021-01-06 03:45:23 +03:00
|
|
|
addJob({name, at, job, data, offloaded = true}) {
|
|
|
|
if (offloaded) {
|
|
|
|
this.logging.info('Adding offloaded job to the queue');
|
|
|
|
let schedule;
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
if (typeof job === 'string') {
|
|
|
|
name = path.parse(job).name;
|
2020-11-10 07:11:24 +03:00
|
|
|
} else {
|
2021-01-06 03:45:23 +03:00
|
|
|
throw new Error('Name parameter should be present if job is a function');
|
2020-11-10 07:11:24 +03:00
|
|
|
}
|
2020-11-10 06:32:47 +03:00
|
|
|
}
|
2020-08-11 23:13:31 +03:00
|
|
|
|
2021-01-06 03:45:23 +03:00
|
|
|
if (at && !(at instanceof Date)) {
|
|
|
|
if (isCronExpression(at)) {
|
|
|
|
schedule = later.parse.cron(at, true);
|
|
|
|
} else {
|
|
|
|
schedule = later.parse.text(at);
|
|
|
|
}
|
2020-11-05 07:36:29 +03:00
|
|
|
|
2021-01-06 03:45:23 +03:00
|
|
|
if ((schedule.error && schedule.error !== -1) || schedule.schedules.length === 0) {
|
|
|
|
throw new Error('Invalid schedule format');
|
|
|
|
}
|
2020-11-19 07:58:41 +03:00
|
|
|
|
2021-01-06 03:45:23 +03:00
|
|
|
this.logging.info(`Scheduling job ${name} at ${at}. Next run on: ${later.schedule(schedule).next()}`);
|
|
|
|
} else if (at !== undefined) {
|
|
|
|
this.logging.info(`Scheduling job ${name} at ${at}`);
|
2020-12-02 11:26:15 +03:00
|
|
|
} else {
|
2021-01-06 03:45:23 +03:00
|
|
|
this.logging.info(`Scheduling job ${name} to run immediately`);
|
2020-12-02 11:26:15 +03:00
|
|
|
}
|
2020-11-05 07:36:29 +03:00
|
|
|
|
2021-01-06 03:45:23 +03:00
|
|
|
const breeJob = assembleBreeJob(at, job, data, name);
|
|
|
|
this.bree.add(breeJob);
|
|
|
|
return this.bree.start(name);
|
2020-12-09 09:11:11 +03:00
|
|
|
} else {
|
2021-01-06 03:45:23 +03:00
|
|
|
this.logging.info('Adding one off inline job to the queue');
|
|
|
|
|
|
|
|
this.queue.push(async () => {
|
|
|
|
try {
|
|
|
|
if (typeof job === 'function') {
|
|
|
|
await job(data);
|
|
|
|
} else {
|
|
|
|
await require(job)(data);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// NOTE: each job should be written in a safe way and handle all errors internally
|
|
|
|
// if the error is caught here jobs implementaton should be changed
|
|
|
|
this.logging.error(new errors.IgnitionError({
|
|
|
|
level: 'critical',
|
|
|
|
errorType: 'UnhandledJobError',
|
|
|
|
message: 'Processed job threw an unhandled error',
|
|
|
|
context: (typeof job === 'function') ? 'function' : job,
|
|
|
|
err
|
|
|
|
}));
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}, handler);
|
2020-11-05 07:36:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 06:43:18 +03:00
|
|
|
/**
|
2021-01-06 03:45:23 +03:00
|
|
|
* Removes an "offloaded" job from scheduled jobs queue.
|
|
|
|
* It's NOT yet possible to remove "inline" jobs (will be possible when scheduling is added https://github.com/breejs/bree/issues/68).
|
2020-12-08 06:43:18 +03:00
|
|
|
* The method will throw an Error if job with provided name does not exist.
|
|
|
|
*
|
|
|
|
* NOTE: current implementation does not guarante running job termination
|
|
|
|
* for details see https://github.com/breejs/bree/pull/64
|
|
|
|
*
|
|
|
|
* @param {String} name - job name
|
|
|
|
*/
|
|
|
|
async removeJob(name) {
|
|
|
|
await this.bree.remove(name);
|
|
|
|
}
|
|
|
|
|
2020-11-05 07:36:29 +03:00
|
|
|
/**
|
|
|
|
* @param {import('p-wait-for').Options} [options]
|
|
|
|
*/
|
2020-08-11 23:13:31 +03:00
|
|
|
async shutdown(options) {
|
2020-11-19 07:58:41 +03:00
|
|
|
await this.bree.stop();
|
2020-11-05 07:36:29 +03:00
|
|
|
|
2020-08-11 23:13:31 +03:00
|
|
|
if (this.queue.idle()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.logging.warn('Waiting for busy job queue');
|
|
|
|
|
|
|
|
await pWaitFor(() => this.queue.idle() === true, options);
|
|
|
|
|
|
|
|
this.logging.warn('Job queue finished');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = JobManager;
|