X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fthread-worker.ts;h=638de2ba2f9c8f090360c528cd1169cfcd52e823;hb=eceaf36f69c97b0376194a849e77f6ea15fb8f7b;hp=b6573a974cc653656fb6a968b0437e57eeed7dd6;hpb=e102732c0e3966b81834b2c0bdd087eb051162ad;p=poolifier.git diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index b6573a97..638de2ba 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -1,8 +1,13 @@ -import { type MessagePort, isMainThread, parentPort } from 'node:worker_threads' -import type { MessageValue } from '../utility-types' -import { AbstractWorker } from './abstract-worker' -import type { WorkerOptions } from './worker-options' -import type { TaskFunctions, WorkerFunction } from './worker-functions' +import { + type MessagePort, + isMainThread, + parentPort, + threadId +} from 'node:worker_threads' +import type { MessageValue } from '../utility-types.js' +import { AbstractWorker } from './abstract-worker.js' +import type { WorkerOptions } from './worker-options.js' +import type { TaskFunction, TaskFunctions } from './task-functions.js' /** * A thread worker used by a poolifier `ThreadPool`. @@ -22,6 +27,11 @@ export class ThreadWorker< Data = unknown, Response = unknown > extends AbstractWorker { + /** + * Message port used to communicate with the main worker. + */ + private port?: MessagePort + /** * Constructs a new poolifier thread worker. * @@ -29,22 +39,62 @@ export class ThreadWorker< * @param opts - Options for the worker. */ public constructor ( - taskFunctions: - | WorkerFunction - | TaskFunctions, + taskFunctions: TaskFunction | TaskFunctions, opts: WorkerOptions = {} ) { - super( - 'worker-thread-pool:poolifier', - isMainThread, - taskFunctions, - parentPort as MessagePort, - opts - ) + super(isMainThread, parentPort, taskFunctions, opts) + } + + /** @inheritDoc */ + protected handleReadyMessage (message: MessageValue): void { + if ( + message.workerId === this.id && + message.ready === false && + message.port != null + ) { + try { + this.port = message.port + this.port.on('message', this.messageListener.bind(this)) + this.sendToMainWorker({ + ready: true, + taskFunctionNames: this.listTaskFunctionNames() + }) + } catch { + this.sendToMainWorker({ + ready: false, + taskFunctionNames: this.listTaskFunctionNames() + }) + } + } + } + + /** @inheritDoc */ + protected handleKillMessage (message: MessageValue): void { + super.handleKillMessage(message) + this.port?.unref() + this.port?.close() + } + + /** @inheritDoc */ + protected get id (): number { + return threadId } /** @inheritDoc */ - protected sendToMainWorker (message: MessageValue): void { - this.getMainWorker().postMessage(message) + protected readonly sendToMainWorker = ( + message: MessageValue + ): void => { + this.port?.postMessage({ + ...message, + workerId: this.id + } satisfies MessageValue) + } + + /** + * @inheritDoc + * @override + */ + protected handleError (error: Error | string): string { + return error as string } }