node-schedule 1.3.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/README.md +98 -51
- package/UPGRADING.md +20 -0
- package/index.js +17 -0
- package/lib/Invocation.js +348 -0
- package/lib/Job.js +287 -0
- package/lib/schedule.js +55 -598
- package/lib/utils/dateUtils.js +11 -0
- package/package.json +35 -13
- package/.eslintrc +0 -17
- package/.travis.yml +0 -15
- package/CONTRIBUTING.md +0 -31
- package/test/.eslintrc +0 -5
- package/test/cancel-long-running-jobs.js +0 -24
- package/test/convenience-method-test.js +0 -713
- package/test/date-convenience-methods-test.js +0 -59
- package/test/es6/job-test.js +0 -33
- package/test/job-test.js +0 -517
- package/test/range-test.js +0 -77
- package/test/recurrence-rule-test.js +0 -375
- package/test/schedule-cron-jobs.js +0 -134
- package/test/start-end-test.js +0 -327
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Master (Unreleased)
|
|
2
|
+
|
|
3
|
+
# 2.1.0 - 28 November, 2021
|
|
4
|
+
|
|
5
|
+
### New features:
|
|
6
|
+
|
|
7
|
+
- Implement support for graceful shutdown #583
|
|
8
|
+
- Emit a success event after a successful job invocation. #643
|
|
9
|
+
|
|
10
|
+
# 2.0.0 - 01 February, 2021
|
|
11
|
+
|
|
12
|
+
### Breaking changes:
|
|
13
|
+
|
|
14
|
+
- Drop support for Node < 6
|
|
15
|
+
- Remove support for job objects. See `UPGRADING.md` for more details. #557
|
|
16
|
+
|
|
17
|
+
### New features:
|
|
18
|
+
|
|
19
|
+
- Introduce compatibility with browsers and Electron #553
|
|
20
|
+
|
|
21
|
+
### Bug fixes:
|
|
22
|
+
|
|
23
|
+
- Avoid leaking memory on one-off jobs #581
|
|
24
|
+
- Address anonymous job id collision problem to ensure that node-schedule could run with the unlimited uptime #582
|
|
25
|
+
|
|
26
|
+
### Internal changes:
|
|
27
|
+
|
|
28
|
+
- Bump cron-parser from 2.18.0 to 3.1.0 #562
|
|
29
|
+
- Bump sinon from 1.17.7 to 9.2.4 #566
|
|
30
|
+
- Switch to nyc from istanbul #574
|
|
31
|
+
- Convert tests from nodeunit to Tape #575
|
|
32
|
+
- Replace var with let/const #577
|
|
33
|
+
- Execute airtap tests on a headless Chromium #578
|
|
34
|
+
- Make structure more modular #579
|
package/README.md
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/node-schedule)
|
|
4
4
|
[](https://www.npmjs.com/package/node-schedule)
|
|
5
|
-
[](https://github.com/node-schedule/node-schedule/actions)
|
|
6
|
+
[](https://coveralls.io/r/node-schedule/node-schedule?branch=master)
|
|
6
7
|
[](https://gitter.im/node-schedule/node-schedule?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
|
7
8
|
|
|
8
9
|
[](https://nodei.co/npm/node-schedule/)
|
|
@@ -12,45 +13,59 @@ It allows you to schedule jobs (arbitrary functions) for execution at
|
|
|
12
13
|
specific dates, with optional recurrence rules. It only uses a single timer
|
|
13
14
|
at any given time (rather than reevaluating upcoming jobs every second/minute).
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
### Installation
|
|
18
|
-
|
|
19
|
-
You can install using [npm](https://www.npmjs.com/package/node-schedule).
|
|
16
|
+
Node 6+ is supported.
|
|
20
17
|
|
|
21
|
-
|
|
22
|
-
npm install node-schedule
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
### Overview
|
|
18
|
+
## Overview
|
|
26
19
|
|
|
27
20
|
Node Schedule is for time-based scheduling, not interval-based scheduling.
|
|
21
|
+
|
|
28
22
|
While you can easily bend it to your will, if you only want to do something like
|
|
29
|
-
"run this function every 5 minutes",
|
|
30
|
-
and far more appropriate. But if you want to, say, "run this function at the :20
|
|
23
|
+
"run this function every 5 minutes", [toad-scheduler](https://github.com/kibertoad/toad-scheduler) would be a better choice. But if you want to, say, "run this function at the :20
|
|
31
24
|
and :50 of every hour on the third Tuesday of every month," you'll find that
|
|
32
25
|
Node Schedule suits your needs better. Additionally, Node Schedule has Windows
|
|
33
|
-
support unlike true cron since the node runtime is now fully supported.
|
|
26
|
+
support, unlike true `cron`, since the node runtime is now fully supported.
|
|
34
27
|
|
|
35
28
|
Note that Node Schedule is designed for in-process scheduling, i.e. scheduled jobs
|
|
36
29
|
will only fire as long as your script is running, and the schedule will disappear
|
|
37
30
|
when execution completes. If you need to schedule jobs that will persist even when
|
|
38
31
|
your script *isn't* running, consider using actual [cron].
|
|
39
32
|
|
|
33
|
+
In case you need durable jobs that persist across restarts and lock system compatible with multi-node deployments,
|
|
34
|
+
try [agenda](https://github.com/agenda/agenda) or [bree](https://github.com/breejs/bree).
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
### Installation
|
|
39
|
+
|
|
40
|
+
You can install using [npm](https://www.npmjs.com/package/node-schedule).
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
npm install node-schedule
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
|
|
40
47
|
### Jobs and Scheduling
|
|
41
48
|
|
|
42
49
|
Every scheduled job in Node Schedule is represented by a `Job` object. You can
|
|
43
50
|
create jobs manually, then execute the `schedule()` method to apply a schedule,
|
|
44
51
|
or use the convenience function `scheduleJob()` as demonstrated below.
|
|
45
52
|
|
|
46
|
-
`Job` objects are `EventEmitter`
|
|
47
|
-
|
|
48
|
-
`
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
`Job` objects are `EventEmitter`s, and emit the following events:
|
|
54
|
+
* A `run` event after each execution.
|
|
55
|
+
* A `scheduled` event each time they're scheduled to run.
|
|
56
|
+
* A `canceled` event when an invocation is canceled before it's executed.
|
|
57
|
+
Note that `canceled` is the single-L American spelling.
|
|
58
|
+
* An `error` event when a job invocation triggered by a schedule throws or returns
|
|
59
|
+
a rejected `Promise`.
|
|
60
|
+
* A `success` event when a job invocation triggered by a schedule returns successfully or
|
|
61
|
+
returns a resolved `Promise`. In any case, the `success` event receives the value returned
|
|
62
|
+
by the callback or in case of a promise, the resolved value.
|
|
63
|
+
|
|
64
|
+
(Both the `scheduled` and `canceled` events receive a JavaScript date object as
|
|
65
|
+
a parameter).
|
|
66
|
+
Note that jobs are scheduled the first time immediately, so if you create a job
|
|
67
|
+
using the `scheduleJob()` convenience method, you'll miss the first `scheduled`
|
|
68
|
+
event, but you can query the invocation manually (see below).
|
|
54
69
|
|
|
55
70
|
### Cron-style Scheduling
|
|
56
71
|
|
|
@@ -70,9 +85,9 @@ The cron format consists of:
|
|
|
70
85
|
Examples with the cron format:
|
|
71
86
|
|
|
72
87
|
```js
|
|
73
|
-
|
|
88
|
+
const schedule = require('node-schedule');
|
|
74
89
|
|
|
75
|
-
|
|
90
|
+
const job = schedule.scheduleJob('42 * * * *', function(){
|
|
76
91
|
console.log('The answer to life, the universe, and everything!');
|
|
77
92
|
});
|
|
78
93
|
```
|
|
@@ -82,7 +97,7 @@ Execute a cron job when the minute is 42 (e.g. 19:42, 20:42, etc.).
|
|
|
82
97
|
And:
|
|
83
98
|
|
|
84
99
|
```js
|
|
85
|
-
|
|
100
|
+
const job = schedule.scheduleJob('0 17 ? * 0,4-6', function(){
|
|
86
101
|
console.log('Today is recognized by Rebecca Black!');
|
|
87
102
|
});
|
|
88
103
|
```
|
|
@@ -91,16 +106,16 @@ Execute a cron job every 5 Minutes = */5 * * * *
|
|
|
91
106
|
|
|
92
107
|
You can also get when it is scheduled to run for every invocation of the job:
|
|
93
108
|
```js
|
|
94
|
-
|
|
109
|
+
const job = schedule.scheduleJob('0 1 * * *', function(fireDate){
|
|
95
110
|
console.log('This job was supposed to run at ' + fireDate + ', but actually ran at ' + new Date());
|
|
96
111
|
});
|
|
97
112
|
```
|
|
98
113
|
This is useful when you need to check if there is a delay of the job invocation when the system is busy, or save a record of all invocations of a job for audit purpose.
|
|
99
114
|
#### Unsupported Cron Features
|
|
100
115
|
|
|
101
|
-
Currently, `W` (nearest weekday)
|
|
102
|
-
|
|
103
|
-
|
|
116
|
+
Currently, `W` (nearest weekday) and `L` (last day of month/week) are not supported.
|
|
117
|
+
Most other features supported by popular cron implementations should work just fine,
|
|
118
|
+
including `#` (nth weekday of the month).
|
|
104
119
|
|
|
105
120
|
[cron-parser] is used to parse crontab instructions.
|
|
106
121
|
|
|
@@ -110,10 +125,10 @@ Say you very specifically want a function to execute at 5:30am on December 21, 2
|
|
|
110
125
|
Remember - in JavaScript - 0 - January, 11 - December.
|
|
111
126
|
|
|
112
127
|
```js
|
|
113
|
-
|
|
114
|
-
|
|
128
|
+
const schedule = require('node-schedule');
|
|
129
|
+
const date = new Date(2012, 11, 21, 5, 30, 0);
|
|
115
130
|
|
|
116
|
-
|
|
131
|
+
const job = schedule.scheduleJob(date, function(){
|
|
117
132
|
console.log('The world is going to end today.');
|
|
118
133
|
});
|
|
119
134
|
```
|
|
@@ -121,10 +136,10 @@ var j = schedule.scheduleJob(date, function(){
|
|
|
121
136
|
To use current data in the future you can use binding:
|
|
122
137
|
|
|
123
138
|
```js
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
139
|
+
const schedule = require('node-schedule');
|
|
140
|
+
const date = new Date(2012, 11, 21, 5, 30, 0);
|
|
141
|
+
const x = 'Tada!';
|
|
142
|
+
const job = schedule.scheduleJob(date, function(y){
|
|
128
143
|
console.log(y);
|
|
129
144
|
}.bind(null,x));
|
|
130
145
|
x = 'Changing Data';
|
|
@@ -138,12 +153,12 @@ You can build recurrence rules to specify when a job should recur. For instance,
|
|
|
138
153
|
consider this rule, which executes the function every hour at 42 minutes after the hour:
|
|
139
154
|
|
|
140
155
|
```js
|
|
141
|
-
|
|
156
|
+
const schedule = require('node-schedule');
|
|
142
157
|
|
|
143
|
-
|
|
158
|
+
const rule = new schedule.RecurrenceRule();
|
|
144
159
|
rule.minute = 42;
|
|
145
160
|
|
|
146
|
-
|
|
161
|
+
const job = schedule.scheduleJob(rule, function(){
|
|
147
162
|
console.log('The answer to life, the universe, and everything!');
|
|
148
163
|
});
|
|
149
164
|
```
|
|
@@ -153,16 +168,30 @@ object to specify a range of start and end values, with an optional step paramet
|
|
|
153
168
|
For instance, this will print a message on Thursday, Friday, Saturday, and Sunday at 5pm:
|
|
154
169
|
|
|
155
170
|
```js
|
|
156
|
-
|
|
171
|
+
const rule = new schedule.RecurrenceRule();
|
|
157
172
|
rule.dayOfWeek = [0, new schedule.Range(4, 6)];
|
|
158
173
|
rule.hour = 17;
|
|
159
174
|
rule.minute = 0;
|
|
160
175
|
|
|
161
|
-
|
|
176
|
+
const job = schedule.scheduleJob(rule, function(){
|
|
162
177
|
console.log('Today is recognized by Rebecca Black!');
|
|
163
178
|
});
|
|
164
179
|
```
|
|
165
180
|
|
|
181
|
+
Timezones are also supported. Here is an example of executing at the start of every day in the UTC timezone.
|
|
182
|
+
|
|
183
|
+
```js
|
|
184
|
+
const rule = new schedule.RecurrenceRule();
|
|
185
|
+
rule.hour = 0;
|
|
186
|
+
rule.tz = 'Etc/UTC';
|
|
187
|
+
|
|
188
|
+
const job = schedule.scheduleJob(rule, function(){
|
|
189
|
+
console.log('A new day has begun in the UTC timezone!');
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
A list of acceptable tz (timezone) values can be found at <https://en.wikipedia.org/wiki/List_of_tz_database_time_zones>
|
|
194
|
+
|
|
166
195
|
#### RecurrenceRule properties
|
|
167
196
|
|
|
168
197
|
- `second (0-59)`
|
|
@@ -172,6 +201,8 @@ var j = schedule.scheduleJob(rule, function(){
|
|
|
172
201
|
- `month (0-11)`
|
|
173
202
|
- `year`
|
|
174
203
|
- `dayOfWeek (0-6) Starting with Sunday`
|
|
204
|
+
- `tz`
|
|
205
|
+
|
|
175
206
|
|
|
176
207
|
> **Note**: It's worth noting that the default value of a component of a recurrence rule is
|
|
177
208
|
> `null` (except for second, which is 0 for familiarity with cron). *If we did not
|
|
@@ -184,7 +215,7 @@ To make things a little easier, an object literal syntax is also supported, like
|
|
|
184
215
|
in this example which will log a message every Sunday at 2:30pm:
|
|
185
216
|
|
|
186
217
|
```js
|
|
187
|
-
|
|
218
|
+
const job = schedule.scheduleJob({hour: 14, minute: 30, dayOfWeek: 0}, function(){
|
|
188
219
|
console.log('Time for tea!');
|
|
189
220
|
});
|
|
190
221
|
```
|
|
@@ -195,13 +226,29 @@ It will run after 5 seconds and stop after 10 seconds in this example.
|
|
|
195
226
|
The ruledat supports the above.
|
|
196
227
|
|
|
197
228
|
```js
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
229
|
+
const startTime = new Date(Date.now() + 5000);
|
|
230
|
+
const endTime = new Date(startTime.getTime() + 5000);
|
|
231
|
+
const job = schedule.scheduleJob({ start: startTime, end: endTime, rule: '*/1 * * * * *' }, function(){
|
|
201
232
|
console.log('Time for tea!');
|
|
202
233
|
});
|
|
203
234
|
```
|
|
204
235
|
|
|
236
|
+
### Graceful Shutdown.
|
|
237
|
+
You can shutdown jobs gracefully.
|
|
238
|
+
`gracefulShutdown()` will cancel all jobs and return Promise.
|
|
239
|
+
It will wait until all jobs are terminated.
|
|
240
|
+
```js
|
|
241
|
+
schedule.gracefulShutdown();
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
You can also gracefully shutdown jobs when a system interrupt occurs.
|
|
245
|
+
```
|
|
246
|
+
process.on('SIGINT', function () {
|
|
247
|
+
schedule.gracefulShutdown()
|
|
248
|
+
.then(() => process.exit(0))
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
205
252
|
### Handle Jobs and Job Invocations
|
|
206
253
|
|
|
207
254
|
There are some function to get information for a Job and to handle the Job and
|
|
@@ -231,8 +278,9 @@ This method returns a Date object for the planned next Invocation for this Job.
|
|
|
231
278
|
|
|
232
279
|
## Contributing
|
|
233
280
|
|
|
234
|
-
This module was originally developed by [Matt Patenaude]
|
|
235
|
-
|
|
281
|
+
This module was originally developed by [Matt Patenaude] who eventually passed over maintainer's mantle over to [Tejas Manohar].
|
|
282
|
+
|
|
283
|
+
Currently it is being maintained by [Igor Savin] and [our amazing community].
|
|
236
284
|
|
|
237
285
|
We'd love to get your contributions. Individuals making significant and valuable
|
|
238
286
|
contributions are given commit-access to the project to contribute as they see fit.
|
|
@@ -243,14 +291,13 @@ Before jumping in, check out our [Contributing] page guide!
|
|
|
243
291
|
|
|
244
292
|
Copyright 2015 Matt Patenaude.
|
|
245
293
|
|
|
246
|
-
Licensed under the **[MIT License]
|
|
294
|
+
Licensed under the **[MIT License](https://github.com/node-schedule/node-schedule/blob/master/LICENSE)**.
|
|
247
295
|
|
|
248
296
|
|
|
249
297
|
[cron]: http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5
|
|
250
298
|
[Contributing]: https://github.com/node-schedule/node-schedule/blob/master/CONTRIBUTING.md
|
|
251
299
|
[Matt Patenaude]: https://github.com/mattpat
|
|
252
300
|
[Tejas Manohar]: http://tejas.io
|
|
253
|
-
[
|
|
254
|
-
[
|
|
255
|
-
[other wonderful contributors]: https://github.com/node-schedule/node-schedule/graphs/contributors
|
|
301
|
+
[Igor Savin]: https://twitter.com/kibertoad
|
|
302
|
+
[our amazing community]: https://github.com/node-schedule/node-schedule/graphs/contributors
|
|
256
303
|
[cron-parser]: https://github.com/harrisiirak/cron-parser
|
package/UPGRADING.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
## Upgrading to new node-schedule versions
|
|
2
|
+
|
|
3
|
+
### Upgrading to version 2.0.0+
|
|
4
|
+
|
|
5
|
+
* Node.js versions older than 6 are no longer supported, please update your environment before upgrading.
|
|
6
|
+
* In order to prevent memory leaks, one-off jobs (targeted to be executed at an exact date) cannot be rescheduled by name, as reference to them is no longer stored indefinitely. If you want to keep rescheduling them, make sure to store reference to the initial job.
|
|
7
|
+
* The `scheduleJob()` method no longer supports passing an object with the job method. If you were using an object, pass the job method directly.
|
|
8
|
+
|
|
9
|
+
E.g. code that previously looked like this:
|
|
10
|
+
```javascript
|
|
11
|
+
const obj = {
|
|
12
|
+
execute() {}
|
|
13
|
+
};
|
|
14
|
+
Scheduler.scheduleJob(obj);
|
|
15
|
+
```
|
|
16
|
+
should be changed to something like this:
|
|
17
|
+
```javascript
|
|
18
|
+
function execute() {}
|
|
19
|
+
Scheduler.scheduleJob(execute);
|
|
20
|
+
```
|
package/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { cancelJob, rescheduleJob, scheduledJobs, scheduleJob, gracefulShutdown} = require('./lib/schedule')
|
|
4
|
+
const { Invocation, RecurrenceRule, Range} = require('./lib/Invocation')
|
|
5
|
+
const { Job } = require('./lib/Job')
|
|
6
|
+
|
|
7
|
+
module.exports = {
|
|
8
|
+
Job,
|
|
9
|
+
Invocation,
|
|
10
|
+
Range,
|
|
11
|
+
RecurrenceRule,
|
|
12
|
+
cancelJob,
|
|
13
|
+
rescheduleJob,
|
|
14
|
+
scheduledJobs,
|
|
15
|
+
scheduleJob,
|
|
16
|
+
gracefulShutdown,
|
|
17
|
+
}
|