+import type { Worker } from 'cluster'
+import type { MessagePort } from 'worker_threads'
+
/**
* Make all properties in T non-readonly
*/
/**
* Message object that is passed between worker and main worker.
*/
-export interface MessageValue<Data = unknown> {
+export interface MessageValue<
+ Data = unknown,
+ MainWorker extends Worker | MessagePort | unknown = unknown
+> {
/**
* Input data that will be passed to the worker.
*/
*
* _Only for internal use_
*/
- readonly parent?: MessagePort
+ readonly parent?: MainWorker
}
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'
* @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 {
* @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)
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),
)
this.checkAlive.bind(this)()
}
+
+ this.mainWorker?.on('message', (value: MessageValue<Data, MainWorker>) => {
+ 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.
* @param opts Options for the worker.
*/
public constructor (fn: (data: Data) => Response, opts: WorkerOptions = {}) {
- super('worker-cluster-pool:pioardi', isMaster, fn, opts)
-
- worker.on('message', (value: MessageValue<Data>) => {
- if (value?.data && value.id) {
- // here you will receive messages
- // console.log('This is the main worker ' + isMaster)
- 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.kill) {
- // here is time to kill this worker, just clearing the interval
- if (this.interval) clearInterval(this.interval)
- this.emitDestroy()
- }
- })
- }
-
- protected getMainWorker (): Worker {
- return worker
+ super('worker-cluster-pool:pioardi', isMaster, fn, worker, opts)
}
protected sendToMainWorker (message: MessageValue<Response>): void {
+import type { MessagePort } from 'worker_threads'
import { isMainThread, parentPort } from 'worker_threads'
import type { JSONValue, MessageValue } from '../utility-types'
import { AbstractWorker } from './abstract-worker'
Data extends JSONValue = JSONValue,
Response extends JSONValue = JSONValue
> extends AbstractWorker<MessagePort, Data, Response> {
- /**
- * Reference to main thread.
- */
- protected parent?: MessagePort
-
/**
* Constructs a new poolifier thread worker.
*
* @param opts Options for the worker.
*/
public constructor (fn: (data: Data) => Response, opts: WorkerOptions = {}) {
- super('worker-thread-pool:pioardi', isMainThread, fn, opts)
-
- parentPort?.on('message', (value: MessageValue<Data>) => {
- if (value?.data && value.id) {
- // here you will receive messages
- // console.log('This is the main worker ' + isMainThread)
- 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 the port to communicate with the main thread
- // this will be received once
- this.parent = 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()
- }
- })
- }
-
- protected getMainWorker (): MessagePort {
- if (!this.parent) {
- throw new Error('Parent was not set')
- }
- return this.parent
+ super('worker-thread-pool:pioardi', isMainThread, fn, parentPort, opts)
}
protected sendToMainWorker (message: MessageValue<Response>): void {