cron 4.1.4 → 4.1.5-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 CHANGED
@@ -1,3 +1,9 @@
1
+ ## [4.1.5-beta.1](https://github.com/kelektiv/node-cron/compare/v4.1.4...v4.1.5-beta.1) (2025-04-06)
2
+
3
+ ### 🐛 Bug Fixes
4
+
5
+ * prevent jobs from stopping unexpectedly ([#963](https://github.com/kelektiv/node-cron/issues/963)) ([69d2ef5](https://github.com/kelektiv/node-cron/commit/69d2ef5ce5235985ceba391e0e04379550572374))
6
+
1
7
  ## [4.1.4](https://github.com/kelektiv/node-cron/compare/v4.1.3...v4.1.4) (2025-04-06)
2
8
 
3
9
  ### 🛠 Builds
package/README.md CHANGED
@@ -195,7 +195,7 @@ day of week 0-7 (0 or 7 is Sunday, or use names)
195
195
 
196
196
  #### Constructor
197
197
 
198
- `constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout)`:
198
+ `constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout, waitForCompletion, errorHandler, name, threshold)`:
199
199
 
200
200
  - `cronTime`: [REQUIRED] - The time to fire off your job. Can be cron syntax, a JS [`Date`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) object or a Luxon [`DateTime`](https://moment.github.io/luxon/api-docs/index.html#datetime) object.
201
201
 
@@ -219,6 +219,10 @@ day of week 0-7 (0 or 7 is Sunday, or use names)
219
219
 
220
220
  - `errorHandler`: [OPTIONAL] - Function to handle any exceptions that occur in the `onTick` method.
221
221
 
222
+ - `name`: [OPTIONAL] - Name of the job. Useful for identifying jobs in logs.
223
+
224
+ - `threshold`: [OPTIONAL] - Threshold in ms to control whether to execute or skip missed execution deadlines caused by slow or busy hardware. Execution delays within threshold will be executed immediately, and otherwise will be skipped. In both cases a warning will be printed to the console with the job name and cron expression. See [issue #962](https://github.com/kelektiv/node-cron/issues/962) for more information. Default is `250`.
225
+
222
226
  #### Methods
223
227
 
224
228
  - `from` (static): Create a new CronJob object providing arguments as an object. See argument names and descriptions above.
package/dist/job.d.ts CHANGED
@@ -9,14 +9,16 @@ export declare class CronJob<OC extends CronOnCompleteCommand | null = null, C =
9
9
  onComplete?: WithOnComplete<OC> extends true ? CronOnCompleteCallback : undefined;
10
10
  waitForCompletion: boolean;
11
11
  errorHandler?: CronJobParams<OC, C>['errorHandler'];
12
+ name?: string;
13
+ threshold: number;
12
14
  private _isActive;
13
15
  private _isCallbackRunning;
14
16
  private _timeout?;
15
17
  private _callbacks;
16
18
  get isActive(): boolean;
17
19
  get isCallbackRunning(): boolean;
18
- constructor(cronTime: CronJobParams<OC, C>['cronTime'], onTick: CronJobParams<OC, C>['onTick'], onComplete?: CronJobParams<OC, C>['onComplete'], start?: CronJobParams<OC, C>['start'], timeZone?: CronJobParams<OC, C>['timeZone'], context?: CronJobParams<OC, C>['context'], runOnInit?: CronJobParams<OC, C>['runOnInit'], utcOffset?: null, unrefTimeout?: CronJobParams<OC, C>['unrefTimeout'], waitForCompletion?: CronJobParams<OC, C>['waitForCompletion'], errorHandler?: CronJobParams<OC, C>['errorHandler']);
19
- constructor(cronTime: CronJobParams<OC, C>['cronTime'], onTick: CronJobParams<OC, C>['onTick'], onComplete?: CronJobParams<OC, C>['onComplete'], start?: CronJobParams<OC, C>['start'], timeZone?: null, context?: CronJobParams<OC, C>['context'], runOnInit?: CronJobParams<OC, C>['runOnInit'], utcOffset?: CronJobParams<OC, C>['utcOffset'], unrefTimeout?: CronJobParams<OC, C>['unrefTimeout'], waitForCompletion?: CronJobParams<OC, C>['waitForCompletion'], errorHandler?: CronJobParams<OC, C>['errorHandler']);
20
+ constructor(cronTime: CronJobParams<OC, C>['cronTime'], onTick: CronJobParams<OC, C>['onTick'], onComplete?: CronJobParams<OC, C>['onComplete'], start?: CronJobParams<OC, C>['start'], timeZone?: CronJobParams<OC, C>['timeZone'], context?: CronJobParams<OC, C>['context'], runOnInit?: CronJobParams<OC, C>['runOnInit'], utcOffset?: null, unrefTimeout?: CronJobParams<OC, C>['unrefTimeout'], waitForCompletion?: CronJobParams<OC, C>['waitForCompletion'], errorHandler?: CronJobParams<OC, C>['errorHandler'], name?: CronJobParams<OC, C>['name'], threshold?: CronJobParams<OC, C>['threshold']);
21
+ constructor(cronTime: CronJobParams<OC, C>['cronTime'], onTick: CronJobParams<OC, C>['onTick'], onComplete?: CronJobParams<OC, C>['onComplete'], start?: CronJobParams<OC, C>['start'], timeZone?: null, context?: CronJobParams<OC, C>['context'], runOnInit?: CronJobParams<OC, C>['runOnInit'], utcOffset?: CronJobParams<OC, C>['utcOffset'], unrefTimeout?: CronJobParams<OC, C>['unrefTimeout'], waitForCompletion?: CronJobParams<OC, C>['waitForCompletion'], errorHandler?: CronJobParams<OC, C>['errorHandler'], name?: CronJobParams<OC, C>['name'], threshold?: CronJobParams<OC, C>['threshold']);
20
22
  static from<OC extends CronOnCompleteCommand | null = null, C = null>(params: CronJobParams<OC, C>): CronJob<OC, C>;
21
23
  private _fnWrap;
22
24
  addCallback(callback: CronCallback<C, WithOnComplete<OC>>): void;
package/dist/job.js CHANGED
@@ -20,11 +20,12 @@ class CronJob {
20
20
  get isCallbackRunning() {
21
21
  return this._isCallbackRunning;
22
22
  }
23
- constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout, waitForCompletion, errorHandler) {
23
+ constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout, waitForCompletion, errorHandler, name, threshold) {
24
24
  this.unrefTimeout = false;
25
25
  this.lastExecution = null;
26
26
  this.runOnce = false;
27
27
  this.waitForCompletion = false;
28
+ this.threshold = 250;
28
29
  this._isActive = false;
29
30
  this._isCallbackRunning = false;
30
31
  this._callbacks = [];
@@ -49,6 +50,12 @@ class CronJob {
49
50
  if (onComplete != null) {
50
51
  this.onComplete = this._fnWrap(onComplete);
51
52
  }
53
+ if (threshold != null) {
54
+ this.threshold = Math.abs(threshold);
55
+ }
56
+ if (name != null) {
57
+ this.name = name;
58
+ }
52
59
  if (this.cronTime.realDate) {
53
60
  this.runOnce = true;
54
61
  }
@@ -65,13 +72,13 @@ class CronJob {
65
72
  throw new errors_1.ExclusiveParametersError('timeZone', 'utcOffset');
66
73
  }
67
74
  if (params.timeZone != null) {
68
- return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, params.timeZone, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout, params.waitForCompletion, params.errorHandler);
75
+ return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, params.timeZone, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout, params.waitForCompletion, params.errorHandler, params.name, params.threshold);
69
76
  }
70
77
  else if (params.utcOffset != null) {
71
- return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, null, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout, params.waitForCompletion, params.errorHandler);
78
+ return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, null, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout, params.waitForCompletion, params.errorHandler, params.name, params.threshold);
72
79
  }
73
80
  else {
74
- return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, params.timeZone, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout, params.waitForCompletion, params.errorHandler);
81
+ return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, params.timeZone, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout, params.waitForCompletion, params.errorHandler, params.name, params.threshold);
75
82
  }
