From c85ec6aaa581f6948755d24a5a5a2515748be9f2 Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 10 Nov 2020 16:32:47 +1300 Subject: [PATCH] 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 --- ghost/job-manager/lib/job-manager.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ghost/job-manager/lib/job-manager.js b/ghost/job-manager/lib/job-manager.js index 7ea4547e56..7fa05260b7 100644 --- a/ghost/job-manager/lib/job-manager.js +++ b/ghost/job-manager/lib/job-manager.js @@ -30,14 +30,18 @@ class JobManager { /** * 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 */ addJob(job, data) { this.logging.info('Adding one off job to the queue'); this.queue.push(async () => { - await job(data); + if (typeof job === 'function') { + await job(data); + } else { + await require(job)(data); + } }, handler); } @@ -45,8 +49,8 @@ class JobManager { * Schedules recuring job * * @param {String} when - cron or human readable schedule format - * @param {Function|String} job - function or path to a file defining a job - * @param {Object} data - data to be passed into the job + * @param {Function|String} job - function or path to a module defining a job + * @param {Object} [data] - data to be passed into the job */ scheduleJob(when, job, data) { let schedule;