From 5bd79900ea3ace8ec6aa00525aff81a345f8e18e Mon Sep 17 00:00:00 2001 From: Rogger Valverde Date: Thu, 23 May 2024 22:07:50 -0600 Subject: [PATCH] fix(repeat): throw error when endDate is pointing to the past (#2574) --- src/classes/queue.ts | 6 ++++++ tests/test_repeat.ts | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) 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');