Comparing version 3.2.1 to 3.3.0
@@ -0,1 +1,28 @@ | ||
## [3.3.0](https://github.com/kelektiv/node-cron/compare/v3.2.1...v3.3.0) (2024-12-10) | ||
### β¨ Features | ||
* 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) | ||
### βοΈ Continuous Integrations | ||
* **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)) | ||
* **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)) | ||
* **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)) | ||
* 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) | ||
### β»οΈ Chores | ||
* **deps:** lock file maintenance ([94465ae](https://github.com/kelektiv/node-cron/commit/94465aed29609c20fc1f24b52547fb022782a164)) | ||
* **deps:** lock file maintenance ([23d67a4](https://github.com/kelektiv/node-cron/commit/23d67a4c5095ac96bb37ae2dae9b5a72b580aca4)) | ||
* **deps:** lock file maintenance ([135fdf7](https://github.com/kelektiv/node-cron/commit/135fdf7667ce5a4516dab975b1592fe43a7d2882)) | ||
* **deps:** lock file maintenance ([edcff3b](https://github.com/kelektiv/node-cron/commit/edcff3b87750057d82ec8df62770dad63af00d59)) | ||
* **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)) | ||
* **deps:** update dependency [@commitlint](https://github.com/commitlint)/cli to v19.6.0 ([9d9ab94](https://github.com/kelektiv/node-cron/commit/9d9ab94196e590b814c2693ff3fcbc7074eca4b4)) | ||
* **deps:** update dependency [@types](https://github.com/types)/node to v20.17.7 ([9181b6a](https://github.com/kelektiv/node-cron/commit/9181b6ac234bee70f3c426059645336610affa8b)) | ||
* **deps:** update dependency [@types](https://github.com/types)/node to v20.17.8 ([5899fc2](https://github.com/kelektiv/node-cron/commit/5899fc22c19fb7b95c0f3e812e2330db3e272e3c)) | ||
* **deps:** update dependency [@types](https://github.com/types)/node to v20.17.9 ([ca5065a](https://github.com/kelektiv/node-cron/commit/ca5065a4d784922feec0257e8ac999f5aa3a9667)) | ||
* **deps:** update dependency husky to v9.1.7 ([a960a29](https://github.com/kelektiv/node-cron/commit/a960a2927cf43f7b212b38aec290e0ad266b33c7)) | ||
* **deps:** update dependency typescript to v5.7.2 ([3447ff5](https://github.com/kelektiv/node-cron/commit/3447ff5f868981a70beeef804bff9139484f6d12)) | ||
## [3.2.1](https://github.com/kelektiv/node-cron/compare/v3.2.0...v3.2.1) (2024-11-12) | ||
@@ -2,0 +29,0 @@ |
@@ -11,6 +11,9 @@ import { CronTime } from './time'; | ||
onComplete?: WithOnComplete<OC> extends true ? CronOnCompleteCallback : undefined; | ||
waitForCompletion: boolean; | ||
private _isCallbackRunning; | ||
private _timeout?; | ||
private _callbacks; | ||
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']); | ||
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']); | ||
get isCallbackRunning(): boolean; | ||
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']); | ||
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']); | ||
static from<OC extends CronOnCompleteCommand | null = null, C = null>(params: CronJobParams<OC, C>): CronJob<OC, C>; | ||
@@ -21,7 +24,9 @@ private _fnWrap; | ||
nextDate(): import("luxon").DateTime<boolean>; | ||
fireOnTick(): void; | ||
fireOnTick(): Promise<void>; | ||
nextDates(i?: number): import("luxon").DateTime<boolean>[]; | ||
start(): void; | ||
lastDate(): Date | null; | ||
private _executeOnComplete; | ||
private _waitForJobCompletion; | ||
stop(): void; | ||
} |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -8,3 +17,6 @@ exports.CronJob = void 0; | ||
class CronJob { | ||
constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout) { | ||
get isCallbackRunning() { | ||
return this._isCallbackRunning; | ||
} | ||
constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout, waitForCompletion) { | ||
this.running = false; | ||
@@ -14,4 +26,7 @@ this.unrefTimeout = false; | ||
this.runOnce = false; | ||
this.waitForCompletion = false; | ||
this._isCallbackRunning = false; | ||
this._callbacks = []; | ||
this.context = (context !== null && context !== void 0 ? context : this); | ||
this.waitForCompletion = Boolean(waitForCompletion); | ||
if (timeZone != null && utcOffset != null) { | ||
@@ -41,3 +56,3 @@ throw new errors_1.ExclusiveParametersError('timeZone', 'utcOffset'); | ||
this.lastExecution = new Date(); | ||
this.fireOnTick(); | ||
void this.fireOnTick(); | ||
} | ||
@@ -52,9 +67,9 @@ if (start) | ||
if (params.timeZone != null) { | ||
return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, params.timeZone, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout); | ||
return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, params.timeZone, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout, params.waitForCompletion); | ||
} | ||
else if (params.utcOffset != null) { | ||
return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, null, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout); | ||
return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, null, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout, params.waitForCompletion); | ||
} | ||
else { | ||
return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, params.timeZone, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout); | ||
return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, params.timeZone, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout, params.waitForCompletion); | ||
} | ||
@@ -98,5 +113,20 @@ } | ||
fireOnTick() { | ||
for (const callback of this._callbacks) { | ||
void callback.call(this.context, this.onComplete); | ||
} | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.waitForCompletion && this._isCallbackRunning) | ||
return; | ||
this._isCallbackRunning = true; | ||
try { | ||
for (const callback of this._callbacks) { | ||
const result = callback.call(this.context, this.onComplete); | ||
if (this.waitForCompletion) | ||
yield result; | ||
} | ||
} | ||
catch (error) { | ||
console.error('[Cron] error in callback', error); | ||
} | ||
finally { | ||
this._isCallbackRunning = false; | ||
} | ||
}); | ||
} | ||
@@ -107,5 +137,4 @@ nextDates(i) { | ||
start() { | ||
if (this.running) { | ||
if (this.running) | ||
return; | ||
} | ||
const MAXDELAY = 2147483647; | ||
@@ -145,6 +174,5 @@ let timeout = this.cronTime.getTimeout(); | ||
this.running = false; | ||
if (!this.runOnce) { | ||
if (!this.runOnce) | ||
this.start(); | ||
} | ||
this.fireOnTick(); | ||
void this.fireOnTick(); | ||
} | ||
@@ -167,2 +195,21 @@ }; | ||
} | ||
_executeOnComplete() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (typeof this.onComplete !== 'function') | ||
return; | ||
try { | ||
yield this.onComplete.call(this.context); | ||
} | ||
catch (error) { | ||
console.error('[Cron] error in onComplete callback:', error); | ||
} | ||
}); | ||
} | ||
_waitForJobCompletion() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
while (this._isCallbackRunning) { | ||
yield new Promise(resolve => setTimeout(resolve, 100)); | ||
} | ||
}); | ||
} | ||
stop() { | ||
@@ -172,5 +219,10 @@ if (this._timeout) | ||
this.running = false; | ||
if (typeof this.onComplete === 'function') { | ||
void this.onComplete.call(this.context); | ||
if (!this.waitForCompletion) { | ||
void this._executeOnComplete(); | ||
return; | ||
} | ||
void Promise.resolve().then(() => __awaiter(this, void 0, void 0, function* () { | ||
yield this._waitForJobCompletion(); | ||
yield this._executeOnComplete(); | ||
})); | ||
} | ||
@@ -177,0 +229,0 @@ } |
@@ -14,2 +14,3 @@ import { SpawnOptions } from 'child_process'; | ||
unrefTimeout?: boolean | null; | ||
waitForCompletion?: boolean | null; | ||
} | ||
@@ -16,0 +17,0 @@ export type CronJobParams<OC extends CronOnCompleteCommand | null = null, C = null> = BaseCronJobParams<OC, C> & ({ |
{ | ||
"name": "cron", | ||
"description": "Cron jobs for your node", | ||
"version": "3.2.1", | ||
"version": "3.3.0", | ||
"author": "Nick Campbell <nicholas.j.campbell@gmail.com> (https://github.com/ncb000gt)", | ||
@@ -31,3 +31,3 @@ "bugs": { | ||
"devDependencies": { | ||
"@commitlint/cli": "19.5.0", | ||
"@commitlint/cli": "19.6.0", | ||
"@eslint/js": "^9.14.0", | ||
@@ -44,3 +44,3 @@ "@fast-check/jest": "2.0.3", | ||
"@types/jest": "29.5.14", | ||
"@types/node": "20.17.6", | ||
"@types/node": "20.17.9", | ||
"@types/sinon": "17.0.3", | ||
@@ -52,4 +52,5 @@ "chai": "4.5.0", | ||
"eslint-plugin-prettier": "5.2.1", | ||
"husky": "9.1.6", | ||
"husky": "9.1.7", | ||
"jest": "29.7.0", | ||
"lint-staged": "15.2.10", | ||
"prettier": "3.3.3", | ||
@@ -59,3 +60,3 @@ "semantic-release": "24.2.0", | ||
"ts-jest": "29.2.5", | ||
"typescript": "5.6.3", | ||
"typescript": "5.7.2", | ||
"typescript-eslint": "^7.2.0" | ||
@@ -97,3 +98,7 @@ }, | ||
"README.md" | ||
] | ||
], | ||
"lint-staged": { | ||
"*.ts": "eslint src/ tests/ --fix", | ||
"*.{json,md,yml}": "prettier ./**/*.{json,md,yml} --check --write" | ||
} | ||
} |
@@ -197,2 +197,4 @@ <p align="center"> | ||
- `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`. | ||
#### Methods | ||
@@ -218,2 +220,19 @@ | ||
#### Properties | ||
- `isCallbackRunning`: [READ-ONLY] Indicates if a callback is currently executing. | ||
```javascript | ||
const job = new CronJob('* * * * * *', async () => { | ||
console.log(job.isCallbackRunning); // true during callback execution | ||
await someAsyncTask(); | ||
console.log(job.isCallbackRunning); // still true until callback completes | ||
}); | ||
console.log(job.isCallbackRunning); // false | ||
job.start(); | ||
console.log(job.running); // true (schedule is active) | ||
console.log(job.isCallbackRunning); // false (no callback executing) | ||
``` | ||
### CronTime Class | ||
@@ -220,0 +239,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
120090
981
269
28
6
1