Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete a task by reference or by name #385

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
12 changes: 11 additions & 1 deletion src/node-cron.js
Expand Up @@ -61,4 +61,14 @@ function getTasks() {
return storage.getTasks();
}

module.exports = { schedule, validate, getTasks };
/**
* Deletes a scheduled task.
* @param {string || ScheduledTask} task This can be a string of the task Id or the task object itself.
* @returns {ScheduledTask[]} The scheduled tasks.
*/
function deleteTask(task) {
storage.delete(task);
return this.getTasks();
}

module.exports = { schedule, validate, getTasks, deleteTask };
9 changes: 9 additions & 0 deletions src/storage.js
Expand Up @@ -14,6 +14,15 @@ module.exports = (() => {
},
getTasks: () => {
return global.scheduledTasks;
},
delete: (task) => {
if (typeof task === 'string') {
const taskInstance = global.scheduledTasks.get(task).stop();
brian-geoghegan marked this conversation as resolved.
Show resolved Hide resolved
global.scheduledTasks.delete(task);
brian-geoghegan marked this conversation as resolved.
Show resolved Hide resolved
} else {
task.stop();
global.scheduledTasks.delete(task.options.name);
brian-geoghegan marked this conversation as resolved.
Show resolved Hide resolved
}
}
};
})();
30 changes: 30 additions & 0 deletions test/node-cron-test.js
Expand Up @@ -69,6 +69,36 @@ describe('node-cron', () => {

assert.equal(0, executed);
});

it('should delete a scheduled task', () => {
const name = 'testId';
let executed = 0;
const task = cron.schedule('* * * * * *', () => {
executed += 1;
}, { scheduled: true, name });

this.clock.tick(2000);

assert.equal(2, executed);
assert.equal(cron.getTasks().get(name), task);
cron.deleteTask(task);
assert.equal(cron.getTasks().get(name), undefined);
});

it('should delete a scheduled task by name', () => {
const name = 'testId';
let executed = 0;
const task = cron.schedule('* * * * * *', () => {
executed += 1;
}, { scheduled: true, name });

this.clock.tick(2000);

assert.equal(2, executed);
assert.equal(cron.getTasks().get(name), task);
cron.deleteTask(name);
assert.equal(cron.getTasks().get(name), undefined);
});

it('should start a stoped task', () => {
let executed = 0;
Expand Down
9 changes: 9 additions & 0 deletions test/storage-test.js
Expand Up @@ -14,6 +14,15 @@ describe('storage', () => {
assert.lengthOf(storage.getTasks(), 1);
});

it('should delete a task', () => {
global.scheduledTasks = new Map();
global.scheduledTasks.set('id1', 'test');
global.scheduledTasks.set('id2', 'test2');
storage.delete('id2');
assert.equal(storage.getTasks().get('id1'), 'test');
assert.equal(storage.getTasks().get('id2'), undefined);
});

describe('on import', () => {
it('should keep stored items across imports', () => {
delete require.cache[require.resolve('../src/storage')];
Expand Down