X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=34ab964c68d17c873b13149d2b228f570a9a6595;hb=d56f6cfde3b9eeb2a5e4341b91ec474580a0c07a;hp=fd163547a78eb08c7ca0c0a93519374530ed5c8f;hpb=729c563db85562dd7d0f7733b1a3e2d98467134b;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index fd163547..34ab964c 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -1,4 +1,6 @@ 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' @@ -10,7 +12,7 @@ import type { WorkerOptions } from './worker-options' * @template Response Type of response the worker sends back to the main worker. */ export abstract class AbstractWorker< - MainWorker, + MainWorker extends Worker | MessagePort, Data = unknown, Response = unknown > extends AsyncResource { @@ -37,12 +39,14 @@ export abstract class AbstractWorker< * @param type The type of async event. * @param isMain Whether this is the main worker or not. * @param fn Function processed by the worker when the pool's `execution` function is invoked. + * @param mainWorker Reference to main worker. * @param opts Options for the worker. */ public constructor ( type: string, isMain: boolean, fn: (data: Data) => Response, + protected mainWorker?: MainWorker | null, public readonly opts: WorkerOptions = {} ) { super(type) @@ -51,7 +55,7 @@ export abstract class AbstractWorker< this.async = !!this.opts.async this.lastTask = Date.now() if (!fn) throw new Error('fn parameter is mandatory') - // keep the worker active + // Keep the worker active if (!isMain) { this.interval = setInterval( this.checkAlive.bind(this), @@ -59,12 +63,38 @@ export abstract class AbstractWorker< ) this.checkAlive.bind(this)() } + + this.mainWorker?.on('message', (value: MessageValue) => { + if (value?.data && value.id) { + // Here you will receive messages + if (this.async) { + this.runInAsyncScope(this.runAsync.bind(this), this, fn, value) + } else { + this.runInAsyncScope(this.run.bind(this), this, fn, value) + } + } else if (value.parent) { + // Save a reference of the main worker to communicate with it + // This will be received once + this.mainWorker = value.parent + } else if (value.kill) { + // Here is time to kill this worker, just clearing the interval + if (this.interval) clearInterval(this.interval) + this.emitDestroy() + } + }) } /** * Returns the main worker. + * + * @returns Reference to the main worker. */ - protected abstract getMainWorker (): MainWorker + protected getMainWorker (): MainWorker { + if (!this.mainWorker) { + throw new Error('Main worker was not set') + } + return this.mainWorker + } /** * Send a message to the main worker. @@ -86,6 +116,7 @@ export abstract class AbstractWorker< * Handle an error and convert it to a string so it can be sent back to the main worker. * * @param e The error raised by the worker. + * @returns Message of the error. */ protected handleError (e: Error | string): string { return (e as unknown) as string