From 7a6d9a68bd1898abb7b4161f867c3320ea27c5c0 Mon Sep 17 00:00:00 2001 From: Kevin Sensory Date: Mon, 25 Oct 2021 09:27:56 +0200 Subject: [PATCH 1/2] added ID example to the documentation --- README.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/README.md b/README.md index fcb2022..5fa9efc 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ scheduler.addSimpleIntervalJob(job) scheduler.stop() ``` + ## Usage with async tasks In order to avoid unhandled rejections, make sure to use AsyncTask if your task is asynchronous: @@ -55,6 +56,50 @@ scheduler.stop() Note that in order to avoid memory leaks, it is recommended to use promise chains instead of async/await inside task definition. See [talk on common Promise mistakes](https://www.youtube.com/watch?v=XV-u_Ow47s0) for more details. +## Using IDs and ES6-style imports + +You can attach IDs to tasks to identify them later. This is helpful in projects that run a lot of tasks and especially if you wanna target the tasks specifically (e.g. starting, stopping or checking their status). + +```js +import { ToadScheduler, SimpleIntervalJob, Task } from 'toad-scheduler'; + +const scheduler = new ToadScheduler(); + +const task = new Task('simple task', () => { + console.log('Task triggered'); +}); + +const job1 = new SimpleIntervalJob( + { seconds: 20, runImmediately: true }, + task, + 'id_1' +); + +const job2 = new SimpleIntervalJob( + { seconds: 15, runImmediately: true }, + task, + 'id_2' +); + +//create and start jobs +scheduler.addSimpleIntervalJob(job1); +scheduler.addSimpleIntervalJob(job2); + +// stop job with ID: id_2 +scheduler.stopById('id_2'); + +// remove job with ID: id_1 +scheduler.removeById('id_1'); + +// check status of jobs +console.log(scheduler.getById('id_1').getStatus()); // returns Error (job not found) + +console.log(scheduler.getById('id_2').getStatus()); // returns "stopped" and can be started again + +``` + + + ## API for schedule * `days?: number` - how many days to wait before executing the job for the next time; From 7fb72665eb3ce775616f5360a10b5f30150481af Mon Sep 17 00:00:00 2001 From: Kevin Potschien <74183739+kpotschi@users.noreply.github.com> Date: Sun, 31 Oct 2021 21:17:36 +0100 Subject: [PATCH 2/2] Update README.md Co-authored-by: Igor Savin --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5fa9efc..0bb5451 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ Note that in order to avoid memory leaks, it is recommended to use promise chain ## Using IDs and ES6-style imports -You can attach IDs to tasks to identify them later. This is helpful in projects that run a lot of tasks and especially if you wanna target the tasks specifically (e.g. starting, stopping or checking their status). +You can attach IDs to tasks to identify them later. This is helpful in projects that run a lot of tasks and especially if you want to target some of the tasks specifically (e. g. in order to stop or restart them, or to check their status). ```js import { ToadScheduler, SimpleIntervalJob, Task } from 'toad-scheduler';