cron 3.0.0-beta.4 → 3.0.0-beta.6
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 +29 -1
- package/README.md +66 -52
- package/dist/constants.d.ts +74 -0
- package/dist/constants.js +78 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +13 -0
- package/dist/job.d.ts +24 -0
- package/dist/job.js +145 -0
- package/dist/time.d.ts +31 -0
- package/dist/time.js +450 -0
- package/dist/types/cron.types.d.ts +38 -0
- package/dist/types/cron.types.js +3 -0
- package/dist/types/utils.d.ts +2 -0
- package/dist/types/utils.js +3 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +8 -0
- package/package.json +16 -27
- package/lib/cron.js +0 -55
- package/lib/job.js +0 -215
- package/lib/time.js +0 -834
- package/types/index.d.ts +0 -181
- package/types/index.test-d.ts +0 -85
package/CHANGELOG.md
CHANGED
|
@@ -1,8 +1,36 @@
|
|
|
1
|
-
## [3.0.0-beta.
|
|
1
|
+
## [3.0.0-beta.6](https://github.com/kelektiv/node-cron/compare/v3.0.0-beta.5...v3.0.0-beta.6) (2023-09-26)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### ⚠ Breaking changes
|
|
5
|
+
|
|
6
|
+
* removed `cron.job()` method in favor of `new CronJob(...args)` /
|
|
7
|
+
`CronJob.from(argsObject)`
|
|
8
|
+
* removed `cron.time()` method in favor of `new CronTime()`
|
|
9
|
+
* `CronJob`: constructor no longer accepts an object as its first and
|
|
10
|
+
only params. Use `CronJob.from(argsObject)` instead.
|
|
11
|
+
* `CronJob`: callbacks are now called in the order they were registered
|
|
12
|
+
|
|
13
|
+
### 📦 Code Refactoring
|
|
14
|
+
|
|
15
|
+
* migrate to TypeScript ([#694](https://github.com/kelektiv/node-cron/issues/694)) ([97a65e1](https://github.com/kelektiv/node-cron/commit/97a65e109d473cb037e2fb4690c18ac897d5316a))
|
|
16
|
+
|
|
17
|
+
## [3.0.0-beta.5](https://github.com/kelektiv/node-cron/compare/v3.0.0-beta.4...v3.0.0-beta.5) (2023-09-25)
|
|
2
18
|
|
|
3
19
|
|
|
4
20
|
### 🐛 Bug Fixes
|
|
5
21
|
|
|
22
|
+
* added fractional offset support ([#685](https://github.com/kelektiv/node-cron/issues/685)) ([ce78478](https://github.com/kelektiv/node-cron/commit/ce784784575b65bd75b8b1a4adda3d8fd42fe1c0))
|
|
23
|
+
|
|
24
|
+
## [2.4.4](https://github.com/kelektiv/node-cron/compare/v2.4.3...v2.4.4) (2023-09-25)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### 🐛 Bug Fixes
|
|
28
|
+
|
|
29
|
+
* added fractional offset support ([#685](https://github.com/kelektiv/node-cron/issues/685)) ([ce78478](https://github.com/kelektiv/node-cron/commit/ce784784575b65bd75b8b1a4adda3d8fd42fe1c0))
|
|
30
|
+
|
|
31
|
+
## [3.0.0-beta.4](https://github.com/kelektiv/node-cron/compare/v3.0.0-beta.3...v3.0.0-beta.4) (2023-09-10)
|
|
32
|
+
|
|
33
|
+
|
|
6
34
|
* **deps:** update dependency luxon to v3.3.0 & add [@types](https://github.com/types)/luxon ([#689](https://github.com/kelektiv/node-cron/issues/689)) ([c95a449](https://github.com/kelektiv/node-cron/commit/c95a449121e440b82d391fc11f8dc148748f93ec)), closes [#688](https://github.com/kelektiv/node-cron/issues/688)
|
|
7
35
|
* fix range parsing when upper limit = 0 ([#687](https://github.com/kelektiv/node-cron/issues/687)) ([d96746f](https://github.com/kelektiv/node-cron/commit/d96746f7b8f357e565d1fad48c9f70d3d646da64))
|
|
8
36
|
|
package/README.md
CHANGED
|
@@ -12,59 +12,70 @@
|
|
|
12
12
|
[](https://snyk.io/test/github/kelektiv/node-cron)
|
|
13
13
|
[](https://badgen.net/bundlephobia/min/cron)
|
|
14
14
|
[](https://badgen.net/bundlephobia/minzip/cron)
|
|
15
|
-
[](https://badgen.net/npm/dm/cron)
|
|
15
|
+
[](https://badgen.net/npm/dm/cron)
|
|
16
16
|
|
|
17
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
18
|
|
|
19
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
|
|
20
|
+
- execute a job external to the javascript process (like a system command) using `child_process`
|
|
21
|
+
- use a Date or Luxon DateTime object instead of cron syntax as the trigger for your callback
|
|
22
22
|
- use an additional slot for seconds (leaving it off will default to 0 and match the Unix behavior)
|
|
23
23
|
|
|
24
|
-
## Installation
|
|
24
|
+
## Installation
|
|
25
25
|
|
|
26
26
|
```
|
|
27
|
-
npm install cron
|
|
27
|
+
npm install cron
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
## Migrating from v2 to v3
|
|
30
|
+
## Migrating from v2 to v3
|
|
31
31
|
|
|
32
|
-
In version 3 of this library, we
|
|
32
|
+
In version 3 of this library, we migrated to TypeScript, aligned our cron patterns format with the UNIX standard, and released some other breaking changes. See below for the changes you need to make when upgrading:
|
|
33
33
|
|
|
34
34
|
<details>
|
|
35
35
|
<summary>Migrating from v2 to v3</summary>
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
**Month indexing went from `0-11` to `1-12`. So you need to increment all numeric months by 1.**
|
|
37
|
+
### Month & day-of-week indexing changes
|
|
40
38
|
|
|
41
|
-
|
|
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
|
+
### CronJob changes
|
|
44
|
+
|
|
45
|
+
- **constructor no longer accepts an object as its first and only params. Use `CronJob.from(argsObject)` instead.**
|
|
46
|
+
- **callbacks are now called in the order they were registered.**
|
|
47
|
+
|
|
48
|
+
### Removed methods
|
|
49
|
+
|
|
50
|
+
- **removed `job()` method in favor of `new CronJob(...args)` / `CronJob.from(argsObject)`**
|
|
51
|
+
|
|
52
|
+
- **removed `time()` method in favor of `new CronTime()`**
|
|
42
53
|
|
|
43
54
|
</details>
|
|
44
55
|
|
|
45
56
|
## Versions and Backwards compatibility breaks
|
|
46
57
|
|
|
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).
|
|
58
|
+
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
59
|
|
|
49
60
|
## Usage (basic cron usage)
|
|
50
61
|
|
|
51
62
|
```javascript
|
|
52
63
|
var CronJob = require('cron').CronJob;
|
|
53
64
|
var job = new CronJob(
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
65
|
+
'* * * * * *',
|
|
66
|
+
function () {
|
|
67
|
+
console.log('You will see this message every second');
|
|
68
|
+
},
|
|
69
|
+
null,
|
|
70
|
+
true,
|
|
71
|
+
'America/Los_Angeles'
|
|
61
72
|
);
|
|
62
73
|
// job.start() - See note below when to use this
|
|
63
|
-
```
|
|
74
|
+
```
|
|
64
75
|
|
|
65
76
|
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()`.
|
|
66
77
|
|
|
67
|
-
There are more examples available in this repository at: [/examples](https://github.com/kelektiv/node-cron/tree/main/examples)
|
|
78
|
+
There are more examples available in this repository at: [/examples](https://github.com/kelektiv/node-cron/tree/main/examples)
|
|
68
79
|
|
|
69
80
|
## Available Cron patterns
|
|
70
81
|
|
|
@@ -74,11 +85,11 @@ Ranges e.g. 1-3,5
|
|
|
74
85
|
Steps e.g. */2
|
|
75
86
|
```
|
|
76
87
|
|
|
77
|
-
[Read up on cron patterns here](http://crontab.org). Note the examples in the link have five fields, and 1 minute as the finest granularity, but this library has six fields, with 1 second as the finest granularity.
|
|
88
|
+
[Read up on cron patterns here](http://crontab.org). Note the examples in the link have five fields, and 1 minute as the finest granularity, but this library has six fields, with 1 second as the finest granularity.
|
|
78
89
|
|
|
79
|
-
There are tools that help when constructing your cronjobs. You might find something like https://crontab.guru/ or https://cronjob.xyz/ helpful. But, note that these don't necessarily accept the exact same syntax as this library, for instance, it doesn't accept the `seconds` field, so keep that in mind.
|
|
90
|
+
There are tools that help when constructing your cronjobs. You might find something like https://crontab.guru/ or https://cronjob.xyz/ helpful. But, note that these don't necessarily accept the exact same syntax as this library, for instance, it doesn't accept the `seconds` field, so keep that in mind.
|
|
80
91
|
|
|
81
|
-
### Cron Ranges
|
|
92
|
+
### Cron Ranges
|
|
82
93
|
|
|
83
94
|
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
95
|
|
|
@@ -96,42 +107,43 @@ day of week 0-7 (0 or 7 is Sunday, or use names)
|
|
|
96
107
|
> 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
108
|
> Examples: "mon,wed,fri", "jan-mar".
|
|
98
109
|
|
|
99
|
-
## Gotchas
|
|
110
|
+
## Gotchas
|
|
100
111
|
|
|
101
112
|
- 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
113
|
- 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.
|
|
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)
|
|
114
|
+
- 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)
|
|
104
115
|
|
|
105
|
-
## API
|
|
116
|
+
## API
|
|
106
117
|
|
|
107
|
-
Parameter Based
|
|
118
|
+
Parameter Based
|
|
108
119
|
|
|
109
|
-
- `job` - shortcut to `new cron.CronJob()`.
|
|
110
|
-
- `time` - shortcut to `new cron.CronTime()`.
|
|
111
120
|
- `sendAt` - tells you when a `CronTime` will be run.
|
|
112
121
|
- `timeout` - tells you when the next timeout is.
|
|
113
122
|
- `CronJob`
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
123
|
+
- `constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout)`
|
|
124
|
+
- `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.
|
|
125
|
+
- `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.
|
|
126
|
+
- `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.
|
|
127
|
+
- `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.
|
|
128
|
+
- `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 the various time zones format accepted in the [Luxon documentation](https://github.com/moment/luxon/blob/master/docs/zones.md#specifying-a-zone). Note: This parameter supports minutes offsets, e.g. `UTC+5:30`. **Warning**: Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
|
|
129
|
+
- `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.
|
|
130
|
+
- `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.
|
|
131
|
+
- `utcOffset` - [OPTIONAL] - This allows you to specify the offset of your time zone rather than using the `timeZone` param. This should be an integer representing the number of minutes offset (like `120` for +2 hours or `-90` for -1.5 hours). **Warning**: Minutes offsets < 60 and >-60 will be treated as an offset in hours. This means a minute offset of `30` means an offset of +30 hours. Use the `timeZone` param in this case. This behavior [is planned to be removed in V3](https://github.com/kelektiv/node-cron/pull/685#issuecomment-1676417917). **Warning**: Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
|
|
132
|
+
- `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.
|
|
133
|
+
- `from` (static) - Create a new CronJob object providing arguments as an object. See argument names and descriptions above.
|
|
134
|
+
- `start` - Runs your job.
|
|
135
|
+
- `stop` - Stops your job.
|
|
136
|
+
- `setTime` - Stops and changes the time for the `CronJob`. Param must be a `CronTime`.
|
|
137
|
+
- `lastDate` - Tells you the last execution date.
|
|
138
|
+
- `nextDate` - Provides the next date that will trigger an `onTick`.
|
|
139
|
+
- `nextDates` - Provides an array of the next set of dates that will trigger an `onTick`.
|
|
140
|
+
- `fireOnTick` - Allows you to override the `onTick` calling behavior. This matters so only do this if you have a really good reason to do so.
|
|
141
|
+
- `addCallback` - Allows you to add `onTick` callbacks. Callbacks are run in the order they are registered.
|
|
132
142
|
- `CronTime`
|
|
133
|
-
|
|
134
|
-
|
|
143
|
+
- `constructor(time, zone, utcOffset)`
|
|
144
|
+
- `time` - [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.
|
|
145
|
+
- `zone` - [OPTIONAL] - Same as `timeZone` from `CronJob` parameters.
|
|
146
|
+
- `utcOffset` - [OPTIONAL] - Same as `utcOffset` from `CronJob` parameters.
|
|
135
147
|
|
|
136
148
|
## Community
|
|
137
149
|
|
|
@@ -151,8 +163,10 @@ Because we can't magically know what you are doing to expose an issue, it is bes
|
|
|
151
163
|
|
|
152
164
|
### Acknowledgements
|
|
153
165
|
|
|
154
|
-
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
|
|
166
|
+
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.
|
|
167
|
+
|
|
168
|
+
Special thanks to [Hiroki Horiuchi](https://github.com/horiuchi), [Lundarl Gholoi](https://github.com/winup) and [koooge](https://github.com/koooge) for their work on the [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped) typings before they were imported in v2.4.0.
|
|
155
169
|
|
|
156
|
-
## License
|
|
170
|
+
## License
|
|
157
171
|
|
|
158
172
|
MIT
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export declare const CONSTRAINTS: Readonly<{
|
|
2
|
+
readonly second: readonly [0, 59];
|
|
3
|
+
readonly minute: readonly [0, 59];
|
|
4
|
+
readonly hour: readonly [0, 23];
|
|
5
|
+
readonly dayOfMonth: readonly [1, 31];
|
|
6
|
+
readonly month: readonly [1, 12];
|
|
7
|
+
readonly dayOfWeek: readonly [0, 7];
|
|
8
|
+
}>;
|
|
9
|
+
export declare const MONTH_CONSTRAINTS: Readonly<{
|
|
10
|
+
readonly 1: 31;
|
|
11
|
+
readonly 2: 29;
|
|
12
|
+
readonly 3: 31;
|
|
13
|
+
readonly 4: 30;
|
|
14
|
+
readonly 5: 31;
|
|
15
|
+
readonly 6: 30;
|
|
16
|
+
readonly 7: 31;
|
|
17
|
+
readonly 8: 31;
|
|
18
|
+
readonly 9: 30;
|
|
19
|
+
readonly 10: 31;
|
|
20
|
+
readonly 11: 30;
|
|
21
|
+
readonly 12: 31;
|
|
22
|
+
}>;
|
|
23
|
+
export declare const PARSE_DEFAULTS: Readonly<{
|
|
24
|
+
readonly second: "0";
|
|
25
|
+
readonly minute: "*";
|
|
26
|
+
readonly hour: "*";
|
|
27
|
+
readonly dayOfMonth: "*";
|
|
28
|
+
readonly month: "*";
|
|
29
|
+
readonly dayOfWeek: "*";
|
|
30
|
+
}>;
|
|
31
|
+
export declare const ALIASES: Readonly<{
|
|
32
|
+
readonly jan: 1;
|
|
33
|
+
readonly feb: 2;
|
|
34
|
+
readonly mar: 3;
|
|
35
|
+
readonly apr: 4;
|
|
36
|
+
readonly may: 5;
|
|
37
|
+
readonly jun: 6;
|
|
38
|
+
readonly jul: 7;
|
|
39
|
+
readonly aug: 8;
|
|
40
|
+
readonly sep: 9;
|
|
41
|
+
readonly oct: 10;
|
|
42
|
+
readonly nov: 11;
|
|
43
|
+
readonly dec: 12;
|
|
44
|
+
readonly sun: 0;
|
|
45
|
+
readonly mon: 1;
|
|
46
|
+
readonly tue: 2;
|
|
47
|
+
readonly wed: 3;
|
|
48
|
+
readonly thu: 4;
|
|
49
|
+
readonly fri: 5;
|
|
50
|
+
readonly sat: 6;
|
|
51
|
+
}>;
|
|
52
|
+
export declare const TIME_UNITS_MAP: Readonly<{
|
|
53
|
+
readonly SECOND: "second";
|
|
54
|
+
readonly MINUTE: "minute";
|
|
55
|
+
readonly HOUR: "hour";
|
|
56
|
+
readonly DAY_OF_MONTH: "dayOfMonth";
|
|
57
|
+
readonly MONTH: "month";
|
|
58
|
+
readonly DAY_OF_WEEK: "dayOfWeek";
|
|
59
|
+
}>;
|
|
60
|
+
export declare const TIME_UNITS: ["second", "minute", "hour", "dayOfMonth", "month", "dayOfWeek"];
|
|
61
|
+
export declare const TIME_UNITS_LEN: number;
|
|
62
|
+
export declare const PRESETS: Readonly<{
|
|
63
|
+
readonly '@yearly': "0 0 0 1 1 *";
|
|
64
|
+
readonly '@monthly': "0 0 0 1 * *";
|
|
65
|
+
readonly '@weekly': "0 0 0 * * 0";
|
|
66
|
+
readonly '@daily': "0 0 0 * * *";
|
|
67
|
+
readonly '@hourly': "0 0 * * * *";
|
|
68
|
+
readonly '@minutely': "0 * * * * *";
|
|
69
|
+
readonly '@secondly': "* * * * * *";
|
|
70
|
+
readonly '@weekdays': "0 0 0 * * 1-5";
|
|
71
|
+
readonly '@weekends': "0 0 0 * * 0,6";
|
|
72
|
+
}>;
|
|
73
|
+
export declare const RE_WILDCARDS: RegExp;
|
|
74
|
+
export declare const RE_RANGE: RegExp;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RE_RANGE = exports.RE_WILDCARDS = exports.PRESETS = exports.TIME_UNITS_LEN = exports.TIME_UNITS = exports.TIME_UNITS_MAP = exports.ALIASES = exports.PARSE_DEFAULTS = exports.MONTH_CONSTRAINTS = exports.CONSTRAINTS = void 0;
|
|
4
|
+
exports.CONSTRAINTS = Object.freeze({
|
|
5
|
+
second: [0, 59],
|
|
6
|
+
minute: [0, 59],
|
|
7
|
+
hour: [0, 23],
|
|
8
|
+
dayOfMonth: [1, 31],
|
|
9
|
+
month: [1, 12],
|
|
10
|
+
dayOfWeek: [0, 7]
|
|
11
|
+
});
|
|
12
|
+
exports.MONTH_CONSTRAINTS = Object.freeze({
|
|
13
|
+
1: 31,
|
|
14
|
+
2: 29,
|
|
15
|
+
3: 31,
|
|
16
|
+
4: 30,
|
|
17
|
+
5: 31,
|
|
18
|
+
6: 30,
|
|
19
|
+
7: 31,
|
|
20
|
+
8: 31,
|
|
21
|
+
9: 30,
|
|
22
|
+
10: 31,
|
|
23
|
+
11: 30,
|
|
24
|
+
12: 31
|
|
25
|
+
});
|
|
26
|
+
exports.PARSE_DEFAULTS = Object.freeze({
|
|
27
|
+
second: '0',
|
|
28
|
+
minute: '*',
|
|
29
|
+
hour: '*',
|
|
30
|
+
dayOfMonth: '*',
|
|
31
|
+
month: '*',
|
|
32
|
+
dayOfWeek: '*'
|
|
33
|
+
});
|
|
34
|
+
exports.ALIASES = Object.freeze({
|
|
35
|
+
jan: 1,
|
|
36
|
+
feb: 2,
|
|
37
|
+
mar: 3,
|
|
38
|
+
apr: 4,
|
|
39
|
+
may: 5,
|
|
40
|
+
jun: 6,
|
|
41
|
+
jul: 7,
|
|
42
|
+
aug: 8,
|
|
43
|
+
sep: 9,
|
|
44
|
+
oct: 10,
|
|
45
|
+
nov: 11,
|
|
46
|
+
dec: 12,
|
|
47
|
+
sun: 0,
|
|
48
|
+
mon: 1,
|
|
49
|
+
tue: 2,
|
|
50
|
+
wed: 3,
|
|
51
|
+
thu: 4,
|
|
52
|
+
fri: 5,
|
|
53
|
+
sat: 6
|
|
54
|
+
});
|
|
55
|
+
exports.TIME_UNITS_MAP = Object.freeze({
|
|
56
|
+
SECOND: 'second',
|
|
57
|
+
MINUTE: 'minute',
|
|
58
|
+
HOUR: 'hour',
|
|
59
|
+
DAY_OF_MONTH: 'dayOfMonth',
|
|
60
|
+
MONTH: 'month',
|
|
61
|
+
DAY_OF_WEEK: 'dayOfWeek'
|
|
62
|
+
});
|
|
63
|
+
exports.TIME_UNITS = Object.freeze(Object.values(exports.TIME_UNITS_MAP));
|
|
64
|
+
exports.TIME_UNITS_LEN = exports.TIME_UNITS.length;
|
|
65
|
+
exports.PRESETS = Object.freeze({
|
|
66
|
+
'@yearly': '0 0 0 1 1 *',
|
|
67
|
+
'@monthly': '0 0 0 1 * *',
|
|
68
|
+
'@weekly': '0 0 0 * * 0',
|
|
69
|
+
'@daily': '0 0 0 * * *',
|
|
70
|
+
'@hourly': '0 0 * * * *',
|
|
71
|
+
'@minutely': '0 * * * * *',
|
|
72
|
+
'@secondly': '* * * * * *',
|
|
73
|
+
'@weekdays': '0 0 0 * * 1-5',
|
|
74
|
+
'@weekends': '0 0 0 * * 0,6'
|
|
75
|
+
});
|
|
76
|
+
exports.RE_WILDCARDS = /\*/g;
|
|
77
|
+
exports.RE_RANGE = /^(\d+)(?:-(\d+))?(?:\/(\d+))?$/g;
|
|
78
|
+
//# sourceMappingURL=constants.js.map
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.timeout = exports.sendAt = exports.CronTime = exports.CronJob = void 0;
|
|
4
|
+
const time_1 = require("./time");
|
|
5
|
+
var job_1 = require("./job");
|
|
6
|
+
Object.defineProperty(exports, "CronJob", { enumerable: true, get: function () { return job_1.CronJob; } });
|
|
7
|
+
var time_2 = require("./time");
|
|
8
|
+
Object.defineProperty(exports, "CronTime", { enumerable: true, get: function () { return time_2.CronTime; } });
|
|
9
|
+
const sendAt = (cronTime) => new time_1.CronTime(cronTime).sendAt();
|
|
10
|
+
exports.sendAt = sendAt;
|
|
11
|
+
const timeout = (cronTime) => new time_1.CronTime(cronTime).getTimeout();
|
|
12
|
+
exports.timeout = timeout;
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
package/dist/job.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { CronTime } from './time';
|
|
2
|
+
import { CronJobParams } from './types/cron.types';
|
|
3
|
+
export declare class CronJob {
|
|
4
|
+
cronTime: CronTime;
|
|
5
|
+
running: boolean;
|
|
6
|
+
unrefTimeout: boolean;
|
|
7
|
+
lastExecution: Date | null;
|
|
8
|
+
runOnce: boolean;
|
|
9
|
+
context: unknown;
|
|
10
|
+
onComplete?: (...args: any) => void;
|
|
11
|
+
private _timeout?;
|
|
12
|
+
private _callbacks;
|
|
13
|
+
constructor(cronTime: CronJobParams['cronTime'], onTick: CronJobParams['onTick'], onComplete?: CronJobParams['onComplete'], start?: CronJobParams['start'], timeZone?: CronJobParams['timeZone'], context?: CronJobParams['context'], runOnInit?: CronJobParams['runOnInit'], utcOffset?: CronJobParams['utcOffset'], unrefTimeout?: CronJobParams['unrefTimeout']);
|
|
14
|
+
static from(params: CronJobParams): CronJob;
|
|
15
|
+
private _fnWrap;
|
|
16
|
+
addCallback(callback: (...args: any) => void): void;
|
|
17
|
+
setTime(time: CronTime): void;
|
|
18
|
+
nextDate(): import("luxon").DateTime;
|
|
19
|
+
fireOnTick(): void;
|
|
20
|
+
nextDates(i?: number): import("luxon").DateTime[];
|
|
21
|
+
start(): void;
|
|
22
|
+
lastDate(): Date | null;
|
|
23
|
+
stop(): void;
|
|
24
|
+
}
|
package/dist/job.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CronJob = void 0;
|
|
4
|
+
const child_process_1 = require("child_process");
|
|
5
|
+
const time_1 = require("./time");
|
|
6
|
+
class CronJob {
|
|
7
|
+
constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout) {
|
|
8
|
+
this.running = false;
|
|
9
|
+
this.unrefTimeout = false;
|
|
10
|
+
this.lastExecution = null;
|
|
11
|
+
this.runOnce = false;
|
|
12
|
+
this._callbacks = [];
|
|
13
|
+
this.context = context || this;
|
|
14
|
+
this.cronTime = new time_1.CronTime(cronTime, timeZone, utcOffset);
|
|
15
|
+
if (unrefTimeout != null) {
|
|
16
|
+
this.unrefTimeout = unrefTimeout;
|
|
17
|
+
}
|
|
18
|
+
if (onComplete != null) {
|
|
19
|
+
this.onComplete = this._fnWrap(onComplete);
|
|
20
|
+
}
|
|
21
|
+
if (this.cronTime.realDate) {
|
|
22
|
+
this.runOnce = true;
|
|
23
|
+
}
|
|
24
|
+
this.addCallback(this._fnWrap(onTick));
|
|
25
|
+
if (runOnInit) {
|
|
26
|
+
this.lastExecution = new Date();
|
|
27
|
+
this.fireOnTick();
|
|
28
|
+
}
|
|
29
|
+
if (start)
|
|
30
|
+
this.start();
|
|
31
|
+
}
|
|
32
|
+
static from(params) {
|
|
33
|
+
return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, params.timeZone, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout);
|
|
34
|
+
}
|
|
35
|
+
_fnWrap(cmd) {
|
|
36
|
+
var _a, _b;
|
|
37
|
+
switch (typeof cmd) {
|
|
38
|
+
case 'function': {
|
|
39
|
+
return cmd;
|
|
40
|
+
}
|
|
41
|
+
case 'string': {
|
|
42
|
+
const [command, ...args] = cmd.split(' ');
|
|
43
|
+
return child_process_1.spawn.bind(undefined, command !== null && command !== void 0 ? command : cmd, args);
|
|
44
|
+
}
|
|
45
|
+
case 'object': {
|
|
46
|
+
return child_process_1.spawn.bind(undefined, cmd.command, (_a = cmd.args) !== null && _a !== void 0 ? _a : [], (_b = cmd.options) !== null && _b !== void 0 ? _b : {});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
addCallback(callback) {
|
|
51
|
+
if (typeof callback === 'function') {
|
|
52
|
+
this._callbacks.push(callback);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
setTime(time) {
|
|
56
|
+
if (!(time instanceof time_1.CronTime)) {
|
|
57
|
+
throw new Error('time must be an instance of CronTime.');
|
|
58
|
+
}
|
|
59
|
+
const wasRunning = this.running;
|
|
60
|
+
this.stop();
|
|
61
|
+
this.cronTime = time;
|
|
62
|
+
if (wasRunning)
|
|
63
|
+
this.start();
|
|
64
|
+
}
|
|
65
|
+
nextDate() {
|
|
66
|
+
return this.cronTime.sendAt();
|
|
67
|
+
}
|
|
68
|
+
fireOnTick() {
|
|
69
|
+
for (const callback of this._callbacks) {
|
|
70
|
+
callback.call(this.context, this.onComplete);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
nextDates(i) {
|
|
74
|
+
return this.cronTime.sendAt(i !== null && i !== void 0 ? i : 0);
|
|
75
|
+
}
|
|
76
|
+
start() {
|
|
77
|
+
if (this.running) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const MAXDELAY = 2147483647;
|
|
81
|
+
let timeout = this.cronTime.getTimeout();
|
|
82
|
+
let remaining = 0;
|
|
83
|
+
let startTime;
|
|
84
|
+
const setCronTimeout = (t) => {
|
|
85
|
+
startTime = Date.now();
|
|
86
|
+
this._timeout = setTimeout(callbackWrapper, t);
|
|
87
|
+
if (this.unrefTimeout && typeof this._timeout.unref === 'function') {
|
|
88
|
+
this._timeout.unref();
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const callbackWrapper = () => {
|
|
92
|
+
const diff = startTime + timeout - Date.now();
|
|
93
|
+
if (diff > 0) {
|
|
94
|
+
let newTimeout = this.cronTime.getTimeout();
|
|
95
|
+
if (newTimeout > diff) {
|
|
96
|
+
newTimeout = diff;
|
|
97
|
+
}
|
|
98
|
+
remaining += newTimeout;
|
|
99
|
+
}
|
|
100
|
+
this.lastExecution = new Date();
|
|
101
|
+
if (remaining) {
|
|
102
|
+
if (remaining > MAXDELAY) {
|
|
103
|
+
remaining -= MAXDELAY;
|
|
104
|
+
timeout = MAXDELAY;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
timeout = remaining;
|
|
108
|
+
remaining = 0;
|
|
109
|
+
}
|
|
110
|
+
setCronTimeout(timeout);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
this.running = false;
|
|
114
|
+
if (!this.runOnce) {
|
|
115
|
+
this.start();
|
|
116
|
+
}
|
|
117
|
+
this.fireOnTick();
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
if (timeout >= 0) {
|
|
121
|
+
this.running = true;
|
|
122
|
+
if (timeout > MAXDELAY) {
|
|
123
|
+
remaining = timeout - MAXDELAY;
|
|
124
|
+
timeout = MAXDELAY;
|
|
125
|
+
}
|
|
126
|
+
setCronTimeout(timeout);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
this.stop();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
lastDate() {
|
|
133
|
+
return this.lastExecution;
|
|
134
|
+
}
|
|
135
|
+
stop() {
|
|
136
|
+
if (this._timeout)
|
|
137
|
+
clearTimeout(this._timeout);
|
|
138
|
+
this.running = false;
|
|
139
|
+
if (typeof this.onComplete === 'function') {
|
|
140
|
+
this.onComplete();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
exports.CronJob = CronJob;
|
|
145
|
+
//# sourceMappingURL=job.js.map
|
package/dist/time.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { DateTime, Zone } from 'luxon';
|
|
2
|
+
export declare class CronTime {
|
|
3
|
+
source: string | DateTime;
|
|
4
|
+
zone?: string;
|
|
5
|
+
utcOffset?: number | string;
|
|
6
|
+
realDate: boolean;
|
|
7
|
+
private second;
|
|
8
|
+
private minute;
|
|
9
|
+
private hour;
|
|
10
|
+
private dayOfMonth;
|
|
11
|
+
private month;
|
|
12
|
+
private dayOfWeek;
|
|
13
|
+
constructor(source: string | Date | DateTime, zone?: string | null, utcOffset?: string | number | null);
|
|
14
|
+
private _getWeekDay;
|
|
15
|
+
private _verifyParse;
|
|
16
|
+
sendAt(): DateTime;
|
|
17
|
+
sendAt(i: number): DateTime[];
|
|
18
|
+
getTimeout(): number;
|
|
19
|
+
toString(): string;
|
|
20
|
+
toJSON(): string[];
|
|
21
|
+
getNextDateFrom(start: Date | DateTime, zone?: string | Zone): DateTime;
|
|
22
|
+
private _findPreviousDSTJump;
|
|
23
|
+
private _checkTimeInSkippedRange;
|
|
24
|
+
private _checkTimeInSkippedRangeSingleHour;
|
|
25
|
+
private _checkTimeInSkippedRangeMultiHour;
|
|
26
|
+
private _forwardDSTJump;
|
|
27
|
+
private _wcOrAll;
|
|
28
|
+
private _hasAll;
|
|
29
|
+
private _parse;
|
|
30
|
+
private _parseField;
|
|
31
|
+
}
|