From: Jérôme Benoit Date: Mon, 20 May 2024 16:06:20 +0000 (+0200) Subject: fix: fix tasks stealing dynamic worker node handling X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=152e87a8c57c8cbd3a3b437106bcc79c30fcc720;p=poolifier.git fix: fix tasks stealing dynamic worker node handling Signed-off-by: Jérôme Benoit --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 2348d103..257f095b 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 tasks stealing dynamic worker node is not destroyed on inactivity. + ## [4.0.9] - 2024-05-19 ### Changed diff --git a/docs/api.md b/docs/api.md index 458017a1..28e6aad2 100644 --- a/docs/api.md +++ b/docs/api.md @@ -153,8 +153,8 @@ An object with these properties: `opts` (optional) An object with these properties: - `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. - **KillBehaviors.HARD**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker will be deleted. + **KillBehaviors.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. + **KillBehaviors.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. Default: `KillBehaviors.SOFT` diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 649d5f0d..95d3970f 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -1597,6 +1597,7 @@ 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 ( @@ -1605,6 +1606,8 @@ export abstract class AbstractPool< ((this.opts.enableTasksQueue === false && workerUsage.tasks.executing === 0) || (this.opts.enableTasksQueue === true && + workerInfo != null && + !workerInfo.stealing && workerUsage.tasks.executing === 0 && this.tasksQueueSize(localWorkerNodeKey) === 0))) ) { diff --git a/src/worker/worker-options.ts b/src/worker/worker-options.ts index 7f9c6506..50cf85bb 100644 --- a/src/worker/worker-options.ts +++ b/src/worker/worker-options.ts @@ -4,11 +4,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) @@ -30,8 +30,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. *