From a6a2dc4c2e3b352d0b207aa652d8280ce8f640d4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 16 Jun 2023 12:37:28 +0200 Subject: [PATCH] fix: ensure no tasks are queued when trying to soft kill a dynamic worker MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 4 ++++ README.md | 4 ++-- src/pools/abstract-pool.ts | 12 ++++++++---- src/worker/worker-options.ts | 8 ++++---- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 581de285..8466e2ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Ensure no tasks are queued when trying to soft kill a dynamic worker. + ## [2.6.2] - 2023-06-12 ### Fixed diff --git a/README.md b/README.md index 140663c5..94b71d75 100644 --- a/README.md +++ b/README.md @@ -242,8 +242,8 @@ This method will call the terminate method on each worker. Default: `60000` - `killBehavior` (optional) - Dictates if your async unit (worker/process) will be deleted in case that a task is active on it. - **KillBehaviors.SOFT**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing, then the worker **won't** be deleted. - **KillBehaviors.HARD**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing, then the worker will be deleted. + **KillBehaviors.SOFT**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker **won't** be deleted. + **KillBehaviors.HARD**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker will be deleted. This option only apply to the newly created workers. Default: `KillBehaviors.SOFT` diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 6f5f3aeb..21cfb395 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -720,13 +720,17 @@ export abstract class AbstractPool< const currentWorkerNodeKey = this.getWorkerNodeKey(worker) if ( isKillBehavior(KillBehaviors.HARD, message.kill) || - (message.kill != null && + (this.opts.enableTasksQueue === false && + message.kill != null && this.workerNodes[currentWorkerNodeKey].workerUsage.tasks.executing === - 0) + 0) || + (this.opts.enableTasksQueue === true && + message.kill != null && + this.workerNodes[currentWorkerNodeKey].workerUsage.tasks.executing === + 0 && + this.tasksQueueSize(currentWorkerNodeKey) === 0) ) { // Kill message received from the worker: no new tasks are submitted to that worker for a while ( > maxInactiveTime) - this.flushTasksQueue(currentWorkerNodeKey) - // FIXME: wait for tasks to be finished void (this.destroyWorker(worker) as Promise) } }) diff --git a/src/worker/worker-options.ts b/src/worker/worker-options.ts index b71a90e5..90aea1ef 100644 --- a/src/worker/worker-options.ts +++ b/src/worker/worker-options.ts @@ -3,11 +3,11 @@ */ export const KillBehaviors = Object.freeze({ /** - * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing, then the worker **wont** be deleted. + * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker **wont** be deleted. */ SOFT: 'SOFT', /** - * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing, then the worker will be deleted. + * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker will be deleted. */ HARD: 'HARD' } as const) @@ -59,8 +59,8 @@ export interface WorkerOptions { /** * `killBehavior` dictates if your async unit (worker/process) will be deleted in case that a task is active on it. * - * - SOFT: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing, then the worker **won't** be deleted. - * - HARD: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing, then the worker will be deleted. + * - SOFT: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker **won't** be deleted. + * - HARD: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker will be deleted. * * This option only apply to the newly created workers. * -- 2.34.1