76
83
  }
77
84
  _fnWrap(cmd) {
@@ -138,6 +145,7 @@ class CronJob {
138
145
  start() {
139
146
  if (this._isActive)
140
147
  return;
148
+ this._isActive = true;
141
149
  const MAXDELAY = 2147483647;
142
150
  let timeout = this.cronTime.getTimeout();
143
151
  let remaining = 0;
@@ -178,7 +186,6 @@ class CronJob {
178
186
  }
179
187
  };
180
188
  if (timeout >= 0) {
181
- this._isActive = true;
182
189
  if (timeout > MAXDELAY) {
183
190
  remaining = timeout - MAXDELAY;
184
191
  timeout = MAXDELAY;
@@ -186,7 +193,18 @@ class CronJob {
186
193
  setCronTimeout(timeout);
187
194
  }
188
195
  else {
189
- this.stop();
196
+ const absoluteTimeout = Math.abs(timeout);
197
+ const message = `[Cron] Missed execution deadline by ${absoluteTimeout}ms for job${this.name ? ` "${this.name}"` : ''} with cron expression '${String(this.cronTime.source)}'`;
198
+ if (absoluteTimeout <= this.threshold) {
199
+ console.warn(`${message}. Executing immediately.`);
200
+ this.lastExecution = new Date();
201
+ void this.fireOnTick();
202
+ }
203
+ else {
204
+ console.warn(`${message}. Skipping execution as it exceeds threshold (${this.threshold}ms).`);
205
+ }
206
+ timeout = this.cronTime.getTimeout();
207
+ setCronTimeout(timeout);
190
208
  }
191
209
  }
192
210
  lastDate() {
package/dist/time.js CHANGED
@@ -96,7 +96,7 @@ class CronTime {
96
96
  }
97
97
  }
98
98
  getTimeout() {
99
- return Math.max(-1, this.sendAt().toMillis() - luxon_1.DateTime.utc().toMillis());
99
+ return this.sendAt().toMillis() - luxon_1.DateTime.local().toMillis();
100
100
  }
101
101
  toString() {
102
102
  return this.toJSON().join(' ');
@@ -13,6 +13,8 @@ interface BaseCronJobParams<OC extends CronOnCompleteCommand | null = null, C =
13
13
  unrefTimeout?: boolean | null;
14
14
  waitForCompletion?: boolean | null;
15
15
  errorHandler?: ((error: unknown) => void) | null;
16
+ threshold?: number | null;
17
+ name?: string | null;
16
18
  }
17
19
  export type CronJobParams<OC extends CronOnCompleteCommand | null = null, C = null> = BaseCronJobParams<OC, C> & ({
18
20
  timeZone?: string | null;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cron",
3
3
  "description": "Cron jobs for your node",
4
- "version": "4.1.4",
4
+ "version": "4.1.5-beta.1",
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"