Skip to content

Commit

Permalink
fix: attempt to fix kelektiv#805
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox committed Dec 27, 2024
1 parent dfcbd3c commit 187dbe5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
Perfect for tasks like data backups, notifications, and many more!
</p>

> **⚠️ WARNING**
>
> This is a WIP released as beta for testing purposes.
> For more information, please refer to [issue #805](https://github.com/kelektiv/node-cron/issues/805).
# Cron for Node.js

[![Version](https://img.shields.io/npm/v/cron?label=version&logo=npm)](https://www.npmjs.com/package/cron)
Expand Down
28 changes: 27 additions & 1 deletion src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,33 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {

setCronTimeout(timeout);
} else {
this.stop();
// WARNING: this is a WIP released as beta for testing purposes
// for more info, please refer to https://github.com/kelektiv/node-cron/issues/805
// probable next steps:
// - throw an error when stopping the job
// - turn delayed execution into an opt-in option

// We have missed the correct point in time.
if (timeout <= 1000) {
console.warn(`[CRON DEBUG] Executing job with a delay: ${timeout}ms`);

this.lastExecution = new Date();

this.running = false;

// start before calling back so the callbacks have the ability to stop the cron job
if (!this.runOnce) this.start();

void this.fireOnTick();
} else {
console.error(
`[CRON DEBUG] Stopping job due to delay being higher than 1s: ${timeout}ms`
);
this.stop();
}

console.debug(`[CRON DEBUG] Current date: ${new Date().toISOString()}`);
console.debug(`[CRON DEBUG] Source: ${String(this.cronTime.source)}`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class CronTime {
* Get the number of milliseconds in the future at which to fire our callbacks.
*/
getTimeout() {
return Math.max(-1, this.sendAt().toMillis() - DateTime.local().toMillis());
return this.sendAt().toMillis() - DateTime.local().toMillis();
}

/**
Expand Down

0 comments on commit 187dbe5

Please sign in to comment.