From 013051a6c9034e06bea337b6ba1969ca693eb922 Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 26 Jul 2022 15:12:44 +0100 Subject: [PATCH] Made name parameter required for one off jobs refs https://github.com/TryGhost/Toolbox/issues/359 - Without a "name" parameter it's impossible to identify a job in the storage. It was missed during the PoC inmplementation --- ghost/job-manager/lib/job-manager.js | 8 +++++++- ghost/job-manager/test/job-manager.test.js | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ghost/job-manager/lib/job-manager.js b/ghost/job-manager/lib/job-manager.js index 9ec056e985..5c623857ee 100644 --- a/ghost/job-manager/lib/job-manager.js +++ b/ghost/job-manager/lib/job-manager.js @@ -179,12 +179,18 @@ class JobManager { * * @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} 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 */ async addOneOffJob({name, job, data, offloaded = true}) { + if (!name) { + throw new IncorrectUsageError({ + message: `The name parameter is required for a one off job.` + }); + } + const persistedJob = await this._jobsRepository.read(name); if (persistedJob) { diff --git a/ghost/job-manager/test/job-manager.test.js b/ghost/job-manager/test/job-manager.test.js index 5efbbc3fd6..e9a5fc9d2a 100644 --- a/ghost/job-manager/test/job-manager.test.js +++ b/ghost/job-manager/test/job-manager.test.js @@ -253,6 +253,19 @@ describe('Job Manager', function () { }); describe('Add one off job', function () { + it('throws if name parameter is not provided', async function () { + const jobManager = new JobManager({}); + + try { + await jobManager.addOneOffJob({ + job: () => {} + }); + throw new Error('should have thrown'); + } catch (err) { + should.equal(err.message, 'The name parameter is required for a one off job.'); + } + }); + it('adds job to the queue when it is a unique one', async function () { const spy = sinon.spy(); const JobModel = {