diff --git a/ghost/job-manager/lib/job-manager.js b/ghost/job-manager/lib/job-manager.js index 8ab18986ff..d838e668c7 100644 --- a/ghost/job-manager/lib/job-manager.js +++ b/ghost/job-manager/lib/job-manager.js @@ -111,6 +111,21 @@ class JobManager { return this.bree.start(name); } + /** + * Removes a job from sqcheduled (offloaded) jobs queue. + * There is no way to remove jovs from in-line (same event loop) jobs + * added through `addJob` method. + * 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); + } + /** * @param {import('p-wait-for').Options} [options] */ diff --git a/ghost/job-manager/test/job-manager.test.js b/ghost/job-manager/test/job-manager.test.js index 19b4f12a7f..e157a360f1 100644 --- a/ghost/job-manager/test/job-manager.test.js +++ b/ghost/job-manager/test/job-manager.test.js @@ -59,7 +59,7 @@ describe('Job Manager', function () { }); }); - describe('Schedule Job', function () { + describe('Schedule a Job', function () { it('fails to schedule for invalid scheduling expression', function () { const jobManager = new JobManager(logging); @@ -115,6 +115,22 @@ describe('Job Manager', function () { }); }); + describe('Remove a Job', function () { + it('removes a scheduled job from the queue', async function () { + const jobManager = new JobManager(logging); + + const timeInTenSeconds = new Date(Date.now() + 10); + const jobPath = path.resolve(__dirname, './jobs/simple.js'); + + jobManager.scheduleJob(timeInTenSeconds, jobPath, null, 'job-in-ten'); + jobManager.bree.config.jobs[0].name.should.equal('job-in-ten'); + + await jobManager.removeJob('job-in-ten'); + + should(jobManager.bree.config.jobs[0]).be.undefined; + }); + }); + describe('Shutdown', function () { it('gracefully shuts down a synchronous jobs', async function () { const jobManager = new JobManager(logging);