Skip to content

Commit

Permalink
Check that the new nextRunAt is different that the previous nextRunAt (
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Rheault committed Dec 10, 2019
1 parent b8579dc commit 109e375
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/job/compute-next-run-at.js
Expand Up @@ -15,6 +15,7 @@ module.exports = function() {
const interval = this.attrs.repeatInterval;
const timezone = this.attrs.repeatTimezone;
const {repeatAt} = this.attrs;
const previousNextRunAt = this.attrs.nextRunAt || new Date();
this.attrs.nextRunAt = undefined;

const dateForTimezone = date => {
Expand All @@ -37,7 +38,7 @@ module.exports = function() {
try {
const cronTime = new CronTime(interval);
let nextDate = cronTime._getNextDateFrom(lastRun);
if (nextDate.valueOf() === lastRun.valueOf()) {
if (nextDate.valueOf() === lastRun.valueOf() || nextDate.valueOf() <= previousNextRunAt.valueOf()) {
// Handle cronTime giving back the same date for the next run time
nextDate = cronTime._getNextDateFrom(dateForTimezone(new Date(lastRun.valueOf() + 1000)));
}
Expand Down
16 changes: 16 additions & 0 deletions test/job.js
Expand Up @@ -223,6 +223,22 @@ describe('Job', () => {
expect(moment(job.attrs.nextRunAt).toDate().getDate()).to.be(moment(job.attrs.lastRunAt).add(1, 'days').toDate().getDate());
});

it('gives the correct nextDate when the lastRun is 1ms before the expected time', () => {
// (Issue #858): lastRunAt being 1ms before the nextRunAt makes cronTime return the same nextRunAt
const last = new Date();
last.setSeconds(59);
last.setMilliseconds(999);
const next = new Date(last.valueOf() + 1);
const expectedDate = new Date(next.valueOf() + 60000);
job.attrs.lastRunAt = last;
job.attrs.nextRunAt = next;
job.repeatEvery('* * * * *', {
timezone: 'GMT'
});
job.computeNextRunAt();
expect(job.attrs.nextRunAt.valueOf()).to.be(expectedDate.valueOf());
});

describe('when repeat at time is invalid', () => {
beforeEach(() => {
job.attrs.repeatAt = 'foo';
Expand Down

0 comments on commit 109e375

Please sign in to comment.