Skip to content

Commit

Permalink
fix: fix tasks stealing dynamic worker node handling
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
  • Loading branch information
jerome-benoit committed May 20, 2024
1 parent 51aa64a commit 2fe5023
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
7 changes: 5 additions & 2 deletions src/pools/abstract-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
) {
Expand Down
8 changes: 4 additions & 4 deletions src/worker/worker-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
*
Expand Down

0 comments on commit 2fe5023

Please sign in to comment.