cron 5.0.0-beta.2 → 5.0.0-beta.4
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 +91 -1313
- package/README.md +1 -1
- package/dist/job.js +64 -73
- package/dist/time.d.ts +4 -0
- package/dist/time.js +58 -25
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -72,7 +72,7 @@ const job = new CronJob('* * * * * *', function () {
|
|
|
72
72
|
});
|
|
73
73
|
```
|
|
74
74
|
|
|
75
|
-
While this method is still officially supported (any bugs or security issues will be fixed) it is deprecated and no longer recommended. You will get a warning that support will be dropped in the next major version of Cron.
|
|
75
|
+
While this method is still officially supported (any bugs or security issues with it will be fixed) it is deprecated and no longer recommended. You will get a warning that support will be dropped in the next major version of Cron.
|
|
76
76
|
|
|
77
77
|
Instead, create `CronJob` objects like this:
|
|
78
78
|
|
package/dist/job.js
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.CronJob = void 0;
|
|
13
4
|
const errors_1 = require("./errors");
|
|
14
5
|
const time_1 = require("./time");
|
|
15
6
|
class CronJob {
|
|
7
|
+
cronTime;
|
|
8
|
+
unrefTimeout = false;
|
|
9
|
+
lastExecution = null;
|
|
10
|
+
runOnce = false;
|
|
11
|
+
context;
|
|
12
|
+
onComplete;
|
|
13
|
+
waitForCompletion = false;
|
|
14
|
+
errorHandler;
|
|
15
|
+
name;
|
|
16
|
+
threshold = 250;
|
|
17
|
+
_isActive = false;
|
|
18
|
+
_isCallbackRunning = false;
|
|
19
|
+
_timeout;
|
|
20
|
+
_callbacks = [];
|
|
16
21
|
get isActive() {
|
|
17
22
|
return this._isActive;
|
|
18
23
|
}
|
|
@@ -20,27 +25,19 @@ class CronJob {
|
|
|
20
25
|
return this._isCallbackRunning;
|
|
21
26
|
}
|
|
22
27
|
constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout, waitForCompletion, errorHandler, name, threshold, usedFrom) {
|
|
23
|
-
this.unrefTimeout = false;
|
|
24
|
-
this.lastExecution = null;
|
|
25
|
-
this.runOnce = false;
|
|
26
|
-
this.waitForCompletion = false;
|
|
27
|
-
this.threshold = 250;
|
|
28
|
-
this._isActive = false;
|
|
29
|
-
this._isCallbackRunning = false;
|
|
30
|
-
this._callbacks = [];
|
|
31
28
|
if (!usedFrom) {
|
|
32
29
|
console.warn(`[Cron] Warning: The parameterized constructor is deprecated and will be removed in the next major version of Cron (v6).
|
|
33
30
|
\n Consider updating the instantiation of your CronJob object to use the static 'from' method.
|
|
34
31
|
\n See https://github.com/kelektiv/node-cron#deprecated-the-parameterized-constructor`);
|
|
35
32
|
}
|
|
36
|
-
this.context = (context
|
|
33
|
+
this.context = (context ?? this);
|
|
37
34
|
this.waitForCompletion = Boolean(waitForCompletion);
|
|
38
35
|
this.errorHandler = errorHandler;
|
|
39
36
|
(0, errors_1.assertExclusiveTimeZoneAndUtcOffset)(timeZone, utcOffset);
|
|
40
37
|
this.cronTime =
|
|
41
38
|
timeZone != null
|
|
42
39
|
? new time_1.CronTime(cronTime, timeZone, null)
|
|
43
|
-
: new time_1.CronTime(cronTime, null, utcOffset
|
|
40
|
+
: new time_1.CronTime(cronTime, null, utcOffset ?? null);
|
|
44
41
|
if (unrefTimeout != null) {
|
|
45
42
|
this.unrefTimeout = unrefTimeout;
|
|
46
43
|
}
|
|
@@ -99,44 +96,42 @@ class CronJob {
|
|
|
99
96
|
nextDate() {
|
|
100
97
|
return this.cronTime.sendAt();
|
|
101
98
|
}
|
|
102
|
-
fireOnTick() {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
});
|
|
123
|
-
}
|
|
99
|
+
async fireOnTick() {
|
|
100
|
+
if (this.waitForCompletion && this._isCallbackRunning)
|
|
101
|
+
return;
|
|
102
|
+
this._isCallbackRunning = true;
|
|
103
|
+
try {
|
|
104
|
+
for (const callback of this._callbacks) {
|
|
105
|
+
const result = callback.call(this.context, this.onComplete);
|
|
106
|
+
if (result &&
|
|
107
|
+
typeof result === 'object' &&
|
|
108
|
+
typeof result.then === 'function') {
|
|
109
|
+
if (this.waitForCompletion) {
|
|
110
|
+
await result;
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
result.catch(error => {
|
|
114
|
+
if (this.errorHandler != null)
|
|
115
|
+
this.errorHandler(error);
|
|
116
|
+
else
|
|
117
|
+
console.error('[Cron] error in callback', error);
|
|
118
|
+
});
|
|
124
119
|
}
|
|
125
120
|
}
|
|
126
121
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
if (this.errorHandler != null)
|
|
125
|
+
this.errorHandler(error);
|
|
126
|
+
else
|
|
127
|
+
console.error('[Cron] error in callback', error);
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
this._isCallbackRunning = false;
|
|
131
|
+
}
|
|
137
132
|
}
|
|
138
133
|
nextDates(i) {
|
|
139
|
-
return this.cronTime.sendAt(i
|
|
134
|
+
return this.cronTime.sendAt(i ?? 0);
|
|
140
135
|
}
|
|
141
136
|
start() {
|
|
142
137
|
if (this._isActive)
|
|
@@ -206,24 +201,20 @@ class CronJob {
|
|
|
206
201
|
lastDate() {
|
|
207
202
|
return this.lastExecution;
|
|
208
203
|
}
|
|
209
|
-
_executeOnComplete() {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
});
|
|
204
|
+
async _executeOnComplete() {
|
|
205
|
+
if (typeof this.onComplete !== 'function')
|
|
206
|
+
return;
|
|
207
|
+
try {
|
|
208
|
+
await this.onComplete.call(this.context);
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
console.error('[Cron] error in onComplete callback:', error);
|
|
212
|
+
}
|
|
220
213
|
}
|
|
221
|
-
_waitForJobCompletion() {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
});
|
|
214
|
+
async _waitForJobCompletion() {
|
|
215
|
+
while (this._isCallbackRunning) {
|
|
216
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
217
|
+
}
|
|
227
218
|
}
|
|
228
219
|
stop() {
|
|
229
220
|
if (this._timeout)
|
|
@@ -233,10 +224,10 @@ class CronJob {
|
|
|
233
224
|
void this._executeOnComplete();
|
|
234
225
|
return;
|
|
235
226
|
}
|
|
236
|
-
return Promise.resolve().then(() =>
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
})
|
|
227
|
+
return Promise.resolve().then(async () => {
|
|
228
|
+
await this._waitForJobCompletion();
|
|
229
|
+
await this._executeOnComplete();
|
|
230
|
+
});
|
|
240
231
|
}
|
|
241
232
|
}
|
|
242
233
|
exports.CronJob = CronJob;
|
package/dist/time.d.ts
CHANGED
|
@@ -25,6 +25,10 @@ export declare class CronTime {
|
|
|
25
25
|
error?: CronError;
|
|
26
26
|
};
|
|
27
27
|
private _getWeekDay;
|
|
28
|
+
private _matchesDay;
|
|
29
|
+
private _matches;
|
|
30
|
+
private _matchesMinute;
|
|
31
|
+
private _firstMatchBetween;
|
|
28
32
|
sendAt(): DateTime;
|
|
29
33
|
sendAt(i: number): DateTime[];
|
|
30
34
|
getTimeout(): number;
|
package/dist/time.js
CHANGED
|
@@ -5,14 +5,17 @@ const luxon_1 = require("luxon");
|
|
|
5
5
|
const constants_1 = require("./constants");
|
|
6
6
|
const errors_1 = require("./errors");
|
|
7
7
|
class CronTime {
|
|
8
|
+
source;
|
|
9
|
+
timeZone;
|
|
10
|
+
utcOffset;
|
|
11
|
+
realDate = false;
|
|
12
|
+
second = {};
|
|
13
|
+
minute = {};
|
|
14
|
+
hour = {};
|
|
15
|
+
dayOfMonth = {};
|
|
16
|
+
month = {};
|
|
17
|
+
dayOfWeek = {};
|
|
8
18
|
constructor(source, timeZone, utcOffset) {
|
|
9
|
-
this.realDate = false;
|
|
10
|
-
this.second = {};
|
|
11
|
-
this.minute = {};
|
|
12
|
-
this.hour = {};
|
|
13
|
-
this.dayOfMonth = {};
|
|
14
|
-
this.month = {};
|
|
15
|
-
this.dayOfWeek = {};
|
|
16
19
|
(0, errors_1.assertExclusiveTimeZoneAndUtcOffset)(timeZone, utcOffset);
|
|
17
20
|
if (timeZone) {
|
|
18
21
|
const dt = luxon_1.DateTime.fromObject({}, { zone: timeZone });
|
|
@@ -55,6 +58,37 @@ class CronTime {
|
|
|
55
58
|
_getWeekDay(date) {
|
|
56
59
|
return date.weekday === 7 ? 0 : date.weekday;
|
|
57
60
|
}
|
|
61
|
+
_matchesDay(date) {
|
|
62
|
+
return !((!(date.day in this.dayOfMonth) &&
|
|
63
|
+
Object.keys(this.dayOfMonth).length !== 31 &&
|
|
64
|
+
!(this._getWeekDay(date) in this.dayOfWeek &&
|
|
65
|
+
Object.keys(this.dayOfWeek).length !== 7)) ||
|
|
66
|
+
(!(this._getWeekDay(date) in this.dayOfWeek) &&
|
|
67
|
+
Object.keys(this.dayOfWeek).length !== 7 &&
|
|
68
|
+
!(date.day in this.dayOfMonth &&
|
|
69
|
+
Object.keys(this.dayOfMonth).length !== 31)));
|
|
70
|
+
}
|
|
71
|
+
_matches(date) {
|
|
72
|
+
return this._matchesMinute(date) && date.second in this.second;
|
|
73
|
+
}
|
|
74
|
+
_matchesMinute(date) {
|
|
75
|
+
return (date.month in this.month &&
|
|
76
|
+
this._matchesDay(date) &&
|
|
77
|
+
date.hour in this.hour &&
|
|
78
|
+
date.minute in this.minute);
|
|
79
|
+
}
|
|
80
|
+
_firstMatchBetween(from, to) {
|
|
81
|
+
let date = from;
|
|
82
|
+
while (date < to) {
|
|
83
|
+
if (this._matches(date)) {
|
|
84
|
+
return date;
|
|
85
|
+
}
|
|
86
|
+
date = this._matchesMinute(date)
|
|
87
|
+
? date.plus({ second: 1 })
|
|
88
|
+
: date.plus({ minute: 1 }).startOf('minute');
|
|
89
|
+
}
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
58
92
|
sendAt(i) {
|
|
59
93
|
let date = this.realDate && this.source instanceof luxon_1.DateTime
|
|
60
94
|
? this.source
|
|
@@ -105,7 +139,6 @@ class CronTime {
|
|
|
105
139
|
});
|
|
106
140
|
}
|
|
107
141
|
getNextDateFrom(start, timeZone) {
|
|
108
|
-
var _a, _b;
|
|
109
142
|
if (start instanceof Date) {
|
|
110
143
|
start = luxon_1.DateTime.fromJSDate(start);
|
|
111
144
|
}
|
|
@@ -113,7 +146,7 @@ class CronTime {
|
|
|
113
146
|
start = start.setZone(timeZone);
|
|
114
147
|
}
|
|
115
148
|
else {
|
|
116
|
-
timeZone =
|
|
149
|
+
timeZone = start.zone.zoneName ?? start.zone.fixed;
|
|
117
150
|
}
|
|
118
151
|
let date = luxon_1.DateTime.fromFormat(`${start.year}-${start.month}-${start.day} ${start.hour}:${start.minute}:${start.second}`, 'yyyy-M-d H:m:s', {
|
|
119
152
|
zone: 'UTC'
|
|
@@ -132,7 +165,7 @@ class CronTime {
|
|
|
132
165
|
if (date > maxMatch) {
|
|
133
166
|
throw new errors_1.CronError(`Something went wrong. No execution date was found in the next 8 years.
|
|
134
167
|
Please provide the following string if you would like to help debug:
|
|
135
|
-
Time Zone: ${
|
|
168
|
+
Time Zone: ${timeZone?.toString() ?? '""'} - Cron String: ${this.source.toString()} - UTC offset: ${date.offset} - current Date: ${luxon_1.DateTime.local().toString()}`);
|
|
136
169
|
}
|
|
137
170
|
if (!(date.month in this.month) &&
|
|
138
171
|
Object.keys(this.month).length !== 12) {
|
|
@@ -140,14 +173,7 @@ class CronTime {
|
|
|
140
173
|
date = date.set({ day: 1, hour: 0, minute: 0, second: 0 });
|
|
141
174
|
continue;
|
|
142
175
|
}
|
|
143
|
-
if (
|
|
144
|
-
Object.keys(this.dayOfMonth).length !== 31 &&
|
|
145
|
-
!(this._getWeekDay(date) in this.dayOfWeek &&
|
|
146
|
-
Object.keys(this.dayOfWeek).length !== 7)) ||
|
|
147
|
-
(!(this._getWeekDay(date) in this.dayOfWeek) &&
|
|
148
|
-
Object.keys(this.dayOfWeek).length !== 7 &&
|
|
149
|
-
!(date.day in this.dayOfMonth &&
|
|
150
|
-
Object.keys(this.dayOfMonth).length !== 31))) {
|
|
176
|
+
if (!this._matchesDay(date)) {
|
|
151
177
|
date = date.plus({ days: 1 });
|
|
152
178
|
date = date.set({ hour: 0, minute: 0, second: 0 });
|
|
153
179
|
continue;
|
|
@@ -176,7 +202,11 @@ class CronTime {
|
|
|
176
202
|
date = luxon_1.DateTime.fromFormat(`${date.year}-${date.month}-${date.day} ${date.hour}:${date.minute}:${date.second}`, 'yyyy-M-d H:m:s', {
|
|
177
203
|
zone: timeZone
|
|
178
204
|
});
|
|
179
|
-
const
|
|
205
|
+
const januaryReferenceDate = luxon_1.DateTime.fromFormat(`${date.year}-1-1 0:0:0`, 'yyyy-M-d H:m:s', { zone: timeZone });
|
|
206
|
+
const julyReferenceDate = luxon_1.DateTime.fromFormat(`${date.year}-7-1 0:0:0`, 'yyyy-M-d H:m:s', { zone: timeZone });
|
|
207
|
+
const nonDSTReferenceDate = januaryReferenceDate.offset <= julyReferenceDate.offset
|
|
208
|
+
? januaryReferenceDate
|
|
209
|
+
: julyReferenceDate;
|
|
180
210
|
if ((expectedHour !== date.hour || expectedMinute !== date.minute) &&
|
|
181
211
|
nonDSTReferenceDate.offset !== date.offset) {
|
|
182
212
|
while (date.minus({ minute: 1 }).offset !== nonDSTReferenceDate.offset) {
|
|
@@ -188,14 +218,18 @@ class CronTime {
|
|
|
188
218
|
const twoHourTestDate = date.minus({ hour: 2 });
|
|
189
219
|
if ((hourTestDate.hour === date.hour ||
|
|
190
220
|
twoHourTestDate.hour === hourTestDate.hour) &&
|
|
221
|
+
(hourTestDate.offset > date.offset ||
|
|
222
|
+
twoHourTestDate.offset > hourTestDate.offset) &&
|
|
191
223
|
hourTestDate > start) {
|
|
192
|
-
date = hourTestDate;
|
|
224
|
+
date = this._firstMatchBetween(hourTestDate, date) ?? date;
|
|
193
225
|
}
|
|
194
226
|
const halfHourTestDate = date.minus({ minute: 30 });
|
|
195
227
|
if ((halfHourTestDate.minute === date.minute ||
|
|
196
228
|
hourTestDate.minute === halfHourTestDate.minute) &&
|
|
229
|
+
(halfHourTestDate.offset > date.offset ||
|
|
230
|
+
hourTestDate.offset > halfHourTestDate.offset) &&
|
|
197
231
|
halfHourTestDate > start) {
|
|
198
|
-
date = halfHourTestDate;
|
|
232
|
+
date = this._firstMatchBetween(halfHourTestDate, date) ?? date;
|
|
199
233
|
}
|
|
200
234
|
return date;
|
|
201
235
|
}
|
|
@@ -221,7 +255,6 @@ class CronTime {
|
|
|
221
255
|
return true;
|
|
222
256
|
}
|
|
223
257
|
_parse(source) {
|
|
224
|
-
var _a;
|
|
225
258
|
source = source.toLowerCase();
|
|
226
259
|
if (Object.keys(constants_1.PRESETS).includes(source)) {
|
|
227
260
|
source = constants_1.PRESETS[source];
|
|
@@ -242,7 +275,7 @@ class CronTime {
|
|
|
242
275
|
const unitsLen = units.length;
|
|
243
276
|
for (const unit of constants_1.TIME_UNITS) {
|
|
244
277
|
const i = constants_1.TIME_UNITS.indexOf(unit);
|
|
245
|
-
const cur =
|
|
278
|
+
const cur = units[i - (constants_1.TIME_UNITS_LEN - unitsLen)] ?? constants_1.PARSE_DEFAULTS[unit];
|
|
246
279
|
this._parseField(cur, unit);
|
|
247
280
|
}
|
|
248
281
|
}
|
|
@@ -263,12 +296,12 @@ class CronTime {
|
|
|
263
296
|
const allRanges = value.split(',');
|
|
264
297
|
for (const range of allRanges) {
|
|
265
298
|
const match = [...range.matchAll(constants_1.RE_RANGE)][0];
|
|
266
|
-
if (
|
|
299
|
+
if (match?.[1] !== undefined) {
|
|
267
300
|
const [, mLower, mUpper, mStep] = match;
|
|
268
301
|
let lower = parseInt(mLower, 10);
|
|
269
302
|
let upper = mUpper !== undefined ? parseInt(mUpper, 10) : undefined;
|
|
270
303
|
const wasStepDefined = mStep !== undefined;
|
|
271
|
-
const step = parseInt(mStep
|
|
304
|
+
const step = parseInt(mStep ?? '1', 10);
|
|
272
305
|
if (step === 0) {
|
|
273
306
|
throw new errors_1.CronError(`Field (${unit}) has a step of zero`);
|
|
274
307
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cron",
|
|
3
3
|
"description": "Cron jobs for your node",
|
|
4
|
-
"version": "5.0.0-beta.
|
|
4
|
+
"version": "5.0.0-beta.4",
|
|
5
5
|
"author": "Brandon der Blätter <https://intcreator.com/> (https://github.com/intcreator)",
|
|
6
6
|
"funding": {
|
|
7
7
|
"type": "ko-fi",
|
|
@@ -28,33 +28,33 @@
|
|
|
28
28
|
"prepare": "husky"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@types/luxon": "~3.7.
|
|
32
|
-
"luxon": "~3.7.
|
|
31
|
+
"@types/luxon": "~3.7.6",
|
|
32
|
+
"luxon": "~3.7.2"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@commitlint/cli": "20.5.3",
|
|
36
36
|
"@eslint/js": "9.39.4",
|
|
37
|
-
"@fast-check/jest": "2.
|
|
37
|
+
"@fast-check/jest": "2.3.0",
|
|
38
38
|
"@insurgent/commitlint-config": "20.0.0",
|
|
39
39
|
"@insurgent/conventional-changelog-preset": "10.0.0",
|
|
40
40
|
"@semantic-release/changelog": "6.0.3",
|
|
41
41
|
"@semantic-release/commit-analyzer": "13.0.1",
|
|
42
42
|
"@semantic-release/git": "10.0.1",
|
|
43
|
-
"@semantic-release/github": "12.0.
|
|
44
|
-
"@semantic-release/npm": "13.
|
|
43
|
+
"@semantic-release/github": "12.0.10",
|
|
44
|
+
"@semantic-release/npm": "13.2.0",
|
|
45
45
|
"@semantic-release/release-notes-generator": "14.1.1",
|
|
46
|
-
"@swc/core": "1.16.
|
|
46
|
+
"@swc/core": "1.16.22",
|
|
47
47
|
"@swc/jest": "0.2.39",
|
|
48
48
|
"@types/jest": "30.0.0",
|
|
49
49
|
"@types/node": "24.13.3",
|
|
50
|
-
"@types/sinon": "22.0.
|
|
50
|
+
"@types/sinon": "22.0.2",
|
|
51
51
|
"cross-env": "7.0.3",
|
|
52
52
|
"eslint": "8.57.1",
|
|
53
53
|
"eslint-config-prettier": "9.1.2",
|
|
54
54
|
"eslint-plugin-jest": "27.9.0",
|
|
55
55
|
"eslint-plugin-prettier": "5.5.6",
|
|
56
56
|
"husky": "9.1.7",
|
|
57
|
-
"jest": "30.
|
|
57
|
+
"jest": "30.5.2",
|
|
58
58
|
"lint-staged": "15.5.2",
|
|
59
59
|
"prettier": "3.8.4",
|
|
60
60
|
"semantic-release": "25.0.9",
|