diff --git a/src/classes/queue.ts b/src/classes/queue.ts index 82d609dcb6..f21310b437 100644 --- a/src/classes/queue.ts +++ b/src/classes/queue.ts @@ -196,6 +196,12 @@ export class Queue< opts?: JobsOptions, ): Promise> { if (opts && opts.repeat) { + if (opts.repeat.endDate) { + if (+new Date(opts.repeat.endDate) < Date.now()) { + throw new Error('End date must be greater than current timestamp'); + } + } + return (await this.repeat).addNextRepeatableJob< DataType, ResultType, diff --git a/tests/test_repeat.ts b/tests/test_repeat.ts index cec55869c7..d4a3f28ca0 100644 --- a/tests/test_repeat.ts +++ b/tests/test_repeat.ts @@ -114,6 +114,23 @@ describe('repeat', function () { }); }); + describe('when endDate is not greater than current timestamp', () => { + it('throws an error', async function () { + await expect( + queue.add( + 'test', + { foo: 'bar' }, + { + repeat: { + endDate: Date.now() - 1000, + every: 100, + }, + }, + ), + ).to.be.rejectedWith('End date must be greater than current timestamp'); + }); + }); + it('it should stop repeating after endDate', async function () { const every = 100; const date = new Date('2017-02-07 9:24:00');