Added ability to remove scheduled jobs

closes #119

- A future use-case which this feature caters for is allowing to migrate "post scheduler" to use job manager instead of managing scheduling itself
- removeJob method will be needed to allow "rescheduling" of the post
This commit is contained in:
Naz 2020-12-08 16:43:18 +13:00
parent 86b9f0d2ce
commit 320e7feb0e
2 changed files with 32 additions and 1 deletions

View File

@ -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]
*/

View File

@ -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);