Skip to content

Commit

Permalink
fix(repeatable): validate endDate when adding next repeatable job
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored and manast committed Jul 21, 2021
1 parent 6774a2a commit 1324cbb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/classes/repeat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createHash } from 'crypto';
import { isUndefined } from 'lodash';
import { JobsOptions, RepeatOptions } from '../interfaces';
import { QueueBase } from './queue-base';
import { Job } from './job';
Expand All @@ -24,6 +25,14 @@ export class Repeat extends QueueBase {
}

let now = Date.now();

if (
!isUndefined(repeatOpts.endDate) &&
now > new Date(repeatOpts.endDate).getTime()
) {
return;
}

now = prevMillis < now ? now : prevMillis;

const nextMillis = getNextMillis(now, repeatOpts);
Expand Down
45 changes: 44 additions & 1 deletion src/test/test_repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('repeat', function() {
});

beforeEach(async function() {
queueName = 'test-' + v4();
queueName = `test-${v4()}`;
queue = new Queue(queueName);
repeat = new Repeat(queueName);
queueEvents = new QueueEvents(queueName);
Expand All @@ -47,6 +47,49 @@ describe('repeat', function() {
await removeAllQueueData(new IORedis(), queueName);
});

it('it should stop repeating after endDate', async function() {
const queueScheduler = new QueueScheduler(queueName);
await queueScheduler.waitUntilReady();
const every = 100;
const date = new Date('2017-02-07 9:24:00');
this.clock.setSystemTime(date);
const worker = new Worker(queueName, NoopProc);
await worker.waitUntilReady();

let processed = 0;
const completing = new Promise(resolve => {
worker.on('completed', async () => {
this.clock.tick(every);
processed++;
if (processed === 10) {
resolve();
}
});
});

await queue.add(
'test',
{ foo: 'bar' },
{
repeat: {
endDate: Date.now() + 1000,
every: 100,
},
},
);

this.clock.tick(every + 1);

await completing;
const delayed = await queue.getDelayed();

expect(delayed).to.have.length(0);
expect(processed).to.be.equal(10);

await queueScheduler.close();
await worker.close();
});

it('should create multiple jobs if they have the same cron pattern', async function() {
const cron = '*/10 * * * * *';

Expand Down

0 comments on commit 1324cbb

Please sign in to comment.