Skip to content

Commit

Permalink
fix(pattern): do not save offset when immediately is provided (#2756)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Sep 10, 2024
1 parent 3dc13d1 commit a8cb8a2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/classes/repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class Repeat extends QueueBase {
const hasImmediately = Boolean(
(every || pattern) && repeatOpts.immediately,
);
const offset = hasImmediately ? now - nextMillis : undefined;
const offset = (hasImmediately && every) ? now - nextMillis : undefined;
if (nextMillis) {
// We store the undecorated opts.jobId into the repeat options
if (!prevMillis && opts.jobId) {
Expand Down Expand Up @@ -340,7 +340,11 @@ export const getNextMillis = (
});

try {
return interval.next().getTime();
if(opts.immediately){
return new Date().getTime();
} else {
return interval.next().getTime();
}
} catch (e) {
// Ignore error
}
Expand Down
72 changes: 71 additions & 1 deletion tests/test_repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ describe('repeat', function () {
delayStub.restore();
});

it('should repeat once a day for 5 days and start immediately', async function () {
it('should repeat once a day for 5 days and start immediately using endDate', async function () {
this.timeout(8000);

const date = new Date('2017-05-05 01:01:00');
Expand Down Expand Up @@ -909,6 +909,76 @@ describe('repeat', function () {
delayStub.restore();
});

it('should repeat once a day for 5 days and start immediately', async function () {
this.timeout(8000);

const date = new Date('2017-05-05 01:01:00');
this.clock.setSystemTime(date);

const nextTick = ONE_DAY + 10 * ONE_SECOND;
const delay = 5 * ONE_SECOND + 500;

let counter = 0;
const worker = new Worker(
queueName,
async () => {
if(counter === 0){
this.clock.tick(6 * ONE_HOUR);

}else {
this.clock.tick(nextTick);
}
},
{
autorun: false,
connection,
prefix,
skipStalledCheck: true,
skipLockRenewal: true,
},
);
const delayStub = sinon.stub(worker, 'delay').callsFake(async () => {
console.log('delay');
});

let prev: Job;
const completing = new Promise<void>((resolve, reject) => {
worker.on('completed', async job => {
if (counter === 1) {
expect(prev.timestamp).to.be.lt(job.timestamp);
expect(job.timestamp - prev.timestamp).to.be.gte(delay);
} else if (prev) {
expect(prev.timestamp).to.be.lt(job.timestamp);
expect(job.timestamp - prev.timestamp).to.be.gte(ONE_DAY);
}
prev = job;

counter++;
if (counter == 5) {
resolve();
}
});
});

await queue.add(
'repeat',
{ foo: 'bar' },
{
repeat: {
pattern: '0 0 7 * * *',
immediately: true,
},
},
);
this.clock.tick(delay);

worker.run();

await completing;
await worker.close();
delayStub.restore();
});

it('should repeat once a day for 5 days', async function () {
this.timeout(8000);

Expand Down

0 comments on commit a8cb8a2

Please sign in to comment.