Skip to content
Permalink
Browse files

Check that the new nextRunAt is different that the previous nextRunAt (

  • Loading branch information
RaphaelRheault authored and koresar committed Dec 16, 2019
1 parent a80c940 commit 10c977329405a18cffac4fd41f1e65644487e983
Showing with 18 additions and 1 deletion.
  1. +2 −1 lib/job/compute-next-run-at.js
  2. +16 −0 test/job.js
@@ -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 => {
@@ -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)));
}
@@ -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';

0 comments on commit 10c9773

Please sign in to comment.
You can’t perform that action at this time.