Skip to content

Commit

Permalink
fix(worker): allow retrieving concurrency value (#2883) fixes #2880
Browse files Browse the repository at this point in the history
  • Loading branch information
fgozdz authored Nov 8, 2024
1 parent acadd32 commit 52f6317
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/classes/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export class Worker<
>>;
private blockingConnection: RedisConnection;
private blockUntil = 0;
private _concurrency: number;
private childPool: ChildPool;
private drained: boolean = false;
private extendLocksTimer: NodeJS.Timeout | null = null;
Expand Down Expand Up @@ -392,7 +393,11 @@ export class Worker<
) {
throw new Error('concurrency must be a finite number greater than 0');
}
this.opts.concurrency = concurrency;
this._concurrency = concurrency;
}

get concurrency() {
return this._concurrency;
}

get repeat(): Promise<Repeat> {
Expand Down Expand Up @@ -466,7 +471,7 @@ export class Worker<
*/
while (
!this.waiting &&
numTotal < this.opts.concurrency &&
numTotal < this._concurrency &&
(!this.limitUntil || numTotal == 0)
) {
const token = `${this.id}:${tokenPostfix++}`;
Expand Down Expand Up @@ -519,7 +524,7 @@ export class Worker<
this.processJob(
<Job<DataType, ResultType, NameType>>job,
token,
() => asyncFifoQueue.numTotal() <= this.opts.concurrency,
() => asyncFifoQueue.numTotal() <= this._concurrency,
jobsInProgress,
),
this.opts.runRetryDelay,
Expand Down
9 changes: 9 additions & 0 deletions tests/test_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4497,4 +4497,13 @@ describe('workers', function () {

await worker.close();
});

it('should retrieve concurrency from getter', async () => {
const worker = new Worker(queueName, async () => {}, { connection, concurrency: 100 });
worker.concurrency = 10;

expect(worker.concurrency).to.equal(10);

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

0 comments on commit 52f6317

Please sign in to comment.