Skip to content

Commit

Permalink
fix problem caused by PR #248 - startAt set in future with interval
Browse files Browse the repository at this point in the history
  • Loading branch information
buzzinJohnnyBoi authored and Hexagon committed May 18, 2024
1 parent 99b4e0a commit 5e85325
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 60 deletions.
30 changes: 21 additions & 9 deletions dist/croner.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 21 additions & 9 deletions dist/croner.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/croner.min.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/croner.min.cjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/croner.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/croner.min.js.map

Large diffs are not rendered by default.

30 changes: 21 additions & 9 deletions dist/croner.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/croner.umd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/croner.umd.min.js.map

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 20 additions & 8 deletions src/croner.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,14 +500,7 @@ Cron.prototype._next = function (prev) {

// If no previous run, and startAt and interval is set, calculate when the last run should have been
if (!prev && this.options.startAt && this.options.interval) {
prev = this.options.startAt;
const now = new CronDate(undefined, this.options.timezone || this.options.utcOffset);
let prevTimePlusInterval = prev.getTime() + this.options.interval * 1000;
while (prevTimePlusInterval <= now.getTime()) {
prev = new CronDate(prev, this.options.timezone || this.options.utcOffset).increment(this._states.pattern, this.options, true);
prevTimePlusInterval = prev.getTime() + this.options.interval * 1000;
}
hasPreviousRun = true;
[prev, hasPreviousRun] = this._calculatePreviousRun(prev, hasPreviousRun);
}

// Ensure previous run is a CronDate
Expand Down Expand Up @@ -540,6 +533,25 @@ Cron.prototype._next = function (prev) {
return nextRun;
}
};
/**
* Calculate the previous run if no previous run is supplied, but startAt and interval are set.
* This calculation is only necessary if the startAt time is before the current time.
* Should only be called from the _next function.
* @private
**/
Cron.prototype._calculatePreviousRun = function (prev, hasPreviousRun) {
const now = new CronDate(undefined, this.options.timezone || this.options.utcOffset);
if (this.options.startAt.getTime() <= now.getTime()) {
prev = this.options.startAt;
let prevTimePlusInterval = prev.getTime() + this.options.interval * 1000;
while (prevTimePlusInterval <= now.getTime()) {
prev = new CronDate(prev, this.options.timezone || this.options.utcOffset).increment(this._states.pattern, this.options, true);
prevTimePlusInterval = prev.getTime() + this.options.interval * 1000;
}
hasPreviousRun = true;
}
return [prev, hasPreviousRun];
};

Cron.Cron = Cron;
Cron.scheduledJobs = scheduledJobs;
Expand Down
18 changes: 18 additions & 0 deletions test/node/js/src/suites/options.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,22 @@ module.exports = function (Cron, test) {
assert.equal(nextRun.getSeconds(), sixDaysFromNow.getSeconds());
});

test("Valid interval starting in the future should give correct start date", function () {

const now = new Date();

const tomorrow = new Date(now);
tomorrow.setDate(now.getDate() + 1);
tomorrow.setHours(0, 31, 2);

const nextRun = Cron("* * * * * *", { interval: 60 * 60 * 24 * 7, startAt: tomorrow.toISOString() }).nextRun();

assert.equal(nextRun.getFullYear(), tomorrow.getFullYear());
assert.equal(nextRun.getMonth(), tomorrow.getMonth());
assert.equal(nextRun.getDate(), tomorrow.getDate());
assert.equal(nextRun.getHours(), tomorrow.getHours());
assert.equal(nextRun.getMinutes(), tomorrow.getMinutes());
// The seconds are not checked because there will be no previous run, so CronDate.increment() will add 1 second to the nextRun
});

};
3 changes: 2 additions & 1 deletion types/croner.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ declare class Cron {
*/
nextRuns(n: number, previous?: Date | string): Date[];
/**
* Return the original pattern, it there was one
* Return the original pattern, if there was one
*
* @returns {string|undefined} - Original pattern
*/
Expand Down Expand Up @@ -138,6 +138,7 @@ declare class Cron {
public trigger(): Promise<void>;
private _checkTrigger;
private _next;
private _calculatePreviousRun;
}
declare namespace Cron {
export { Cron, scheduledJobs, TimePoint, CatchCallbackFn, ProtectCallbackFn, CronOptions, CronPatternPart, CronIndexOffset };
Expand Down
3 changes: 2 additions & 1 deletion types/croner.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class Cron {
*/
nextRuns(n: number, previous?: Date | string): Date[];
/**
* Return the original pattern, it there was one
* Return the original pattern, if there was one
*
* @returns {string|undefined} - Original pattern
*/
Expand Down Expand Up @@ -242,6 +242,7 @@ export class Cron {
public trigger(): Promise<void>;
private _checkTrigger;
private _next;
private _calculatePreviousRun;
}
export namespace Cron {
export { Cron };
Expand Down

0 comments on commit 5e85325

Please sign in to comment.