From 2fe5023c390090a836a911b27704226e04f060ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 20 May 2024 17:49:38 +0200 Subject: [PATCH] fix: fix tasks stealing dynamic worker node handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 4 ++++ docs/api.md | 8 ++++---- src/pools/abstract-pool.ts | 7 +++++-- src/worker/worker-options.ts | 8 ++++---- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aba21e93..5d07e541 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to ## [Unreleased] +### Fixed + +- Ensure tasks stealing dynamic worker node is not destroyed on inactivity. + ## [0.4.5] - 2024-05-15 ### Fixed diff --git a/docs/api.md b/docs/api.md index a7cf846c..47e98525 100644 --- a/docs/api.md +++ b/docs/api.md @@ -217,11 +217,11 @@ levels.\ - `killBehavior` (optional) - Dictates if your worker will be deleted in case a task is active on it.\ **KillBehaviors.SOFT**: If `currentTime - lastActiveTime` is greater than - `maxInactiveTime` but a task is still executing or queued, then the worker - **won't** be deleted.\ + `maxInactiveTime` but the worker is stealing tasks or a task is 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.\ + `maxInactiveTime` but the worker is stealing tasks or a task is 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 ad985451..45534c86 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -1477,15 +1477,18 @@ export abstract class AbstractPool< const localWorkerNodeKey = this.getWorkerNodeKeyByWorkerId( message.workerId, ) + const workerInfo = this.getWorkerInfo(localWorkerNodeKey) const workerUsage = this.workerNodes[localWorkerNodeKey]?.usage // Kill message received from worker if ( isKillBehavior(KillBehaviors.HARD, message.kill) || - (isKillBehavior(KillBehaviors.SOFT, message.kill) && - workerUsage != null && + (workerUsage != null && + isKillBehavior(KillBehaviors.SOFT, message.kill) && ((this.opts.enableTasksQueue === false && workerUsage.tasks.executing === 0) || (this.opts.enableTasksQueue === true && + workerInfo != null && + workerInfo.stealing === false && workerUsage.tasks.executing === 0 && this.tasksQueueSize(localWorkerNodeKey) === 0))) ) { diff --git a/src/worker/worker-options.ts b/src/worker/worker-options.ts index 0025f05a..81060b2b 100644 --- a/src/worker/worker-options.ts +++ b/src/worker/worker-options.ts @@ -5,11 +5,11 @@ export const KillBehaviors: Readonly<{ SOFT: 'SOFT'; HARD: 'HARD' }> = Object .freeze( { /** - * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker **wont** be deleted. + * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but the worker is stealing tasks or a task is executing or queued, then the worker **wont** be deleted. */ SOFT: 'SOFT', /** - * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker will be deleted. + * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but the worker is stealing tasks or a task is executing or queued, then the worker will be deleted. */ HARD: 'HARD', } as const, @@ -32,8 +32,8 @@ export interface WorkerOptions { /** * `killBehavior` dictates if your worker will be deleted in case a task is active on it. * - * - 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. + * - SOFT: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but the worker is stealing tasks or a task is executing or queued, then the worker **won't** be deleted. + * - HARD: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but the worker is stealing tasks or a task is executing or queued, then the worker will be deleted. * * This option only apply to the newly created workers. *