Added support for job execution from module path

no issue

- Jobs should not always be functions, one of the standard practices is having a job defined in a module as a self contained executable function
- First parameter of `addJob` function now also handles path to modules which it imports and executes
This commit is contained in:
Naz 2020-11-10 16:32:47 +13:00
parent 70b42e3a75
commit c85ec6aaa5

View File

@ -30,14 +30,18 @@ class JobManager {
/** /**
* Adds job to queue * Adds job to queue
* *
* @param {Function} job - function to be executed in the queue * @param {Function|String} job - function or path to a module defining a job
* @param {Object} [data] - data to be passed into the job * @param {Object} [data] - data to be passed into the job
*/ */
addJob(job, data) { addJob(job, data) {
this.logging.info('Adding one off job to the queue'); this.logging.info('Adding one off job to the queue');
this.queue.push(async () => { this.queue.push(async () => {
await job(data); if (typeof job === 'function') {
await job(data);
} else {
await require(job)(data);
}
}, handler); }, handler);
} }
@ -45,8 +49,8 @@ class JobManager {
* Schedules recuring job * Schedules recuring job
* *
* @param {String} when - cron or human readable schedule format * @param {String} when - cron or human readable schedule format
* @param {Function|String} job - function or path to a file defining a job * @param {Function|String} job - function or path to a module defining a job
* @param {Object} data - data to be passed into the job * @param {Object} [data] - data to be passed into the job
*/ */
scheduleJob(when, job, data) { scheduleJob(when, job, data) {
let schedule; let schedule;