cron 2.3.0 → 2.4.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 +76 -7
- package/LICENSE +0 -0
- package/README.md +28 -19
- package/lib/cron.js +2 -2
- package/lib/job.js +19 -18
- package/lib/time.js +39 -39
- 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,91 @@
|
|
|
1
|
-
## [v2.0.0
|
|
1
|
+
## [2.4.0](https://github.com/kelektiv/node-cron/compare/v2.3.0...v2.4.0) (2023-07-24)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### ✨ Features
|
|
5
|
+
|
|
6
|
+
* import type definitions from [@types](https://github.com/types)/cron ([d8a2f14](https://github.com/kelektiv/node-cron/commit/d8a2f140b59f063897dd20b7bb4dc7f599d2435b))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### 🐛 Bug Fixes
|
|
10
|
+
|
|
11
|
+
* don't start job in setTime if it wasn't running ([7e26c23](https://github.com/kelektiv/node-cron/commit/7e26c23e06277bfeb04525c71b67703392dbb8b2))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### 🛠 Builds
|
|
15
|
+
|
|
16
|
+
* **npm:** ship type definitions with releases ([0b663a8](https://github.com/kelektiv/node-cron/commit/0b663a8584f87cbec63042a4c217f43f38869fc4))
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### 🚨 Tests
|
|
20
|
+
|
|
21
|
+
* add test case for [#598](https://github.com/kelektiv/node-cron/issues/598) fix ([4322ef2](https://github.com/kelektiv/node-cron/commit/4322ef29fa8af201aed5cdf8b829d411311fe025))
|
|
22
|
+
* don't stop/start job before using setTime ([f0d5d3f](https://github.com/kelektiv/node-cron/commit/f0d5d3f32eddb8fd77b84438fe471fd374b34566))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### ⚙️ Continuous Integrations
|
|
26
|
+
|
|
27
|
+
* add support for beta & maintenance releases ([#677](https://github.com/kelektiv/node-cron/issues/677)) ([c6fc842](https://github.com/kelektiv/node-cron/commit/c6fc8429e905b38b05ba428e0df4a0fea273614a))
|
|
28
|
+
* setup conventional commits & release automation ([#673](https://github.com/kelektiv/node-cron/issues/673)) ([c6f39ff](https://github.com/kelektiv/node-cron/commit/c6f39ff384041b7f91566fc935a9b961d453dd14))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
### ♻️ Chores
|
|
32
|
+
|
|
33
|
+
* update default branch name ([#678](https://github.com/kelektiv/node-cron/issues/678)) ([7471e95](https://github.com/kelektiv/node-cron/commit/7471e95cb7433b4f29cfa68da0a652ec8cf630b6))
|
|
34
|
+
* wrap setTime tests in describe and move down ([31989e0](https://github.com/kelektiv/node-cron/commit/31989e06f939bf1e9dbc6c0b6fc62c0a7144b9eb))
|
|
35
|
+
|
|
36
|
+
## [v2.3.1](https://github.com/kelektiv/node-cron/compare/v2.3.0...v2.3.1) (2023-05-25)
|
|
37
|
+
|
|
38
|
+
### Added
|
|
39
|
+
|
|
40
|
+
- Logo!
|
|
41
|
+
- New test cases
|
|
42
|
+
|
|
43
|
+
### Fixed
|
|
44
|
+
|
|
45
|
+
- Linting issues
|
|
46
|
+
|
|
47
|
+
## [v2.3.0](https://github.com/kelektiv/node-cron/compare/v2.2.0...v2.3.0) (2023-03-14)
|
|
48
|
+
|
|
49
|
+
### Fixed
|
|
50
|
+
|
|
51
|
+
- Some small bugs
|
|
52
|
+
|
|
53
|
+
### Changed
|
|
54
|
+
|
|
55
|
+
- Refactored get next date function
|
|
56
|
+
|
|
57
|
+
## [v2.2.0](https://github.com/kelektiv/node-cron/compare/v2.1.0...v2.2.0) (2023-01-09)
|
|
58
|
+
|
|
59
|
+
### Changed
|
|
60
|
+
|
|
61
|
+
- Updated Luxon dependency
|
|
62
|
+
- Updated unit tests to be compatible with new Luxon version
|
|
63
|
+
|
|
64
|
+
## [v2.1.0](https://github.com/kelektiv/node-cron/compare/v2.0.0...v2.1.0) (2022-07-14)
|
|
65
|
+
|
|
66
|
+
### Changed
|
|
67
|
+
|
|
68
|
+
- "Maximum iterations" will direct the user to refer to a single canonical issue instead of creating a new one
|
|
69
|
+
|
|
70
|
+
## [v2.0.0](https://github.com/kelektiv/node-cron/compare/v1.8.2...v2.0.0) (2022-05-03)
|
|
2
71
|
- Upgrade vulnerable dependencies
|
|
3
72
|
- Move from moment.js to luxon (breaking change)
|
|
4
73
|
|
|
5
|
-
## [v1.8.2]
|
|
74
|
+
## [v1.8.2](https://github.com/kelektiv/node-cron/compare/v1.8.1...v1.8.2) (2020-01-24)
|
|
6
75
|
- Fix syntax parsing bug
|
|
7
76
|
|
|
8
|
-
## [v1.8.1]
|
|
77
|
+
## [v1.8.1](https://github.com/kelektiv/node-cron/compare/v1.8.0...v1.8.1) (2020-01-19)
|
|
9
78
|
- Revert TS definition defaulting to DefinitelyTyped definitions.
|
|
10
79
|
|
|
11
|
-
## [v1.8.0]
|
|
80
|
+
## [v1.8.0](https://github.com/kelektiv/node-cron/compare/v1.7.1...v1.8.0) (2020-01-19)
|
|
12
81
|
- GH-454 - Range upper limit should default to highest value when step is provided by Noah May <noahmouse2011@gmail.com> in d36dc9581f9f68580a2016b368f8002a9f1e357d
|
|
13
82
|
|
|
14
|
-
## [v1.7.1]
|
|
83
|
+
## [v1.7.1](https://github.com/kelektiv/node-cron/compare/v1.7.0...v1.7.1) (2019-04-26)
|
|
15
84
|
- GH-416 - Fix issue where next execution time is incorrect in some cases in Naoya Inada <naoina@kuune.org> in c08522ff80b3987843e9930c307b76d5fe38b5dc
|
|
16
85
|
|
|
17
|
-
## [v1.7.0]
|
|
86
|
+
## [v1.7.0](https://github.com/kelektiv/node-cron/compare/v1.6.0...v1.7.0) (2019-03-19)
|
|
18
87
|
- GH-408 - DST issue by Shua Talansky <shua@bitbean.com> in 1e971fd6dfa6ba4b0469d99dd64e6c31189d17d3 and 849a2467d16216a9dfa818c57cc26be6b6d0899b
|
|
19
88
|
|
|
20
|
-
## [v1.6.0]
|
|
89
|
+
## [v1.6.0](https://github.com/kelektiv/node-cron/compare/v1.5.1...v1.6.0) (2018-11-15)
|
|
21
90
|
- 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
91
|
- 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,11 @@ 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
|
-
## Versions and Backwards compatibility breaks
|
|
30
|
+
## Versions and Backwards compatibility breaks
|
|
26
31
|
|
|
27
|
-
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 (
|
|
32
|
+
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).
|
|
28
33
|
|
|
29
|
-
## Usage (basic cron usage)
|
|
34
|
+
## Usage (basic cron usage)
|
|
30
35
|
|
|
31
36
|
```javascript
|
|
32
37
|
var CronJob = require('cron').CronJob;
|
|
@@ -39,15 +44,14 @@ var job = new CronJob(
|
|
|
39
44
|
true,
|
|
40
45
|
'America/Los_Angeles'
|
|
41
46
|
);
|
|
42
|
-
//
|
|
43
|
-
// job.start()
|
|
47
|
+
// job.start() - See note below when to use this
|
|
44
48
|
```
|
|
45
49
|
|
|
46
|
-
Note -
|
|
50
|
+
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
51
|
|
|
48
|
-
There are more examples available in this repository at: [/examples](https://github.com/kelektiv/node-cron/tree/
|
|
52
|
+
There are more examples available in this repository at: [/examples](https://github.com/kelektiv/node-cron/tree/main/examples)
|
|
49
53
|
|
|
50
|
-
## Available Cron patterns
|
|
54
|
+
## Available Cron patterns
|
|
51
55
|
|
|
52
56
|
```
|
|
53
57
|
Asterisks e.g. *
|
|
@@ -67,12 +71,13 @@ When specifying your cron values you'll need to make sure that your values fall
|
|
|
67
71
|
- Minutes: 0-59
|
|
68
72
|
- Hours: 0-23
|
|
69
73
|
- Day of Month: 1-31
|
|
70
|
-
- Months: 0-11 (Jan-Dec)
|
|
74
|
+
- Months: 0-11 (Jan-Dec) <-- currently different from Unix `cron`!
|
|
71
75
|
- Day of Week: 0-6 (Sun-Sat)
|
|
72
76
|
|
|
73
77
|
## Gotchas
|
|
74
78
|
|
|
75
|
-
-
|
|
79
|
+
- 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`.
|
|
80
|
+
- 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
81
|
- 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
82
|
|
|
78
83
|
## API
|
|
@@ -84,15 +89,15 @@ Parameter Based
|
|
|
84
89
|
- `sendAt` - tells you when a `CronTime` will be run.
|
|
85
90
|
- `timeout` - tells you when the next timeout is.
|
|
86
91
|
- `CronJob`
|
|
87
|
-
- `constructor(cronTime, onTick, onComplete, start,
|
|
92
|
+
- `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
93
|
- `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
94
|
- `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
95
|
- `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
96
|
- `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
|
|
97
|
+
- `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
98
|
- `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
99
|
- `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
|
|
100
|
+
- `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
101
|
- `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
102
|
- `start` - Runs your job.
|
|
98
103
|
- `stop` - Stops your job.
|
|
@@ -111,6 +116,10 @@ Join the [Discord server](https://discord.gg/yyKns29zch)! Here you can discuss i
|
|
|
111
116
|
|
|
112
117
|
## Contributing
|
|
113
118
|
|
|
119
|
+
### Looking for maintainers/contributors
|
|
120
|
+
|
|
121
|
+
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.
|
|
122
|
+
|
|
114
123
|
### Submitting Bugs/Issues
|
|
115
124
|
|
|
116
125
|
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 +128,8 @@ Because we can't magically know what you are doing to expose an issue, it is bes
|
|
|
119
128
|
|
|
120
129
|
### Acknowledgements
|
|
121
130
|
|
|
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
|
|
131
|
+
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
132
|
|
|
124
133
|
## License
|
|
125
134
|
|
|
126
|
-
MIT
|
|
135
|
+
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
|
@@ -70,7 +70,7 @@ function CronTime(luxon) {
|
|
|
70
70
|
this.source = source;
|
|
71
71
|
|
|
72
72
|
if (zone) {
|
|
73
|
-
const dt = luxon.DateTime.fromObject({}, { zone
|
|
73
|
+
const dt = luxon.DateTime.fromObject({}, { zone });
|
|
74
74
|
if (dt.invalid) {
|
|
75
75
|
throw new Error('Invalid timezone.');
|
|
76
76
|
}
|
|
@@ -82,8 +82,8 @@ function CronTime(luxon) {
|
|
|
82
82
|
this.utcOffset = utcOffset;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
|
|
86
|
-
TIME_UNITS.
|
|
85
|
+
const that = this;
|
|
86
|
+
TIME_UNITS.forEach(timeUnit => {
|
|
87
87
|
that[timeUnit] = {};
|
|
88
88
|
});
|
|
89
89
|
|
|
@@ -103,19 +103,19 @@ function CronTime(luxon) {
|
|
|
103
103
|
* Ensure that the syntax parsed correctly and correct the specified values if needed.
|
|
104
104
|
*/
|
|
105
105
|
_verifyParse: function () {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
106
|
+
const months = Object.keys(this.month);
|
|
107
|
+
const dom = Object.keys(this.dayOfMonth);
|
|
108
|
+
let ok = false;
|
|
109
109
|
|
|
110
110
|
/* if a dayOfMonth is not found in all months, we only need to fix the last
|
|
111
111
|
wrong month to prevent infinite loop */
|
|
112
|
-
|
|
113
|
-
for (
|
|
114
|
-
|
|
115
|
-
|
|
112
|
+
let lastWrongMonth = NaN;
|
|
113
|
+
for (let i = 0; i < months.length; i++) {
|
|
114
|
+
const m = months[i];
|
|
115
|
+
const con = MONTH_CONSTRAINTS[parseInt(m, 10)];
|
|
116
116
|
|
|
117
|
-
for (
|
|
118
|
-
|
|
117
|
+
for (let j = 0; j < dom.length; j++) {
|
|
118
|
+
const day = dom[j];
|
|
119
119
|
if (day <= con) {
|
|
120
120
|
ok = true;
|
|
121
121
|
}
|
|
@@ -130,12 +130,12 @@ function CronTime(luxon) {
|
|
|
130
130
|
|
|
131
131
|
// infinite loop detected (dayOfMonth is not found in all months)
|
|
132
132
|
if (!ok) {
|
|
133
|
-
|
|
134
|
-
for (
|
|
135
|
-
|
|
133
|
+
const notOkCon = MONTH_CONSTRAINTS[parseInt(lastWrongMonth, 10)];
|
|
134
|
+
for (let k = 0; k < dom.length; k++) {
|
|
135
|
+
const notOkDay = dom[k];
|
|
136
136
|
if (notOkDay > notOkCon) {
|
|
137
137
|
delete this.dayOfMonth[notOkDay];
|
|
138
|
-
|
|
138
|
+
const fixedDay = Number(notOkDay) % notOkCon;
|
|
139
139
|
this.dayOfMonth[fixedDay] = true;
|
|
140
140
|
}
|
|
141
141
|
}
|
|
@@ -146,7 +146,7 @@ function CronTime(luxon) {
|
|
|
146
146
|
* Calculate the "next" scheduled time
|
|
147
147
|
*/
|
|
148
148
|
sendAt: function (i) {
|
|
149
|
-
|
|
149
|
+
let date = this.realDate ? this.source : luxon.DateTime.local();
|
|
150
150
|
if (this.zone) {
|
|
151
151
|
date = date.setZone(this.zone);
|
|
152
152
|
}
|
|
@@ -185,7 +185,7 @@ function CronTime(luxon) {
|
|
|
185
185
|
return this._getNextDateFrom(date);
|
|
186
186
|
} else {
|
|
187
187
|
// return the next schedule times
|
|
188
|
-
|
|
188
|
+
const dates = [];
|
|
189
189
|
for (; i > 0; i--) {
|
|
190
190
|
date = this._getNextDateFrom(date);
|
|
191
191
|
dates.push(date);
|
|
@@ -213,7 +213,7 @@ function CronTime(luxon) {
|
|
|
213
213
|
* Json representation of the parsed cron syntax.
|
|
214
214
|
*/
|
|
215
215
|
toJSON: function () {
|
|
216
|
-
|
|
216
|
+
const self = this;
|
|
217
217
|
return TIME_UNITS.map(function (timeName) {
|
|
218
218
|
return self._wcOrAll(timeName);
|
|
219
219
|
});
|
|
@@ -244,8 +244,8 @@ function CronTime(luxon) {
|
|
|
244
244
|
if (start instanceof Date) {
|
|
245
245
|
start = luxon.DateTime.fromJSDate(start);
|
|
246
246
|
}
|
|
247
|
-
|
|
248
|
-
|
|
247
|
+
let date = start;
|
|
248
|
+
const firstDate = start.toMillis();
|
|
249
249
|
if (zone) {
|
|
250
250
|
date = date.setZone(zone);
|
|
251
251
|
}
|
|
@@ -262,10 +262,10 @@ function CronTime(luxon) {
|
|
|
262
262
|
// it shouldn't take more than 5 seconds to find the next execution time
|
|
263
263
|
// being very generous with this. Throw error if it takes too long to find the next time to protect from
|
|
264
264
|
// infinite loop.
|
|
265
|
-
|
|
265
|
+
const timeout = Date.now() + 5000;
|
|
266
266
|
// determine next date
|
|
267
267
|
while (true) {
|
|
268
|
-
|
|
268
|
+
const diff = date - start;
|
|
269
269
|
|
|
270
270
|
// hard stop if the current date is after the expected execution
|
|
271
271
|
if (Date.now() > timeout) {
|
|
@@ -661,8 +661,8 @@ function CronTime(luxon) {
|
|
|
661
661
|
return '*';
|
|
662
662
|
}
|
|
663
663
|
|
|
664
|
-
|
|
665
|
-
for (
|
|
664
|
+
const all = [];
|
|
665
|
+
for (const time in this[type]) {
|
|
666
666
|
all.push(time);
|
|
667
667
|
}
|
|
668
668
|
|
|
@@ -670,9 +670,9 @@ function CronTime(luxon) {
|
|
|
670
670
|
},
|
|
671
671
|
|
|
672
672
|
_hasAll: function (type) {
|
|
673
|
-
|
|
673
|
+
const constraints = CONSTRAINTS[TIME_UNITS.indexOf(type)];
|
|
674
674
|
|
|
675
|
-
for (
|
|
675
|
+
for (let i = constraints[0], n = constraints[1]; i < n; i++) {
|
|
676
676
|
if (!(i in this[type])) {
|
|
677
677
|
return false;
|
|
678
678
|
}
|
|
@@ -707,7 +707,7 @@ function CronTime(luxon) {
|
|
|
707
707
|
throw new Error(`Unknown alias: ${alias}`);
|
|
708
708
|
});
|
|
709
709
|
|
|
710
|
-
|
|
710
|
+
const units = source.trim().split(/\s+/);
|
|
711
711
|
|
|
712
712
|
// seconds are optional
|
|
713
713
|
if (units.length < TIME_UNITS_LEN - 1) {
|
|
@@ -718,12 +718,12 @@ function CronTime(luxon) {
|
|
|
718
718
|
throw new Error('Too many fields');
|
|
719
719
|
}
|
|
720
720
|
|
|
721
|
-
|
|
722
|
-
for (
|
|
721
|
+
const unitsLen = units.length;
|
|
722
|
+
for (let i = 0; i < TIME_UNITS_LEN; i++) {
|
|
723
723
|
// If the split source string doesn't contain all digits,
|
|
724
724
|
// assume defaults for first n missing digits.
|
|
725
725
|
// This adds support for 5-digit standard cron syntax
|
|
726
|
-
|
|
726
|
+
const cur = units[i - (TIME_UNITS_LEN - unitsLen)] || PARSE_DEFAULTS[i];
|
|
727
727
|
this._parseField(cur, TIME_UNITS[i], CONSTRAINTS[i]);
|
|
728
728
|
}
|
|
729
729
|
},
|
|
@@ -739,14 +739,14 @@ function CronTime(luxon) {
|
|
|
739
739
|
* - 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
740
|
*/
|
|
741
741
|
_parseField: function (value, type, constraints) {
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
742
|
+
const typeObj = this[type];
|
|
743
|
+
let pointer;
|
|
744
|
+
const low = constraints[0];
|
|
745
|
+
const high = constraints[1];
|
|
746
746
|
|
|
747
|
-
|
|
747
|
+
const fields = value.split(',');
|
|
748
748
|
fields.forEach(field => {
|
|
749
|
-
|
|
749
|
+
const wildcardIndex = field.indexOf('*');
|
|
750
750
|
if (wildcardIndex !== -1 && wildcardIndex !== 0) {
|
|
751
751
|
throw new Error(
|
|
752
752
|
`Field (${field}) has an invalid wildcard expression`
|
|
@@ -758,9 +758,9 @@ function CronTime(luxon) {
|
|
|
758
758
|
value = value.replace(RE_WILDCARDS, `${low}-${high}`);
|
|
759
759
|
|
|
760
760
|
// commas separate information, so split based on those
|
|
761
|
-
|
|
761
|
+
const allRanges = value.split(',');
|
|
762
762
|
|
|
763
|
-
for (
|
|
763
|
+
for (let i = 0; i < allRanges.length; i++) {
|
|
764
764
|
if (allRanges[i].match(RE_RANGE)) {
|
|
765
765
|
allRanges[i].replace(RE_RANGE, ($0, lower, upper, step) => {
|
|
766
766
|
lower = parseInt(lower, 10);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cron",
|
|
3
3
|
"description": "Cron jobs for your node",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.4.0",
|
|
5
5
|
"author": "Nick Campbell <nicholas.j.campbell@gmail.com> (https://github.com/ncb000gt)",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/kelektiv/node-cron/issues"
|
|
@@ -13,26 +13,39 @@
|
|
|
13
13
|
"main": "lib/cron",
|
|
14
14
|
"scripts": {
|
|
15
15
|
"lint": "eslint {lib,tests}/*.js",
|
|
16
|
-
"test": "jest --coverage"
|
|
16
|
+
"test": "jest --coverage",
|
|
17
|
+
"test:watch": "jest --watch --coverage",
|
|
18
|
+
"prepare": "husky install",
|
|
19
|
+
"release": "semantic-release"
|
|
17
20
|
},
|
|
21
|
+
"types": "types/index.d.ts",
|
|
18
22
|
"dependencies": {
|
|
19
23
|
"luxon": "^3.2.1"
|
|
20
24
|
},
|
|
21
25
|
"devDependencies": {
|
|
26
|
+
"@commitlint/cli": "~17.6.6",
|
|
27
|
+
"@insurgentlab/commitlint-config": "^18.1.0",
|
|
28
|
+
"@insurgentlab/conventional-changelog-preset": "~6.0.3",
|
|
29
|
+
"@semantic-release/changelog": "~6.0.x",
|
|
30
|
+
"@semantic-release/commit-analyzer": "~9.0.x",
|
|
31
|
+
"@semantic-release/git": "~10.0.x",
|
|
32
|
+
"@semantic-release/github": "~8.1.x",
|
|
33
|
+
"@semantic-release/npm": "~10.0.x",
|
|
34
|
+
"@semantic-release/release-notes-generator": "~11.0.x",
|
|
22
35
|
"chai": "~4.2.x",
|
|
23
|
-
"eslint": "~
|
|
24
|
-
"eslint-config-prettier": "^
|
|
25
|
-
"eslint-config-standard": "~
|
|
26
|
-
"eslint-
|
|
27
|
-
"eslint-plugin-
|
|
28
|
-
"eslint-plugin-
|
|
29
|
-
"eslint-plugin-
|
|
30
|
-
"eslint-plugin-
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"sinon": "^
|
|
36
|
+
"eslint": "~8.36.x",
|
|
37
|
+
"eslint-config-prettier": "^8.7.x",
|
|
38
|
+
"eslint-config-standard": "~17.0.x",
|
|
39
|
+
"eslint-plugin-import": "~2.27.x",
|
|
40
|
+
"eslint-plugin-jest": "~27.2.x",
|
|
41
|
+
"eslint-plugin-n": "~15.6.x",
|
|
42
|
+
"eslint-plugin-prettier": "~4.2.x",
|
|
43
|
+
"eslint-plugin-promise": "~6.1.x",
|
|
44
|
+
"husky": "^8.0.3",
|
|
45
|
+
"jest": "~29.5.x",
|
|
46
|
+
"prettier": "~2.8.x",
|
|
47
|
+
"semantic-release": "~21.0.x",
|
|
48
|
+
"sinon": "^15.0.x"
|
|
36
49
|
},
|
|
37
50
|
"keywords": [
|
|
38
51
|
"cron",
|
|
@@ -45,6 +58,7 @@
|
|
|
45
58
|
],
|
|
46
59
|
"license": "MIT",
|
|
47
60
|
"contributors": [
|
|
61
|
+
"Brandon der Blätter <https://interlucid.com/contact/> (https://github.com/intcreator)",
|
|
48
62
|
"Romain Beauxis <toots@rastageeks.org> (https://github.com/toots)",
|
|
49
63
|
"James Padolsey <> (https://github.com/jamespadolsey)",
|
|
50
64
|
"Finn Herpich <fh@three-heads.de> (https://github.com/ErrorProne)",
|
|
@@ -73,5 +87,12 @@
|
|
|
73
87
|
"lines": 80
|
|
74
88
|
}
|
|
75
89
|
}
|
|
76
|
-
}
|
|
90
|
+
},
|
|
91
|
+
"files": [
|
|
92
|
+
"lib",
|
|
93
|
+
"types",
|
|
94
|
+
"CHANGELOG.md",
|
|
95
|
+
"LICENSE",
|
|
96
|
+
"README.md"
|
|
97
|
+
]
|
|
77
98
|
}
|