From 7d443b015af2359dd319718b456e077dde65d117 Mon Sep 17 00:00:00 2001 From: Brian Geoghegan Date: Tue, 22 Nov 2022 18:52:48 +0000 Subject: [PATCH 01/14] Delete a task by reference or by name --- src/node-cron.js | 12 +++++++++++- src/storage.js | 7 +++++++ test/node-cron-test.js | 30 ++++++++++++++++++++++++++++++ test/storage-test.js | 12 ++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/node-cron.js b/src/node-cron.js index 459f161..7bc9fba 100644 --- a/src/node-cron.js +++ b/src/node-cron.js @@ -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 }; diff --git a/src/storage.js b/src/storage.js index 6c131a4..57972ea 100644 --- a/src/storage.js +++ b/src/storage.js @@ -14,6 +14,13 @@ module.exports = (() => { }, getTasks: () => { return global.scheduledTasks; + }, + delete: (task) => { + if (typeof task === 'string') { + global.scheduledTasks.delete(task); + } else { + global.scheduledTasks.delete(task.options.name); + } } }; })(); \ No newline at end of file diff --git a/test/node-cron-test.js b/test/node-cron-test.js index d65de1e..cfdb74f 100644 --- a/test/node-cron-test.js +++ b/test/node-cron-test.js @@ -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; diff --git a/test/storage-test.js b/test/storage-test.js index b7a7316..3c3f518 100644 --- a/test/storage-test.js +++ b/test/storage-test.js @@ -14,6 +14,18 @@ describe('storage', () => { assert.lengthOf(storage.getTasks(), 1); }); + it('should delete a tasks', () => { + global.scheduledTasks = new Map(); + global.scheduledTasks.set('id1', {}); + global.scheduledTasks.set('id2', {}); + assert.lengthOf(storage.getTasks(), 2); + storage.delete('id2'); + assert.lengthOf(storage.getTasks(), 1); + assert.equal(storage.getTasks()); + assert.equal(storage.getTasks().get('id1'), {}); + assert.equal(storage.getTasks().get('id2'), undefined); + }); + describe('on import', () => { it('should keep stored items across imports', () => { delete require.cache[require.resolve('../src/storage')]; From 93ce38325b2fe1ef9d22790585a2914f3c6bc9ec Mon Sep 17 00:00:00 2001 From: Brian Geoghegan Date: Tue, 22 Nov 2022 19:01:40 +0000 Subject: [PATCH 02/14] lint fix --- test/storage-test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/storage-test.js b/test/storage-test.js index 3c3f518..e479c61 100644 --- a/test/storage-test.js +++ b/test/storage-test.js @@ -14,14 +14,13 @@ describe('storage', () => { assert.lengthOf(storage.getTasks(), 1); }); - it('should delete a tasks', () => { + it('should delete a task', () => { global.scheduledTasks = new Map(); global.scheduledTasks.set('id1', {}); global.scheduledTasks.set('id2', {}); assert.lengthOf(storage.getTasks(), 2); storage.delete('id2'); assert.lengthOf(storage.getTasks(), 1); - assert.equal(storage.getTasks()); assert.equal(storage.getTasks().get('id1'), {}); assert.equal(storage.getTasks().get('id2'), undefined); }); From e560293eca4fb353eaca140490dfedc6e57c2a59 Mon Sep 17 00:00:00 2001 From: Brian Geoghegan Date: Tue, 22 Nov 2022 19:04:21 +0000 Subject: [PATCH 03/14] lint fix --- test/storage-test.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/storage-test.js b/test/storage-test.js index e479c61..fa46e84 100644 --- a/test/storage-test.js +++ b/test/storage-test.js @@ -16,12 +16,10 @@ describe('storage', () => { it('should delete a task', () => { global.scheduledTasks = new Map(); - global.scheduledTasks.set('id1', {}); - global.scheduledTasks.set('id2', {}); - assert.lengthOf(storage.getTasks(), 2); + global.scheduledTasks.set('id1', 'test'); + global.scheduledTasks.set('id2', 'test2'); storage.delete('id2'); - assert.lengthOf(storage.getTasks(), 1); - assert.equal(storage.getTasks().get('id1'), {}); + assert.equal(storage.getTasks().get('id1'), 'test'); assert.equal(storage.getTasks().get('id2'), undefined); }); From bf49259b57d3227dcabb41d94f17ca2a9bd851d5 Mon Sep 17 00:00:00 2001 From: brian-geoghegan Date: Thu, 24 Nov 2022 15:41:45 +0000 Subject: [PATCH 04/14] Update src/storage.js --- src/storage.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/storage.js b/src/storage.js index 57972ea..2b4724b 100644 --- a/src/storage.js +++ b/src/storage.js @@ -17,6 +17,8 @@ module.exports = (() => { }, delete: (task) => { if (typeof task === 'string') { + const taskInstance = global.scheduledTasks.get(task).stop(); + delete taskInstance; global.scheduledTasks.delete(task); } else { global.scheduledTasks.delete(task.options.name); From 3e208962743e7eea0fd41650c8a10bcbd7370338 Mon Sep 17 00:00:00 2001 From: brian-geoghegan Date: Thu, 24 Nov 2022 15:42:20 +0000 Subject: [PATCH 05/14] Update src/storage.js --- src/storage.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/storage.js b/src/storage.js index 2b4724b..9e368ad 100644 --- a/src/storage.js +++ b/src/storage.js @@ -21,6 +21,8 @@ module.exports = (() => { delete taskInstance; global.scheduledTasks.delete(task); } else { + task.stop(); + delete task; global.scheduledTasks.delete(task.options.name); } } From ae38c6f9c8229d973019ae3c50719b6d8cbce39d Mon Sep 17 00:00:00 2001 From: brian-geoghegan Date: Thu, 24 Nov 2022 15:47:16 +0000 Subject: [PATCH 06/14] Update src/storage.js --- src/storage.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/storage.js b/src/storage.js index 9e368ad..a74d07f 100644 --- a/src/storage.js +++ b/src/storage.js @@ -18,6 +18,7 @@ module.exports = (() => { delete: (task) => { if (typeof task === 'string') { const taskInstance = global.scheduledTasks.get(task).stop(); + // eslint-disable-next-line class-methods-use-this delete taskInstance; global.scheduledTasks.delete(task); } else { From a68dca581d09b2461a9265143d917b4386ceb893 Mon Sep 17 00:00:00 2001 From: brian-geoghegan Date: Thu, 24 Nov 2022 15:47:33 +0000 Subject: [PATCH 07/14] Update src/storage.js --- src/storage.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/storage.js b/src/storage.js index a74d07f..b574459 100644 --- a/src/storage.js +++ b/src/storage.js @@ -23,6 +23,7 @@ module.exports = (() => { global.scheduledTasks.delete(task); } else { task.stop(); + // eslint-disable-next-line class-methods-use-this delete task; global.scheduledTasks.delete(task.options.name); } From 78f805a52baf47a8bda33ff2f30a5257bf30d837 Mon Sep 17 00:00:00 2001 From: brian-geoghegan Date: Thu, 24 Nov 2022 15:51:05 +0000 Subject: [PATCH 08/14] Update src/storage.js --- src/storage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage.js b/src/storage.js index b574459..8f4ef8e 100644 --- a/src/storage.js +++ b/src/storage.js @@ -18,7 +18,7 @@ module.exports = (() => { delete: (task) => { if (typeof task === 'string') { const taskInstance = global.scheduledTasks.get(task).stop(); - // eslint-disable-next-line class-methods-use-this + // eslint-disable-next-line no-delete-var delete taskInstance; global.scheduledTasks.delete(task); } else { From 4364b03ff7a487fe322cd810d7c58fae30dbbed5 Mon Sep 17 00:00:00 2001 From: brian-geoghegan Date: Thu, 24 Nov 2022 15:51:20 +0000 Subject: [PATCH 09/14] Update src/storage.js --- src/storage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage.js b/src/storage.js index 8f4ef8e..c5e792b 100644 --- a/src/storage.js +++ b/src/storage.js @@ -23,7 +23,7 @@ module.exports = (() => { global.scheduledTasks.delete(task); } else { task.stop(); - // eslint-disable-next-line class-methods-use-this + // eslint-disable-next-line no-delete-var delete task; global.scheduledTasks.delete(task.options.name); } From df60832cb0c811df044ef32b1b0efb4eaa187269 Mon Sep 17 00:00:00 2001 From: brian-geoghegan Date: Thu, 24 Nov 2022 15:53:16 +0000 Subject: [PATCH 10/14] Update src/storage.js --- src/storage.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/storage.js b/src/storage.js index c5e792b..d6f155c 100644 --- a/src/storage.js +++ b/src/storage.js @@ -23,7 +23,6 @@ module.exports = (() => { global.scheduledTasks.delete(task); } else { task.stop(); - // eslint-disable-next-line no-delete-var delete task; global.scheduledTasks.delete(task.options.name); } From a5aa61fe6e9b7068a1b81cdc663e6923173a1a7d Mon Sep 17 00:00:00 2001 From: brian-geoghegan Date: Thu, 24 Nov 2022 15:53:23 +0000 Subject: [PATCH 11/14] Update src/storage.js --- src/storage.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/storage.js b/src/storage.js index d6f155c..d489494 100644 --- a/src/storage.js +++ b/src/storage.js @@ -23,7 +23,6 @@ module.exports = (() => { global.scheduledTasks.delete(task); } else { task.stop(); - delete task; global.scheduledTasks.delete(task.options.name); } } From cb0818d341c52e02d21119897062c8e318cf8e21 Mon Sep 17 00:00:00 2001 From: brian-geoghegan Date: Thu, 24 Nov 2022 15:53:51 +0000 Subject: [PATCH 12/14] Update src/storage.js --- src/storage.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/storage.js b/src/storage.js index d489494..be4aab5 100644 --- a/src/storage.js +++ b/src/storage.js @@ -18,7 +18,6 @@ module.exports = (() => { delete: (task) => { if (typeof task === 'string') { const taskInstance = global.scheduledTasks.get(task).stop(); - // eslint-disable-next-line no-delete-var delete taskInstance; global.scheduledTasks.delete(task); } else { From 7309f7155d7feacd2874c77e5e8ccfd09f4e062f Mon Sep 17 00:00:00 2001 From: brian-geoghegan Date: Thu, 24 Nov 2022 15:54:04 +0000 Subject: [PATCH 13/14] Update src/storage.js --- src/storage.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/storage.js b/src/storage.js index be4aab5..37fd195 100644 --- a/src/storage.js +++ b/src/storage.js @@ -18,7 +18,6 @@ module.exports = (() => { delete: (task) => { if (typeof task === 'string') { const taskInstance = global.scheduledTasks.get(task).stop(); - delete taskInstance; global.scheduledTasks.delete(task); } else { task.stop(); From b33bd8225e2ef47760124f9b37d039242651db3c Mon Sep 17 00:00:00 2001 From: brian-geoghegan Date: Fri, 25 Nov 2022 12:02:43 +0000 Subject: [PATCH 14/14] Update src/storage.js --- src/storage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage.js b/src/storage.js index 37fd195..4b57b38 100644 --- a/src/storage.js +++ b/src/storage.js @@ -17,7 +17,7 @@ module.exports = (() => { }, delete: (task) => { if (typeof task === 'string') { - const taskInstance = global.scheduledTasks.get(task).stop(); + global.scheduledTasks.get(task).stop(); global.scheduledTasks.delete(task); } else { task.stop();