X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=bf81a9f2432ff81606f7c10536c6ce338f16f066;hb=79aafe9fe0d0c4c40da43337b0a8c358f5f834ce;hp=1f42a90bcf073e514a0acf2eec4fa4f04fdb0aea;hpb=fc3e65861bc1939ae047ee1e8e91a1ce577035f4;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 1f42a90b..bf81a9f2 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -3,11 +3,7 @@ import type { Worker } from 'node:cluster' import type { MessagePort } from 'node:worker_threads' import type { MessageValue } from '../utility-types' import { EMPTY_FUNCTION } from '../utils' -import { - isKillBehavior, - type KillBehavior, - type WorkerOptions -} from './worker-options' +import { type KillBehavior, type WorkerOptions } from './worker-options' import { KillBehaviors } from './worker-options' const DEFAULT_MAX_INACTIVE_TIME = 60000 @@ -48,7 +44,7 @@ export abstract class AbstractWorker< */ public constructor ( type: string, - isMain: boolean, + protected readonly isMain: boolean, fn: (data: Data) => Response, protected mainWorker: MainWorker | undefined | null, opts: WorkerOptions = { @@ -67,7 +63,7 @@ export abstract class AbstractWorker< this.opts = opts this.checkFunctionInput(fn) this.checkWorkerOptions(this.opts) - if (!isMain && isKillBehavior(KillBehaviors.HARD, this.opts.killBehavior)) { + if (!this.isMain) { this.lastTaskTimestamp = Date.now() this.aliveInterval = setInterval( this.checkAlive.bind(this), @@ -98,7 +94,7 @@ export abstract class AbstractWorker< this.mainWorker = value.parent } else if (value.kill !== undefined) { // Here is time to kill this worker, just clearing the interval - if (this.aliveInterval != null) clearInterval(this.aliveInterval) + this.aliveInterval != null && clearInterval(this.aliveInterval) this.emitDestroy() } } @@ -182,8 +178,7 @@ export abstract class AbstractWorker< const err = this.handleError(e as Error) this.sendToMainWorker({ error: err, id: value.id }) } finally { - isKillBehavior(KillBehaviors.HARD, this.opts.killBehavior) && - (this.lastTaskTimestamp = Date.now()) + !this.isMain && (this.lastTaskTimestamp = Date.now()) } } @@ -209,8 +204,7 @@ export abstract class AbstractWorker< this.sendToMainWorker({ error: err, id: value.id }) }) .finally(() => { - isKillBehavior(KillBehaviors.HARD, this.opts.killBehavior) && - (this.lastTaskTimestamp = Date.now()) + !this.isMain && (this.lastTaskTimestamp = Date.now()) }) .catch(EMPTY_FUNCTION) }