cron 3.2.1 → 3.3.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 CHANGED
@@ -1,3 +1,30 @@
1
+ ## [3.3.0](https://github.com/kelektiv/node-cron/compare/v3.2.1...v3.3.0) (2024-12-10)
2
+
3
+ ### ✨ Features
4
+
5
+ * support async handling and add CronJob status tracking ([#894](https://github.com/kelektiv/node-cron/issues/894)) ([b58fb6b](https://github.com/kelektiv/node-cron/commit/b58fb6b1dc122a6d55bd13134aab1a038e9a531d)), closes [#713](https://github.com/kelektiv/node-cron/issues/713) [#556](https://github.com/kelektiv/node-cron/issues/556)
6
+
7
+ ### ⚙️ Continuous Integrations
8
+
9
+ * **action:** update github/codeql-action action to v3.27.2 ([#912](https://github.com/kelektiv/node-cron/issues/912)) ([d11ba30](https://github.com/kelektiv/node-cron/commit/d11ba304b380e03e3fa3f7f1185b3eb6cb259405))
10
+ * **action:** update github/codeql-action action to v3.27.5 ([#917](https://github.com/kelektiv/node-cron/issues/917)) ([2a4035e](https://github.com/kelektiv/node-cron/commit/2a4035e4310495847a3cfa54a893e2c216d54c09))
11
+ * **action:** update step-security/harden-runner action to v2.10.2 ([#920](https://github.com/kelektiv/node-cron/issues/920)) ([26a8f9f](https://github.com/kelektiv/node-cron/commit/26a8f9f714c04077f77d24214676feeb1ccf1837))
12
+ * add pre-commit hook to lint and prettify ([#911](https://github.com/kelektiv/node-cron/issues/911)) ([e1140d1](https://github.com/kelektiv/node-cron/commit/e1140d1f6d4fa79d7a2abb876a4aad9c111fec2f)), closes [#907](https://github.com/kelektiv/node-cron/issues/907)
13
+
14
+ ### ♻️ Chores
15
+
16
+ * **deps:** lock file maintenance ([94465ae](https://github.com/kelektiv/node-cron/commit/94465aed29609c20fc1f24b52547fb022782a164))
17
+ * **deps:** lock file maintenance ([23d67a4](https://github.com/kelektiv/node-cron/commit/23d67a4c5095ac96bb37ae2dae9b5a72b580aca4))
18
+ * **deps:** lock file maintenance ([135fdf7](https://github.com/kelektiv/node-cron/commit/135fdf7667ce5a4516dab975b1592fe43a7d2882))
19
+ * **deps:** lock file maintenance ([edcff3b](https://github.com/kelektiv/node-cron/commit/edcff3b87750057d82ec8df62770dad63af00d59))
20
+ * **deps:** pin dependency lint-staged to 15.2.10 ([#916](https://github.com/kelektiv/node-cron/issues/916)) ([5cf24da](https://github.com/kelektiv/node-cron/commit/5cf24da52ea060622e21521212824f33020908d2))
21
+ * **deps:** update dependency [@commitlint](https://github.com/commitlint)/cli to v19.6.0 ([9d9ab94](https://github.com/kelektiv/node-cron/commit/9d9ab94196e590b814c2693ff3fcbc7074eca4b4))
22
+ * **deps:** update dependency [@types](https://github.com/types)/node to v20.17.7 ([9181b6a](https://github.com/kelektiv/node-cron/commit/9181b6ac234bee70f3c426059645336610affa8b))
23
+ * **deps:** update dependency [@types](https://github.com/types)/node to v20.17.8 ([5899fc2](https://github.com/kelektiv/node-cron/commit/5899fc22c19fb7b95c0f3e812e2330db3e272e3c))
24
+ * **deps:** update dependency [@types](https://github.com/types)/node to v20.17.9 ([ca5065a](https://github.com/kelektiv/node-cron/commit/ca5065a4d784922feec0257e8ac999f5aa3a9667))
25
+ * **deps:** update dependency husky to v9.1.7 ([a960a29](https://github.com/kelektiv/node-cron/commit/a960a2927cf43f7b212b38aec290e0ad266b33c7))
26
+ * **deps:** update dependency typescript to v5.7.2 ([3447ff5](https://github.com/kelektiv/node-cron/commit/3447ff5f868981a70beeef804bff9139484f6d12))
27
+
1
28
  ## [3.2.1](https://github.com/kelektiv/node-cron/compare/v3.2.0...v3.2.1) (2024-11-12)
2
29
 
3
30
  ### 🛠 Builds
package/README.md CHANGED
@@ -194,6 +194,8 @@ day of week 0-7 (0 or 7 is Sunday, or use names)
194
194
 
195
195
  - `unrefTimeout`: [OPTIONAL] - Useful for controlling event loop behavior. More details [here](https://nodejs.org/api/timers.html#timers_timeout_unref).
196
196
 
197
+ - `waitForCompletion`: [OPTIONAL] - If `true`, no additional instances of the `onTick` callback function will run until the current onTick callback has completed. Any new scheduled executions that occur while the current callback is running will be skipped entirely. Default is `false`.
198
+
197
199
  #### Methods
198
200
 
199
201
  - `from` (static): Create a new CronJob object providing arguments as an object. See argument names and descriptions above.
@@ -214,6 +216,23 @@ day of week 0-7 (0 or 7 is Sunday, or use names)
214
216
 
215
217
  - `addCallback`: Permits addition of `onTick` callbacks.
216
218
 
219
+ #### Properties
220
+
221
+ - `isCallbackRunning`: [READ-ONLY] Indicates if a callback is currently executing.
222
+
223
+ ```javascript
224
+ const job = new CronJob('* * * * * *', async () => {
225
+ console.log(job.isCallbackRunning); // true during callback execution
226
+ await someAsyncTask();
227
+ console.log(job.isCallbackRunning); // still true until callback completes
228
+ });
229
+
230
+ console.log(job.isCallbackRunning); // false
231
+ job.start();
232
+ console.log(job.running); // true (schedule is active)
233
+ console.log(job.isCallbackRunning); // false (no callback executing)
234
+ ```
235
+
217
236
  ### CronTime Class
218
237
 
219
238
  #### Constructor
package/dist/job.d.ts CHANGED
@@ -8,18 +8,23 @@ export declare class CronJob<OC extends CronOnCompleteCommand | null = null, C =
8
8
  runOnce: boolean;
9
9
  context: CronContext<C>;
10
10
  onComplete?: WithOnComplete<OC> extends true ? CronOnCompleteCallback : undefined;
11
+ waitForCompletion: boolean;
12
+ private _isCallbackRunning;
11
13
  private _timeout?;
12
14
  private _callbacks;
13
- 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']);
14
- 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']);
15
+ get isCallbackRunning(): boolean;
16
+ 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']);
17
+ 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']);
15
18
  static from<OC extends CronOnCompleteCommand | null = null, C = null>(params: CronJobParams<OC, C>): CronJob<OC, C>;
16
19
  private _fnWrap;
17
20
  addCallback(callback: CronCallback<C, WithOnComplete<OC>>): void;
18
21
  setTime(time: CronTime): void;
19
22
  nextDate(): import("luxon").DateTime<boolean>;
20
- fireOnTick(): void;
23
+ fireOnTick(): Promise<void>;
21
24
  nextDates(i?: number): import("luxon").DateTime<boolean>[];
22
25
  start(): void;
23
26
  lastDate(): Date | null;
27
+ private _executeOnComplete;
28
+ private _waitForJobCompletion;
24
29
  stop(): void;
25
30
  }
package/dist/job.js CHANGED
@@ -1,17 +1,32 @@
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
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.CronJob = void 0;
4
13
  const child_process_1 = require("child_process");
5
14
  const errors_1 = require("./errors");
6
15
  const time_1 = require("./time");
7
16
  class CronJob {
8
- constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout) {
17
+ get isCallbackRunning() {
18
+ return this._isCallbackRunning;
19
+ }
20
+ constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout, waitForCompletion) {
9
21
  this.running = false;
10
22
  this.unrefTimeout = false;
11
23
  this.lastExecution = null;
12
24
  this.runOnce = false;
25
+ this.waitForCompletion = false;
26
+ this._isCallbackRunning = false;
13
27
  this._callbacks = [];
14
28
  this.context = (context !== null && context !== void 0 ? context : this);
29
+ this.waitForCompletion = Boolean(waitForCompletion);
15
30
  if (timeZone != null && utcOffset != null) {
16
31
  throw new errors_1.ExclusiveParametersError('timeZone', 'utcOffset');
17
32
  }
@@ -36,7 +51,7 @@ class CronJob {
36
51
  this.addCallback(this._fnWrap(onTick));
37
52
  if (runOnInit) {
38
53
  this.lastExecution = new Date();
39
- this.fireOnTick();
54
+ void this.fireOnTick();
40
55
  }
41
56
  if (start)
42
57
  this.start();
@@ -46,13 +61,13 @@ class CronJob {
46
61
  throw new errors_1.ExclusiveParametersError('timeZone', 'utcOffset');
47
62
  }
48
63
  if (params.timeZone != null) {
49
- return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, params.timeZone, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout);
64
+ return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, params.timeZone, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout, params.waitForCompletion);
50
65
  }
51
66
  else if (params.utcOffset != null) {
52
- return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, null, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout);
67
+ return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, null, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout, params.waitForCompletion);
53
68
  }
54
69
  else {
55
- return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, params.timeZone, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout);
70
+ return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, params.timeZone, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout, params.waitForCompletion);
56
71
  }
57
72
  }
58
73
  _fnWrap(cmd) {
@@ -91,17 +106,31 @@ class CronJob {
91
106
  return this.cronTime.sendAt();
92
107
  }
93
108
  fireOnTick() {
94
- for (const callback of this._callbacks) {
95
- void callback.call(this.context, this.onComplete);
96
- }
109
+ return __awaiter(this, void 0, void 0, function* () {
110
+ if (!this.waitForCompletion && this._isCallbackRunning)
111
+ return;
112
+ this._isCallbackRunning = true;
113
+ try {
114
+ for (const callback of this._callbacks) {
115
+ const result = callback.call(this.context, this.onComplete);
116
+ if (this.waitForCompletion)
117
+ yield result;
118
+ }
119
+ }
120
+ catch (error) {
121
+ console.error('[Cron] error in callback', error);
122
+ }
123
+ finally {
124
+ this._isCallbackRunning = false;
125
+ }
126
+ });
97
127
  }
98
128
  nextDates(i) {
99
129
  return this.cronTime.sendAt(i !== null && i !== void 0 ? i : 0);
100
130
  }
101
131
  start() {
102
- if (this.running) {
132
+ if (this.running)
103
133
  return;
104
- }
105
134
  const MAXDELAY = 2147483647;
106
135
  let timeout = this.cronTime.getTimeout();
107
136
  let remaining = 0;
@@ -136,10 +165,9 @@ class CronJob {
136
165
  else {
137
166
  this.lastExecution = new Date();
138
167
  this.running = false;
139
- if (!this.runOnce) {
168
+ if (!this.runOnce)
140
169
  this.start();
141
- }
142
- this.fireOnTick();
170
+ void this.fireOnTick();
143
171
  }
144
172
  };
145
173
  if (timeout >= 0) {
@@ -157,13 +185,37 @@ class CronJob {
157
185
  lastDate() {
158
186
  return this.lastExecution;
159
187
  }
188
+ _executeOnComplete() {
189
+ return __awaiter(this, void 0, void 0, function* () {
190
+ if (typeof this.onComplete !== 'function')
191
+ return;
192
+ try {
193
+ yield this.onComplete.call(this.context);
194
+ }
195
+ catch (error) {
196
+ console.error('[Cron] error in onComplete callback:', error);
197
+ }
198
+ });
199
+ }
200
+ _waitForJobCompletion() {
201
+ return __awaiter(this, void 0, void 0, function* () {
202
+ while (this._isCallbackRunning) {
203
+ yield new Promise(resolve => setTimeout(resolve, 100));
204
+ }
205
+ });
206
+ }
160
207
  stop() {
161
208
  if (this._timeout)
162
209
  clearTimeout(this._timeout);
163
210
  this.running = false;
164
- if (typeof this.onComplete === 'function') {
165
- void this.onComplete.call(this.context);
211
+ if (!this.waitForCompletion) {
212
+ void this._executeOnComplete();
213
+ return;
166
214
  }
215
+ void Promise.resolve().then(() => __awaiter(this, void 0, void 0, function* () {
216
+ yield this._waitForJobCompletion();
217
+ yield this._executeOnComplete();
218
+ }));
167
219
  }
168
220
  }
169
221
  exports.CronJob = CronJob;
@@ -11,6 +11,7 @@ interface BaseCronJobParams<OC extends CronOnCompleteCommand | null = null, C =
11
11
  context?: C;
12
12
  runOnInit?: boolean | null;
13
13
  unrefTimeout?: boolean | null;
14
+ waitForCompletion?: boolean | null;
14
15
  }
15
16
  export type CronJobParams<OC extends CronOnCompleteCommand | null = null, C = null> = BaseCronJobParams<OC, C> & ({
16
17
  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": "3.2.1",
4
+ "version": "3.3.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"
@@ -28,7 +28,7 @@
28
28
  "luxon": "~3.5.0"
29
29
  },
30
30
  "devDependencies": {
31
- "@commitlint/cli": "19.5.0",
31
+ "@commitlint/cli": "19.6.0",
32
32
  "@eslint/js": "^9.14.0",
33
33
  "@fast-check/jest": "2.0.3",
34
34
  "@insurgent/commitlint-config": "20.0.0",
@@ -40,20 +40,21 @@
40
40
  "@semantic-release/npm": "12.0.1",
41
41
  "@semantic-release/release-notes-generator": "14.0.1",
42
42
  "@types/jest": "29.5.14",
43
- "@types/node": "20.17.6",
43
+ "@types/node": "20.17.9",
44
44
  "@types/sinon": "17.0.3",
45
45
  "chai": "4.5.0",
46
46
  "eslint": "8.57.1",
47
47
  "eslint-config-prettier": "9.1.0",
48
48
  "eslint-plugin-jest": "27.9.0",
49
49
  "eslint-plugin-prettier": "5.2.1",
50
- "husky": "9.1.6",
50
+ "husky": "9.1.7",
51
51
  "jest": "29.7.0",
52
+ "lint-staged": "15.2.10",
52
53
  "prettier": "3.3.3",
53
54
  "semantic-release": "24.2.0",
54
55
  "sinon": "17.0.2",
55
56
  "ts-jest": "29.2.5",
56
- "typescript": "5.6.3",
57
+ "typescript": "5.7.2",
57
58
  "typescript-eslint": "^7.2.0"
58
59
  },
59
60
  "keywords": [
@@ -90,5 +91,9 @@
90
91
  "CHANGELOG.md",
91
92
  "LICENSE",
92
93
  "README.md"
93
- ]
94
+ ],
95
+ "lint-staged": {
96
+ "*.ts": "eslint src/ tests/ --fix",
97
+ "*.{json,md,yml}": "prettier ./**/*.{json,md,yml} --check --write"
98
+ }
94
99
  }