From 12ef7be90d9db138b69246d359aec11660ebd270 Mon Sep 17 00:00:00 2001 From: Manuel Astudillo Date: Sat, 16 Sep 2023 13:09:18 +0200 Subject: [PATCH 1/2] fix(worker): throw exception with NaN as concurrency --- src/classes/worker.ts | 12 ++++++++---- tests/test_worker.ts | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/classes/worker.ts b/src/classes/worker.ts index 5ddcda94f4..da5948999a 100644 --- a/src/classes/worker.ts +++ b/src/classes/worker.ts @@ -350,8 +350,12 @@ export class Worker< } set concurrency(concurrency: number) { - if (typeof concurrency !== 'number' || concurrency < 1) { - throw new Error('concurrency must be a number greater than 0'); + if ( + typeof concurrency !== 'number' || + concurrency < 1 || + !isFinite(concurrency) + ) { + throw new Error('concurrency must be a finite number greater than 0'); } this.opts.concurrency = concurrency; } @@ -564,8 +568,6 @@ export class Worker< if (!this.closing) { await this.delay(); } - } finally { - this.waiting = null; } } @@ -595,6 +597,8 @@ export class Worker< } } + // Update limitUntil and delayUntil + // TODO: Refactor out of this function this.limitUntil = Math.max(limitUntil, 0) || 0; if (delayUntil) { this.blockUntil = Math.max(delayUntil, 0) || 0; diff --git a/tests/test_worker.ts b/tests/test_worker.ts index 26a68c9173..315b22dfa8 100644 --- a/tests/test_worker.ts +++ b/tests/test_worker.ts @@ -1631,7 +1631,21 @@ describe('workers', function () { throw new Error('Should have thrown an exception'); } catch (err) { expect(err.message).to.be.equal( - 'concurrency must be a number greater than 0', + 'concurrency must be a finite number greater than 0', + ); + } + }); + + it('should thrown an exception if I specify a NaN concurrency', () => { + try { + const worker = new Worker(queueName, async () => {}, { + connection, + concurrency: NaN, + }); + throw new Error('Should have thrown an exception'); + } catch (err) { + expect(err.message).to.be.equal( + 'concurrency must be a finite number greater than 0', ); } }); From 762d1ff3477b3d6c4ebe2343b7390d0563c2ff9d Mon Sep 17 00:00:00 2001 From: Manuel Astudillo Date: Wed, 20 Sep 2023 15:21:36 +0200 Subject: [PATCH 2/2] chore(worker): re-add wrongly removed code --- src/classes/worker.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/classes/worker.ts b/src/classes/worker.ts index da5948999a..c711ab68d0 100644 --- a/src/classes/worker.ts +++ b/src/classes/worker.ts @@ -568,6 +568,8 @@ export class Worker< if (!this.closing) { await this.delay(); } + } finally { + this.waiting = null; } }