X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=26468aeb96ae0d5f838518e2e9308b2e31c16c0e;hb=6372ea67bc24d9249a413159f168e39881626144;hp=bbf8f7065cf32656e2afd5c67b9b840fc0ff8c7b;hpb=fb41d7f7735b2e2b13874c11aeb55829f79f292f;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index bbf8f706..26468aeb 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -1,20 +1,20 @@ import { AsyncResource } from 'async_hooks' import type { Worker } from 'cluster' import type { MessagePort } from 'worker_threads' -import type { MessageValue, KillBehavior } from '../utility-types' -import type { WorkerOptions } from './worker-options' -import { killBehaviorEnumeration } from './worker-options' +import type { MessageValue } from '../utility-types' +import { EMPTY_FUNCTION } from '../utils' +import type { KillBehavior, WorkerOptions } from './worker-options' +import { KillBehaviors } from './worker-options' -const defaultMaxInactiveTime = 1000 * 60 -// TODO Shinigami92 to fix this and avoid that SOFT/HARD words are replicated so much times into the project -const defaultKillBehavior: KillBehavior = 'SOFT' +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, @@ -57,16 +57,17 @@ export abstract class AbstractWorker< fn: (data: Data) => Response, protected mainWorker?: MainWorker | null, public readonly opts: WorkerOptions = { - killBehavior: defaultKillBehavior, - maxInactiveTime: defaultMaxInactiveTime + killBehavior: DEFAULT_KILL_BEHAVIOR, + maxInactiveTime: DEFAULT_MAX_INACTIVE_TIME } ) { super(type) - this.killBehavior = this.opts.killBehavior ?? defaultKillBehavior - this.maxInactiveTime = this.opts.maxInactiveTime ?? defaultMaxInactiveTime + 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( @@ -96,6 +97,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. * @@ -147,10 +157,10 @@ export abstract class AbstractWorker< try { const res = fn(value.data) this.sendToMainWorker({ data: res, id: value.id }) - this.lastTask = Date.now() } catch (e) { const err = this.handleError(e) this.sendToMainWorker({ error: err, id: value.id }) + } finally { this.lastTask = Date.now() } } @@ -168,13 +178,15 @@ export abstract class AbstractWorker< fn(value.data) .then(res => { this.sendToMainWorker({ data: res, id: value.id }) - this.lastTask = Date.now() return null }) .catch(e => { const err = this.handleError(e) this.sendToMainWorker({ error: err, id: value.id }) + }) + .finally(() => { this.lastTask = Date.now() }) + .catch(EMPTY_FUNCTION) } }