cron 2.3.0 → 3.0.0-beta.1
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 +63 -7
- package/LICENSE +0 -0
- package/README.md +56 -25
- package/lib/cron.js +2 -2
- package/lib/job.js +19 -18
- package/lib/time.js +78 -65
- package/package.json +37 -16
- package/types/index.d.ts +181 -0
- package/.editorconfig +0 -5
- package/.eslintrc +0 -26
- package/.github/ISSUE_TEMPLATE/bug_issue.yaml +0 -26
- package/.github/ISSUE_TEMPLATE/feature.yaml +0 -31
- package/.github/workflows/nodejs.yml +0 -39
- package/.prettierrc +0 -7
- package/bower.json +0 -29
- package/examples/at_10_minutes.js +0 -9
- package/examples/at_midnight.js +0 -9
- package/examples/basic.js +0 -9
- package/examples/complex_expr.js +0 -9
- package/examples/every_10_minutes.js +0 -9
- package/examples/every_30_minutes_between_9_and_5.js +0 -9
- package/examples/get_next_runs.js +0 -24
- package/examples/in_the_past.js +0 -19
- package/examples/is_crontime_valid.js +0 -9
- package/examples/is_job_running.js +0 -10
- package/examples/long_running_on_tick.js +0 -20
- package/examples/mon_to_fri_at_11_30.js +0 -9
- package/examples/multiple_jobs.js +0 -15
- package/examples/object_param.js +0 -23
- package/examples/run_at_specific_date.js +0 -11
- package/examples/time_dom_syntax_with_tz.js +0 -13
- package/examples/utc_offset_syntax.js +0 -16
- package/tests/cron.test.js +0 -937
- package/tests/crontime.test.js +0 -648
package/CHANGELOG.md
CHANGED
|
@@ -1,22 +1,78 @@
|
|
|
1
|
-
## [
|
|
1
|
+
## [3.0.0-beta.1](https://github.com/kelektiv/node-cron/compare/v2.3.1...v3.0.0-beta.1) (2023-07-23)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### ⚠ Breaking changes
|
|
5
|
+
|
|
6
|
+
* UNIX standard alignments (#667)
|
|
7
|
+
|
|
8
|
+
### ✨ Features
|
|
9
|
+
|
|
10
|
+
* UNIX standard alignments ([#667](https://github.com/kelektiv/node-cron/issues/667)) ([96ef954](https://github.com/kelektiv/node-cron/commit/96ef954998f9c109cc2a969296568b1b56bbbd79))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### ⚙️ Continuous Integrations
|
|
14
|
+
|
|
15
|
+
* add support for beta & maintenance releases ([#677](https://github.com/kelektiv/node-cron/issues/677)) ([c6fc842](https://github.com/kelektiv/node-cron/commit/c6fc8429e905b38b05ba428e0df4a0fea273614a))
|
|
16
|
+
* setup conventional commits & release automation ([#673](https://github.com/kelektiv/node-cron/issues/673)) ([c6f39ff](https://github.com/kelektiv/node-cron/commit/c6f39ff384041b7f91566fc935a9b961d453dd14))
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### ♻️ Chores
|
|
20
|
+
|
|
21
|
+
* update default branch name ([#678](https://github.com/kelektiv/node-cron/issues/678)) ([7471e95](https://github.com/kelektiv/node-cron/commit/7471e95cb7433b4f29cfa68da0a652ec8cf630b6))
|
|
22
|
+
|
|
23
|
+
## [v2.3.1](https://github.com/kelektiv/node-cron/compare/v2.3.0...v2.3.1) (2023-05-25)
|
|
24
|
+
|
|
25
|
+
### Added
|
|
26
|
+
|
|
27
|
+
- Logo!
|
|
28
|
+
- New test cases
|
|
29
|
+
|
|
30
|
+
### Fixed
|
|
31
|
+
|
|
32
|
+
- Linting issues
|
|
33
|
+
|
|
34
|
+
## [v2.3.0](https://github.com/kelektiv/node-cron/compare/v2.2.0...v2.3.0) (2023-03-14)
|
|
35
|
+
|
|
36
|
+
### Fixed
|
|
37
|
+
|
|
38
|
+
- Some small bugs
|
|
39
|
+
|
|
40
|
+
### Changed
|
|
41
|
+
|
|
42
|
+
- Refactored get next date function
|
|
43
|
+
|
|
44
|
+
## [v2.2.0](https://github.com/kelektiv/node-cron/compare/v2.1.0...v2.2.0) (2023-01-09)
|
|
45
|
+
|
|
46
|
+
### Changed
|
|
47
|
+
|
|
48
|
+
- Updated Luxon dependency
|
|
49
|
+
- Updated unit tests to be compatible with new Luxon version
|
|
50
|
+
|
|
51
|
+
## [v2.1.0](https://github.com/kelektiv/node-cron/compare/v2.0.0...v2.1.0) (2022-07-14)
|
|
52
|
+
|
|
53
|
+
### Changed
|
|
54
|
+
|
|
55
|
+
- "Maximum iterations" will direct the user to refer to a single canonical issue instead of creating a new one
|
|
56
|
+
|
|
57
|
+
## [v2.0.0](https://github.com/kelektiv/node-cron/compare/v1.8.2...v2.0.0) (2022-05-03)
|
|
2
58
|
- Upgrade vulnerable dependencies
|
|
3
59
|
- Move from moment.js to luxon (breaking change)
|
|
4
60
|
|
|
5
|
-
## [v1.8.2]
|
|
61
|
+
## [v1.8.2](https://github.com/kelektiv/node-cron/compare/v1.8.1...v1.8.2) (2020-01-24)
|
|
6
62
|
- Fix syntax parsing bug
|
|
7
63
|
|
|
8
|
-
## [v1.8.1]
|
|
64
|
+
## [v1.8.1](https://github.com/kelektiv/node-cron/compare/v1.8.0...v1.8.1) (2020-01-19)
|
|
9
65
|
- Revert TS definition defaulting to DefinitelyTyped definitions.
|
|
10
66
|
|
|
11
|
-
## [v1.8.0]
|
|
67
|
+
## [v1.8.0](https://github.com/kelektiv/node-cron/compare/v1.7.1...v1.8.0) (2020-01-19)
|
|
12
68
|
- GH-454 - Range upper limit should default to highest value when step is provided by Noah May <noahmouse2011@gmail.com> in d36dc9581f9f68580a2016b368f8002a9f1e357d
|
|
13
69
|
|
|
14
|
-
## [v1.7.1]
|
|
70
|
+
## [v1.7.1](https://github.com/kelektiv/node-cron/compare/v1.7.0...v1.7.1) (2019-04-26)
|
|
15
71
|
- GH-416 - Fix issue where next execution time is incorrect in some cases in Naoya Inada <naoina@kuune.org> in c08522ff80b3987843e9930c307b76d5fe38b5dc
|
|
16
72
|
|
|
17
|
-
## [v1.7.0]
|
|
73
|
+
## [v1.7.0](https://github.com/kelektiv/node-cron/compare/v1.6.0...v1.7.0) (2019-03-19)
|
|
18
74
|
- GH-408 - DST issue by Shua Talansky <shua@bitbean.com> in 1e971fd6dfa6ba4b0469d99dd64e6c31189d17d3 and 849a2467d16216a9dfa818c57cc26be6b6d0899b
|
|
19
75
|
|
|
20
|
-
## [v1.6.0]
|
|
76
|
+
## [v1.6.0](https://github.com/kelektiv/node-cron/compare/v1.5.1...v1.6.0) (2018-11-15)
|
|
21
77
|
- GH-393, GH-394 - Remove hard limit on max iters in favor of a timeout by Nick Campbell <nicholas.j.campbell@gmail.com> in 57632b0c06c56e82f40b740b8d7986be43842735
|
|
22
78
|
- GH-390 - better handling of real dates which are in the past by Nick Campbell <nicholas.j.campbell@gmail.com> in 7cbcc984aea6ec063e38829f68eb9bc0dfb1c775
|
package/LICENSE
CHANGED
|
File without changes
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="logo.svg" alt="Node Cron Alarm Clock Star Logo" width="100" />
|
|
3
|
+
</p>
|
|
4
4
|
|
|
5
5
|
# node-cron
|
|
6
6
|
|
|
@@ -14,7 +14,12 @@ This project is looking for help! If you're interested in helping with the proje
|
|
|
14
14
|
[](https://badgen.net/bundlephobia/minzip/cron)
|
|
15
15
|
[](https://badgen.net/npm/dm/cron)
|
|
16
16
|
|
|
17
|
-
Cron is a tool that allows you to execute _something_ on a schedule. This is typically done using the cron syntax. We allow you to
|
|
17
|
+
Cron is a tool that allows you to execute _something_ on a schedule. This is typically done using the cron syntax. We allow you to:
|
|
18
|
+
|
|
19
|
+
- execute a function whenever your scheduled job triggers
|
|
20
|
+
- execute a job external to the javascript process using `child_process`
|
|
21
|
+
- use a Date object instead of cron syntax as the trigger for your callback
|
|
22
|
+
- use an additional slot for seconds (leaving it off will default to 0 and match the Unix behavior)
|
|
18
23
|
|
|
19
24
|
## Installation
|
|
20
25
|
|
|
@@ -22,11 +27,26 @@ Cron is a tool that allows you to execute _something_ on a schedule. This is typ
|
|
|
22
27
|
npm install cron
|
|
23
28
|
```
|
|
24
29
|
|
|
25
|
-
##
|
|
30
|
+
## Migrating from v2 to v3
|
|
31
|
+
|
|
32
|
+
In version 3 of this library, we aligned our format for the cron patterns with the UNIX format. See below for the changes you need to make when upgrading:
|
|
33
|
+
|
|
34
|
+
<details>
|
|
35
|
+
<summary>Migrating from v2 to v3</summary>
|
|
36
|
+
|
|
37
|
+
### Month & day-of-week indexing changes
|
|
38
|
+
|
|
39
|
+
**Month indexing went from `0-11` to `1-12`. So you need to increment all numeric months by 1.**
|
|
40
|
+
|
|
41
|
+
For day-of-week indexing, we only added support for `7` as Sunday, so you don't need to change anything !
|
|
42
|
+
|
|
43
|
+
</details>
|
|
26
44
|
|
|
27
|
-
|
|
45
|
+
## Versions and Backwards compatibility breaks
|
|
28
46
|
|
|
29
|
-
|
|
47
|
+
As goes with semver, breaking backwards compatibility should be explicit in the versioning of your library. As such, we'll upgrade the version of this module in accordance with breaking changes (We're not always great about doing it this way so if you notice that there are breaking changes that haven't been bumped appropriately please let us know).
|
|
48
|
+
|
|
49
|
+
## Usage (basic cron usage)
|
|
30
50
|
|
|
31
51
|
```javascript
|
|
32
52
|
var CronJob = require('cron').CronJob;
|
|
@@ -39,15 +59,14 @@ var job = new CronJob(
|
|
|
39
59
|
true,
|
|
40
60
|
'America/Los_Angeles'
|
|
41
61
|
);
|
|
42
|
-
//
|
|
43
|
-
// job.start()
|
|
62
|
+
// job.start() - See note below when to use this
|
|
44
63
|
```
|
|
45
64
|
|
|
46
|
-
Note -
|
|
65
|
+
Note - In the example above, the 4th parameter of `CronJob()` automatically starts the job on initialization. If this parameter is falsy or not provided, the job needs to be explicitly started using `job.start()`.
|
|
47
66
|
|
|
48
|
-
There are more examples available in this repository at: [/examples](https://github.com/kelektiv/node-cron/tree/
|
|
67
|
+
There are more examples available in this repository at: [/examples](https://github.com/kelektiv/node-cron/tree/main/examples)
|
|
49
68
|
|
|
50
|
-
## Available Cron patterns
|
|
69
|
+
## Available Cron patterns
|
|
51
70
|
|
|
52
71
|
```
|
|
53
72
|
Asterisks e.g. *
|
|
@@ -61,18 +80,26 @@ There are tools that help when constructing your cronjobs. You might find someth
|
|
|
61
80
|
|
|
62
81
|
### Cron Ranges
|
|
63
82
|
|
|
64
|
-
|
|
83
|
+
This library follows the [UNIX Cron format](https://man7.org/linux/man-pages/man5/crontab.5.html), with an added field at the beginning for second granularity.
|
|
84
|
+
|
|
85
|
+
```
|
|
86
|
+
field allowed values
|
|
87
|
+
----- --------------
|
|
88
|
+
second 0-59
|
|
89
|
+
minute 0-59
|
|
90
|
+
hour 0-23
|
|
91
|
+
day of month 1-31
|
|
92
|
+
month 1-12 (or names, see below)
|
|
93
|
+
day of week 0-7 (0 or 7 is Sunday, or use names)
|
|
94
|
+
```
|
|
65
95
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
- Hours: 0-23
|
|
69
|
-
- Day of Month: 1-31
|
|
70
|
-
- Months: 0-11 (Jan-Dec)
|
|
71
|
-
- Day of Week: 0-6 (Sun-Sat)
|
|
96
|
+
> Names can also be used for the 'month' and 'day of week' fields. Use the first three letters of the particular day or month (case does not matter). Ranges and lists of names are allowed.
|
|
97
|
+
> Examples: "mon,wed,fri", "jan-mar".
|
|
72
98
|
|
|
73
99
|
## Gotchas
|
|
74
100
|
|
|
75
|
-
-
|
|
101
|
+
- Months are indexed as 0-11 instead of 1-12. This is different from Unix `cron` and is planned to updated to match Unix `cron` in v3.0.0 of `node-cron`.
|
|
102
|
+
- Millisecond level granularity in JS `Date` or Luxon `DateTime` objects: Because computers take time to do things, there may be some delay in execution. This should be on the order of milliseconds. This module doesn't allow MS level granularity for the regular cron syntax, but _does_ allow you to specify a real date of execution in either a javascript `Date` object or a Luxon `DateTime` object. When this happens you may find that you aren't able to execute a job that _should_ run in the future like with `new Date().setMilliseconds(new Date().getMilliseconds() + 1)`. This is due to those cycles of execution above. This wont be the same for everyone because of compute speed. When we tried it locally we saw that somewhere around the 4-5 ms mark was where we got consistent ticks using real dates, but anything less than that would result in an exception. This could be really confusing. We could restrict the granularity for all dates to seconds, but felt that it wasn't a huge problem so long as you were made aware. If this becomes more of an issue, We can revisit it.
|
|
76
103
|
- Arrow Functions for `onTick`: Arrow functions get their `this` context from their parent scope. Thus, if you use them, you will not get the `this` context of the cronjob. You can read a little more in issue [GH-47](https://github.com/kelektiv/node-cron/issues/47#issuecomment-459762775)
|
|
77
104
|
|
|
78
105
|
## API
|
|
@@ -84,15 +111,15 @@ Parameter Based
|
|
|
84
111
|
- `sendAt` - tells you when a `CronTime` will be run.
|
|
85
112
|
- `timeout` - tells you when the next timeout is.
|
|
86
113
|
- `CronJob`
|
|
87
|
-
- `constructor(cronTime, onTick, onComplete, start,
|
|
114
|
+
- `constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout)` - Of note, the first parameter here can be a JSON object that has the below names and associated types (see examples above).
|
|
88
115
|
- `cronTime` - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) object.
|
|
89
116
|
- `onTick` - [REQUIRED] - The function to fire at the specified time. If an `onComplete` callback was provided, `onTick` will receive it as an argument. `onTick` may call `onComplete` when it has finished its work.
|
|
90
117
|
- `onComplete` - [OPTIONAL] - A function that will fire when the job is stopped with `job.stop()`, and may also be called by `onTick` at the end of each run.
|
|
91
118
|
- `start` - [OPTIONAL] - Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at default you will need to call `job.start()` in order to start the job (assuming `job` is the variable you set the cronjob to). This does not immediately fire your `onTick` function, it just gives you more control over the behavior of your jobs.
|
|
92
|
-
- `timeZone` - [OPTIONAL] - Specify the
|
|
119
|
+
- `timeZone` - [OPTIONAL] - Specify the time zone for the execution. This will modify the actual time relative to your time zone. If the time zone is invalid, an error is thrown. By default (if this is omitted) the local time zone will be used. You can check all time zones available at [Moment Timezone Website](http://momentjs.com/timezone/). Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
|
|
93
120
|
- `context` - [OPTIONAL] - The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to call `this.stop()`. However, if you change this you'll have access to the functions and values within your context object.
|
|
94
121
|
- `runOnInit` - [OPTIONAL] - This will immediately fire your `onTick` function as soon as the requisite initialization has happened. This option is set to `false` by default for backwards compatibility.
|
|
95
|
-
- `utcOffset` - [OPTIONAL] - This allows you to specify the offset of your
|
|
122
|
+
- `utcOffset` - [OPTIONAL] - This allows you to specify the offset of your time zone rather than using the `timeZone` param. This should be an integer amount representing the number of minutes offset (like `120` for +2 hours or `-90` for -1.5 hours) Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
|
|
96
123
|
- `unrefTimeout` - [OPTIONAL] - If you have code that keeps the event loop running and want to stop the node process when that finishes regardless of the state of your cronjob, you can do so making use of this parameter. This is off by default and cron will run as if it needs to control the event loop. For more information take a look at [timers#timers_timeout_unref](https://nodejs.org/api/timers.html#timers_timeout_unref) from the NodeJS docs.
|
|
97
124
|
- `start` - Runs your job.
|
|
98
125
|
- `stop` - Stops your job.
|
|
@@ -111,6 +138,10 @@ Join the [Discord server](https://discord.gg/yyKns29zch)! Here you can discuss i
|
|
|
111
138
|
|
|
112
139
|
## Contributing
|
|
113
140
|
|
|
141
|
+
### Looking for maintainers/contributors
|
|
142
|
+
|
|
143
|
+
This project is looking for help! If you're interested in helping with the project please reach out to me (ncb000gt) on [Twitter](https://twitter.com/ncb000gt). We'd love for it to continue on, but it needs a lot of attention. You can also join the [Discord server](https://discord.gg/yyKns29zch) to learn more about what needs to be done.
|
|
144
|
+
|
|
114
145
|
### Submitting Bugs/Issues
|
|
115
146
|
|
|
116
147
|
Before submitting a bug, please search the existing issues, [Discord](https://discord.gg/yyKns29zch) conversations, and the web to see if someone else has run into the same issue before.
|
|
@@ -119,8 +150,8 @@ Because we can't magically know what you are doing to expose an issue, it is bes
|
|
|
119
150
|
|
|
120
151
|
### Acknowledgements
|
|
121
152
|
|
|
122
|
-
This is a community effort project. In the truest sense, this project started as an open source project from [cron.js](http://github.com/padolsey/cron.js) and grew into something else. Other people have contributed code, time, and oversight to the project. At this point there are too many to name here so
|
|
153
|
+
This is a community effort project. In the truest sense, this project started as an open source project from [cron.js](http://github.com/padolsey/cron.js) and grew into something else. Other people have contributed code, time, and oversight to the project. At this point there are too many to name here so We'll just say thanks.
|
|
123
154
|
|
|
124
155
|
## License
|
|
125
156
|
|
|
126
|
-
MIT
|
|
157
|
+
MIT
|
package/lib/cron.js
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
root.Cron = factory(root.luxon);
|
|
8
8
|
}
|
|
9
9
|
})(this, function (luxon, childProcess) {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
const exports = {};
|
|
11
|
+
const spawn = childProcess && childProcess.spawn;
|
|
12
12
|
const CronTime = require('./time')(luxon);
|
|
13
13
|
const CronJob = require('./job')(CronTime, spawn);
|
|
14
14
|
|
package/lib/job.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
function CronJob(CronTime, spawn) {
|
|
2
2
|
function fnWrap(cmd) {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
let command;
|
|
4
|
+
let args;
|
|
5
5
|
|
|
6
6
|
switch (typeof cmd) {
|
|
7
7
|
case 'string':
|
|
@@ -14,7 +14,7 @@ function CronJob(CronTime, spawn) {
|
|
|
14
14
|
command = cmd && cmd.command;
|
|
15
15
|
if (command) {
|
|
16
16
|
args = cmd.args;
|
|
17
|
-
|
|
17
|
+
const options = cmd.options;
|
|
18
18
|
|
|
19
19
|
return spawn.bind(undefined, command, args, options);
|
|
20
20
|
}
|
|
@@ -35,9 +35,9 @@ function CronJob(CronTime, spawn) {
|
|
|
35
35
|
utcOffset,
|
|
36
36
|
unrefTimeout
|
|
37
37
|
) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
for (
|
|
38
|
+
let _cronTime = cronTime;
|
|
39
|
+
let argCount = 0;
|
|
40
|
+
for (let i = 0; i < arguments.length; i++) {
|
|
41
41
|
if (arguments[i] !== undefined) {
|
|
42
42
|
argCount++;
|
|
43
43
|
}
|
|
@@ -76,7 +76,7 @@ function CronJob(CronTime, spawn) {
|
|
|
76
76
|
return this;
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
|
|
79
|
+
const addCallback = function (callback) {
|
|
80
80
|
if (typeof callback === 'function') {
|
|
81
81
|
this._callbacks.push(callback);
|
|
82
82
|
}
|
|
@@ -88,17 +88,18 @@ function CronJob(CronTime, spawn) {
|
|
|
88
88
|
// crontime is an object...
|
|
89
89
|
throw new Error('time must be an instance of CronTime.');
|
|
90
90
|
}
|
|
91
|
+
const wasRunning = this.running;
|
|
91
92
|
this.stop();
|
|
92
93
|
this.cronTime = time;
|
|
93
|
-
this.start();
|
|
94
|
+
if (wasRunning) this.start();
|
|
94
95
|
};
|
|
95
96
|
|
|
96
97
|
CJ.prototype.nextDate = function () {
|
|
97
98
|
return this.cronTime.sendAt();
|
|
98
99
|
};
|
|
99
100
|
|
|
100
|
-
|
|
101
|
-
for (
|
|
101
|
+
const fireOnTick = function () {
|
|
102
|
+
for (let i = this._callbacks.length - 1; i >= 0; i--) {
|
|
102
103
|
this._callbacks[i].call(this.context, this.onComplete);
|
|
103
104
|
}
|
|
104
105
|
};
|
|
@@ -108,16 +109,16 @@ function CronJob(CronTime, spawn) {
|
|
|
108
109
|
return this.cronTime.sendAt(i);
|
|
109
110
|
};
|
|
110
111
|
|
|
111
|
-
|
|
112
|
+
const start = function () {
|
|
112
113
|
if (this.running) {
|
|
113
114
|
return;
|
|
114
115
|
}
|
|
115
116
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
117
|
+
const MAXDELAY = 2147483647; // The maximum number of milliseconds setTimeout will wait.
|
|
118
|
+
const self = this;
|
|
119
|
+
let timeout = this.cronTime.getTimeout();
|
|
120
|
+
let remaining = 0;
|
|
121
|
+
let startTime;
|
|
121
122
|
|
|
122
123
|
if (this.cronTime.realDate) {
|
|
123
124
|
this.runOnce = true;
|
|
@@ -134,10 +135,10 @@ function CronJob(CronTime, spawn) {
|
|
|
134
135
|
// The callback wrapper checks if it needs to sleep another period or not
|
|
135
136
|
// and does the real callback logic when it's time.
|
|
136
137
|
function callbackWrapper() {
|
|
137
|
-
|
|
138
|
+
const diff = startTime + timeout - Date.now();
|
|
138
139
|
|
|
139
140
|
if (diff > 0) {
|
|
140
|
-
|
|
141
|
+
let newTimeout = self.cronTime.getTimeout();
|
|
141
142
|
|
|
142
143
|
if (newTimeout > diff) {
|
|
143
144
|
newTimeout = diff;
|
package/lib/time.js
CHANGED
|
@@ -3,8 +3,8 @@ const CONSTRAINTS = [
|
|
|
3
3
|
[0, 59],
|
|
4
4
|
[0, 23],
|
|
5
5
|
[1, 31],
|
|
6
|
-
[
|
|
7
|
-
[0,
|
|
6
|
+
[1, 12],
|
|
7
|
+
[0, 7]
|
|
8
8
|
];
|
|
9
9
|
const MONTH_CONSTRAINTS = [
|
|
10
10
|
31,
|
|
@@ -22,18 +22,18 @@ const MONTH_CONSTRAINTS = [
|
|
|
22
22
|
];
|
|
23
23
|
const PARSE_DEFAULTS = ['0', '*', '*', '*', '*', '*'];
|
|
24
24
|
const ALIASES = {
|
|
25
|
-
jan:
|
|
26
|
-
feb:
|
|
27
|
-
mar:
|
|
28
|
-
apr:
|
|
29
|
-
may:
|
|
30
|
-
jun:
|
|
31
|
-
jul:
|
|
32
|
-
aug:
|
|
33
|
-
sep:
|
|
34
|
-
oct:
|
|
35
|
-
nov:
|
|
36
|
-
dec:
|
|
25
|
+
jan: 1,
|
|
26
|
+
feb: 2,
|
|
27
|
+
mar: 3,
|
|
28
|
+
apr: 4,
|
|
29
|
+
may: 5,
|
|
30
|
+
jun: 6,
|
|
31
|
+
jul: 7,
|
|
32
|
+
aug: 8,
|
|
33
|
+
sep: 9,
|
|
34
|
+
oct: 10,
|
|
35
|
+
nov: 11,
|
|
36
|
+
dec: 12,
|
|
37
37
|
sun: 0,
|
|
38
38
|
mon: 1,
|
|
39
39
|
tue: 2,
|
|
@@ -42,17 +42,18 @@ const ALIASES = {
|
|
|
42
42
|
fri: 5,
|
|
43
43
|
sat: 6
|
|
44
44
|
};
|
|
45
|
-
const
|
|
46
|
-
'second',
|
|
47
|
-
'minute',
|
|
48
|
-
'hour',
|
|
49
|
-
'dayOfMonth',
|
|
50
|
-
'month',
|
|
51
|
-
'dayOfWeek'
|
|
52
|
-
|
|
45
|
+
const TIME_UNITS_MAP = {
|
|
46
|
+
SECOND: 'second',
|
|
47
|
+
MINUTE: 'minute',
|
|
48
|
+
HOUR: 'hour',
|
|
49
|
+
DAY_OF_MONTH: 'dayOfMonth',
|
|
50
|
+
MONTH: 'month',
|
|
51
|
+
DAY_OF_WEEK: 'dayOfWeek'
|
|
52
|
+
};
|
|
53
|
+
const TIME_UNITS = Object.values(TIME_UNITS_MAP);
|
|
53
54
|
const TIME_UNITS_LEN = TIME_UNITS.length;
|
|
54
55
|
const PRESETS = {
|
|
55
|
-
'@yearly': '0 0 0 1
|
|
56
|
+
'@yearly': '0 0 0 1 1 *',
|
|
56
57
|
'@monthly': '0 0 0 1 * *',
|
|
57
58
|
'@weekly': '0 0 0 * * 0',
|
|
58
59
|
'@daily': '0 0 0 * * *',
|
|
@@ -70,7 +71,7 @@ function CronTime(luxon) {
|
|
|
70
71
|
this.source = source;
|
|
71
72
|
|
|
72
73
|
if (zone) {
|
|
73
|
-
const dt = luxon.DateTime.fromObject({}, { zone
|
|
74
|
+
const dt = luxon.DateTime.fromObject({}, { zone });
|
|
74
75
|
if (dt.invalid) {
|
|
75
76
|
throw new Error('Invalid timezone.');
|
|
76
77
|
}
|
|
@@ -82,8 +83,8 @@ function CronTime(luxon) {
|
|
|
82
83
|
this.utcOffset = utcOffset;
|
|
83
84
|
}
|
|
84
85
|
|
|
85
|
-
|
|
86
|
-
TIME_UNITS.
|
|
86
|
+
const that = this;
|
|
87
|
+
TIME_UNITS.forEach(timeUnit => {
|
|
87
88
|
that[timeUnit] = {};
|
|
88
89
|
});
|
|
89
90
|
|
|
@@ -103,19 +104,19 @@ function CronTime(luxon) {
|
|
|
103
104
|
* Ensure that the syntax parsed correctly and correct the specified values if needed.
|
|
104
105
|
*/
|
|
105
106
|
_verifyParse: function () {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
const months = Object.keys(this.month);
|
|
108
|
+
const dom = Object.keys(this.dayOfMonth);
|
|
109
|
+
let ok = false;
|
|
109
110
|
|
|
110
111
|
/* if a dayOfMonth is not found in all months, we only need to fix the last
|
|
111
112
|
wrong month to prevent infinite loop */
|
|
112
|
-
|
|
113
|
-
for (
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
let lastWrongMonth = NaN;
|
|
114
|
+
for (let i = 0; i < months.length; i++) {
|
|
115
|
+
const m = months[i];
|
|
116
|
+
const con = MONTH_CONSTRAINTS[parseInt(m, 10) - 1];
|
|
116
117
|
|
|
117
|
-
for (
|
|
118
|
-
|
|
118
|
+
for (let j = 0; j < dom.length; j++) {
|
|
119
|
+
const day = dom[j];
|
|
119
120
|
if (day <= con) {
|
|
120
121
|
ok = true;
|
|
121
122
|
}
|
|
@@ -130,12 +131,12 @@ function CronTime(luxon) {
|
|
|
130
131
|
|
|
131
132
|
// infinite loop detected (dayOfMonth is not found in all months)
|
|
132
133
|
if (!ok) {
|
|
133
|
-
|
|
134
|
-
for (
|
|
135
|
-
|
|
134
|
+
const notOkCon = MONTH_CONSTRAINTS[parseInt(lastWrongMonth, 10) - 1];
|
|
135
|
+
for (let k = 0; k < dom.length; k++) {
|
|
136
|
+
const notOkDay = dom[k];
|
|
136
137
|
if (notOkDay > notOkCon) {
|
|
137
138
|
delete this.dayOfMonth[notOkDay];
|
|
138
|
-
|
|
139
|
+
const fixedDay = Number(notOkDay) % notOkCon;
|
|
139
140
|
this.dayOfMonth[fixedDay] = true;
|
|
140
141
|
}
|
|
141
142
|
}
|
|
@@ -146,7 +147,7 @@ function CronTime(luxon) {
|
|
|
146
147
|
* Calculate the "next" scheduled time
|
|
147
148
|
*/
|
|
148
149
|
sendAt: function (i) {
|
|
149
|
-
|
|
150
|
+
let date = this.realDate ? this.source : luxon.DateTime.local();
|
|
150
151
|
if (this.zone) {
|
|
151
152
|
date = date.setZone(this.zone);
|
|
152
153
|
}
|
|
@@ -185,7 +186,7 @@ function CronTime(luxon) {
|
|
|
185
186
|
return this._getNextDateFrom(date);
|
|
186
187
|
} else {
|
|
187
188
|
// return the next schedule times
|
|
188
|
-
|
|
189
|
+
const dates = [];
|
|
189
190
|
for (; i > 0; i--) {
|
|
190
191
|
date = this._getNextDateFrom(date);
|
|
191
192
|
dates.push(date);
|
|
@@ -213,7 +214,7 @@ function CronTime(luxon) {
|
|
|
213
214
|
* Json representation of the parsed cron syntax.
|
|
214
215
|
*/
|
|
215
216
|
toJSON: function () {
|
|
216
|
-
|
|
217
|
+
const self = this;
|
|
217
218
|
return TIME_UNITS.map(function (timeName) {
|
|
218
219
|
return self._wcOrAll(timeName);
|
|
219
220
|
});
|
|
@@ -244,8 +245,8 @@ function CronTime(luxon) {
|
|
|
244
245
|
if (start instanceof Date) {
|
|
245
246
|
start = luxon.DateTime.fromJSDate(start);
|
|
246
247
|
}
|
|
247
|
-
|
|
248
|
-
|
|
248
|
+
let date = start;
|
|
249
|
+
const firstDate = start.toMillis();
|
|
249
250
|
if (zone) {
|
|
250
251
|
date = date.setZone(zone);
|
|
251
252
|
}
|
|
@@ -262,10 +263,10 @@ function CronTime(luxon) {
|
|
|
262
263
|
// it shouldn't take more than 5 seconds to find the next execution time
|
|
263
264
|
// being very generous with this. Throw error if it takes too long to find the next time to protect from
|
|
264
265
|
// infinite loop.
|
|
265
|
-
|
|
266
|
+
const timeout = Date.now() + 5000;
|
|
266
267
|
// determine next date
|
|
267
268
|
while (true) {
|
|
268
|
-
|
|
269
|
+
const diff = date - start;
|
|
269
270
|
|
|
270
271
|
// hard stop if the current date is after the expected execution
|
|
271
272
|
if (Date.now() > timeout) {
|
|
@@ -278,7 +279,7 @@ function CronTime(luxon) {
|
|
|
278
279
|
}
|
|
279
280
|
|
|
280
281
|
if (
|
|
281
|
-
!(date.month
|
|
282
|
+
!(date.month in this.month) &&
|
|
282
283
|
Object.keys(this.month).length !== 12
|
|
283
284
|
) {
|
|
284
285
|
date = date.plus({ months: 1 });
|
|
@@ -471,7 +472,7 @@ function CronTime(luxon) {
|
|
|
471
472
|
const beforeJumpingPoint = afterJumpingPoint.minus({ second: 1 });
|
|
472
473
|
|
|
473
474
|
if (
|
|
474
|
-
date.month in this.month &&
|
|
475
|
+
date.month + 1 in this.month &&
|
|
475
476
|
date.day in this.dayOfMonth &&
|
|
476
477
|
date.getWeekDay() in this.dayOfWeek
|
|
477
478
|
) {
|
|
@@ -661,8 +662,8 @@ function CronTime(luxon) {
|
|
|
661
662
|
return '*';
|
|
662
663
|
}
|
|
663
664
|
|
|
664
|
-
|
|
665
|
-
for (
|
|
665
|
+
const all = [];
|
|
666
|
+
for (const time in this[type]) {
|
|
666
667
|
all.push(time);
|
|
667
668
|
}
|
|
668
669
|
|
|
@@ -670,9 +671,14 @@ function CronTime(luxon) {
|
|
|
670
671
|
},
|
|
671
672
|
|
|
672
673
|
_hasAll: function (type) {
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
674
|
+
const constraints = CONSTRAINTS[TIME_UNITS.indexOf(type)];
|
|
675
|
+
const low = constraints[0];
|
|
676
|
+
const high =
|
|
677
|
+
type === TIME_UNITS_MAP.DAY_OF_WEEK
|
|
678
|
+
? constraints[1] - 1
|
|
679
|
+
: constraints[1];
|
|
680
|
+
|
|
681
|
+
for (let i = low, n = high; i < n; i++) {
|
|
676
682
|
if (!(i in this[type])) {
|
|
677
683
|
return false;
|
|
678
684
|
}
|
|
@@ -707,7 +713,7 @@ function CronTime(luxon) {
|
|
|
707
713
|
throw new Error(`Unknown alias: ${alias}`);
|
|
708
714
|
});
|
|
709
715
|
|
|
710
|
-
|
|
716
|
+
const units = source.trim().split(/\s+/);
|
|
711
717
|
|
|
712
718
|
// seconds are optional
|
|
713
719
|
if (units.length < TIME_UNITS_LEN - 1) {
|
|
@@ -718,12 +724,12 @@ function CronTime(luxon) {
|
|
|
718
724
|
throw new Error('Too many fields');
|
|
719
725
|
}
|
|
720
726
|
|
|
721
|
-
|
|
722
|
-
for (
|
|
727
|
+
const unitsLen = units.length;
|
|
728
|
+
for (let i = 0; i < TIME_UNITS_LEN; i++) {
|
|
723
729
|
// If the split source string doesn't contain all digits,
|
|
724
730
|
// assume defaults for first n missing digits.
|
|
725
731
|
// This adds support for 5-digit standard cron syntax
|
|
726
|
-
|
|
732
|
+
const cur = units[i - (TIME_UNITS_LEN - unitsLen)] || PARSE_DEFAULTS[i];
|
|
727
733
|
this._parseField(cur, TIME_UNITS[i], CONSTRAINTS[i]);
|
|
728
734
|
}
|
|
729
735
|
},
|
|
@@ -739,14 +745,14 @@ function CronTime(luxon) {
|
|
|
739
745
|
* - Starting with the lower bounds of the range iterate by step up to the upper bounds and toggle the CronTime field value flag on.
|
|
740
746
|
*/
|
|
741
747
|
_parseField: function (value, type, constraints) {
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
748
|
+
const typeObj = this[type];
|
|
749
|
+
let pointer;
|
|
750
|
+
const low = constraints[0];
|
|
751
|
+
const high = constraints[1];
|
|
746
752
|
|
|
747
|
-
|
|
753
|
+
const fields = value.split(',');
|
|
748
754
|
fields.forEach(field => {
|
|
749
|
-
|
|
755
|
+
const wildcardIndex = field.indexOf('*');
|
|
750
756
|
if (wildcardIndex !== -1 && wildcardIndex !== 0) {
|
|
751
757
|
throw new Error(
|
|
752
758
|
`Field (${field}) has an invalid wildcard expression`
|
|
@@ -758,9 +764,9 @@ function CronTime(luxon) {
|
|
|
758
764
|
value = value.replace(RE_WILDCARDS, `${low}-${high}`);
|
|
759
765
|
|
|
760
766
|
// commas separate information, so split based on those
|
|
761
|
-
|
|
767
|
+
const allRanges = value.split(',');
|
|
762
768
|
|
|
763
|
-
for (
|
|
769
|
+
for (let i = 0; i < allRanges.length; i++) {
|
|
764
770
|
if (allRanges[i].match(RE_RANGE)) {
|
|
765
771
|
allRanges[i].replace(RE_RANGE, ($0, lower, upper, step) => {
|
|
766
772
|
lower = parseInt(lower, 10);
|
|
@@ -803,6 +809,13 @@ function CronTime(luxon) {
|
|
|
803
809
|
typeObj[pointer] = true; // mutates the field objects values inside CronTime
|
|
804
810
|
pointer += step;
|
|
805
811
|
} while (pointer <= upper);
|
|
812
|
+
|
|
813
|
+
// merge day 7 into day 0 (both Sunday), and remove day 7
|
|
814
|
+
// since we work with day-of-week 0-6 under the hood
|
|
815
|
+
if (type === 'dayOfWeek') {
|
|
816
|
+
if (!typeObj[0] && !!typeObj[7]) typeObj[0] = typeObj[7];
|
|
817
|
+
delete typeObj[7];
|
|
818
|
+
}
|
|
806
819
|
});
|
|
807
820
|
} else {
|
|
808
821
|
throw new Error(`Field (${type}) cannot be parsed`);
|