X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=c71ff297e2485151a89f470c3c9eb4712122a83a;hb=a9d9ea34ae4621d0babc235b10614c7c8c37d88b;hp=3a81e0f847f753ea55a7630d45a72fd08ec18a85;hpb=d2c1b9b8013ca6ab5a7d3a87dea1cc643a2efbc9;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 3a81e0f8..c71ff297 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -3,7 +3,11 @@ import type { Worker } from 'cluster' import type { MessagePort } from 'worker_threads' import type { MessageValue } from '../utility-types' import { EMPTY_FUNCTION } from '../utils' -import type { KillBehavior, WorkerOptions } from './worker-options' +import { + isKillBehavior, + type KillBehavior, + type WorkerOptions +} from './worker-options' import { KillBehaviors } from './worker-options' const DEFAULT_MAX_INACTIVE_TIME = 60000 @@ -24,7 +28,7 @@ export abstract class AbstractWorker< /** * Timestamp of the last task processed by this worker. */ - protected lastTaskTimestamp: number + protected lastTaskTimestamp!: number /** * Handler Id of the `aliveInterval` worker alive check. */ @@ -63,9 +67,8 @@ export abstract class AbstractWorker< this.opts = opts this.checkFunctionInput(fn) this.checkWorkerOptions(this.opts) - this.lastTaskTimestamp = Date.now() - // Keep the worker active - if (!isMain) { + if (!isMain && isKillBehavior(KillBehaviors.HARD, this.opts.killBehavior)) { + this.lastTaskTimestamp = Date.now() this.aliveInterval = setInterval( this.checkAlive.bind(this), (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2 @@ -179,7 +182,8 @@ export abstract class AbstractWorker< const err = this.handleError(e as Error) this.sendToMainWorker({ error: err, id: value.id }) } finally { - this.lastTaskTimestamp = Date.now() + isKillBehavior(KillBehaviors.HARD, this.opts.killBehavior) && + (this.lastTaskTimestamp = Date.now()) } } @@ -205,7 +209,8 @@ export abstract class AbstractWorker< this.sendToMainWorker({ error: err, id: value.id }) }) .finally(() => { - this.lastTaskTimestamp = Date.now() + isKillBehavior(KillBehaviors.HARD, this.opts.killBehavior) && + (this.lastTaskTimestamp = Date.now()) }) .catch(EMPTY_FUNCTION) }