X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=4f741b47acf134146069e33d866b8f40a09e0bb1;hb=b98ec2e690d5ba33a13a2848b0f1315b885c2f4b;hp=34ab964c68d17c873b13149d2b228f570a9a6595;hpb=d56f6cfde3b9eeb2a5e4341b91ec474580a0c07a;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 34ab964c..4f741b47 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -2,14 +2,18 @@ import { AsyncResource } from 'async_hooks' import type { Worker } from 'cluster' import type { MessagePort } from 'worker_threads' import type { MessageValue } from '../utility-types' -import type { WorkerOptions } from './worker-options' +import type { KillBehavior, WorkerOptions } from './worker-options' +import { KillBehaviors } from './worker-options' + +const DEFAULT_MAX_INACTIVE_TIME = 1000 * 60 +const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT /** * Base class containing some shared logic for all poolifier workers. * * @template MainWorker Type of main worker. - * @template Data Type of data this worker receives from pool's execution. - * @template Response Type of response the worker sends back to the main worker. + * @template Data Type of data this worker receives from pool's execution. This can only be serializable data. + * @template Response Type of response the worker sends back to the main worker. This can only be serializable data. */ export abstract class AbstractWorker< MainWorker extends Worker | MessagePort, @@ -20,6 +24,10 @@ export abstract class AbstractWorker< * The maximum time to keep this worker alive while idle. The pool automatically checks and terminates this worker when the time expires. */ protected readonly maxInactiveTime: number + /** + * The kill behavior set as option on the Worker constructor or a default value. + */ + protected readonly killBehavior: KillBehavior /** * Whether the worker is working asynchronously or not. */ @@ -47,14 +55,18 @@ export abstract class AbstractWorker< isMain: boolean, fn: (data: Data) => Response, protected mainWorker?: MainWorker | null, - public readonly opts: WorkerOptions = {} + public readonly opts: WorkerOptions = { + killBehavior: DEFAULT_KILL_BEHAVIOR, + maxInactiveTime: DEFAULT_MAX_INACTIVE_TIME + } ) { super(type) - - this.maxInactiveTime = this.opts.maxInactiveTime ?? 1000 * 60 + this.killBehavior = this.opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR + this.maxInactiveTime = + this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME this.async = !!this.opts.async this.lastTask = Date.now() - if (!fn) throw new Error('fn parameter is mandatory') + this.checkFunctionInput(fn) // Keep the worker active if (!isMain) { this.interval = setInterval( @@ -84,6 +96,15 @@ export abstract class AbstractWorker< }) } + /** + * Check if the `fn` parameter is passed to the constructor. + * + * @param fn The function that should be defined. + */ + private checkFunctionInput (fn: (data: Data) => Response): void { + if (!fn) throw new Error('fn parameter is mandatory') + } + /** * Returns the main worker. * @@ -108,7 +129,7 @@ export abstract class AbstractWorker< */ protected checkAlive (): void { if (Date.now() - this.lastTask > this.maxInactiveTime) { - this.sendToMainWorker({ kill: 1 }) + this.sendToMainWorker({ kill: this.killBehavior }) } }