1 import { AsyncResource
} from
'node:async_hooks'
2 import type { Worker
} from
'node:cluster'
3 import type { MessagePort
} from
'node:worker_threads'
4 import { performance
} from
'node:perf_hooks'
10 } from
'../utility-types'
17 import { KillBehaviors
, type WorkerOptions
} from
'./worker-options'
23 } from
'./task-functions'
25 const DEFAULT_MAX_INACTIVE_TIME
= 60000
26 const DEFAULT_WORKER_OPTIONS
: WorkerOptions
= {
28 * The kill behavior option on this worker or its default value.
30 killBehavior
: KillBehaviors
.SOFT
,
32 * The maximum time to keep this worker active while idle.
33 * The pool automatically checks and terminates this worker when the time expires.
35 maxInactiveTime
: DEFAULT_MAX_INACTIVE_TIME
,
37 * The function to call when the worker is killed.
39 killHandler
: EMPTY_FUNCTION
43 * Base class that implements some shared logic for all poolifier workers.
45 * @typeParam MainWorker - Type of main worker.
46 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
47 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
49 export abstract class AbstractWorker
<
50 MainWorker
extends Worker
| MessagePort
,
53 > extends AsyncResource
{
57 protected abstract id
: number
59 * Task function(s) processed by the worker when the pool's `execution` function is invoked.
61 protected taskFunctions
!: Map
<string, TaskFunction
<Data
, Response
>>
63 * Timestamp of the last task processed by this worker.
65 protected lastTaskTimestamp
!: number
67 * Performance statistics computation requirements.
69 protected statistics
!: WorkerStatistics
71 * Handler id of the `activeInterval` worker activity check.
73 protected activeInterval
?: NodeJS
.Timeout
75 * Constructs a new poolifier worker.
77 * @param type - The type of async event.
78 * @param isMain - Whether this is the main worker or not.
79 * @param mainWorker - Reference to main worker.
80 * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function.
81 * @param opts - Options for the worker.
85 protected readonly isMain
: boolean,
86 private readonly mainWorker
: MainWorker
,
87 taskFunctions
: TaskFunction
<Data
, Response
> | TaskFunctions
<Data
, Response
>,
88 protected opts
: WorkerOptions
= DEFAULT_WORKER_OPTIONS
91 this.checkWorkerOptions(this.opts
)
92 this.checkTaskFunctions(taskFunctions
)
94 this.getMainWorker()?.on('message', this.handleReadyMessage
.bind(this))
98 private checkWorkerOptions (opts
: WorkerOptions
): void {
99 this.opts
= { ...DEFAULT_WORKER_OPTIONS
, ...opts
}
100 delete this.opts
.async
104 * Checks if the `taskFunctions` parameter is passed to the constructor.
106 * @param taskFunctions - The task function(s) parameter that should be checked.
108 private checkTaskFunctions (
109 taskFunctions
: TaskFunction
<Data
, Response
> | TaskFunctions
<Data
, Response
>
111 if (taskFunctions
== null) {
112 throw new Error('taskFunctions parameter is mandatory')
114 this.taskFunctions
= new Map
<string, TaskFunction
<Data
, Response
>>()
115 if (typeof taskFunctions
=== 'function') {
116 const boundFn
= taskFunctions
.bind(this)
117 this.taskFunctions
.set(DEFAULT_TASK_NAME
, boundFn
)
118 this.taskFunctions
.set(
119 typeof taskFunctions
.name
=== 'string' &&
120 taskFunctions
.name
.trim().length
> 0
125 } else if (isPlainObject(taskFunctions
)) {
126 let firstEntry
= true
127 for (const [name
, fn
] of Object.entries(taskFunctions
)) {
128 if (typeof name
!== 'string') {
130 'A taskFunctions parameter object key is not a string'
133 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
135 'A taskFunctions parameter object key an empty string'
138 if (typeof fn
!== 'function') {
140 'A taskFunctions parameter object value is not a function'
143 const boundFn
= fn
.bind(this)
145 this.taskFunctions
.set(DEFAULT_TASK_NAME
, boundFn
)
148 this.taskFunctions
.set(name
, boundFn
)
151 throw new Error('taskFunctions parameter object is empty')
155 'taskFunctions parameter is not a function or a plain object'
161 * Checks if the worker has a task function with the given name.
163 * @param name - The name of the task function to check.
164 * @returns Whether the worker has a task function with the given name or not.
165 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
167 public hasTaskFunction (name
: string): boolean {
168 this.checkTaskFunctionName(name
)
169 return this.taskFunctions
.has(name
)
173 * Adds a task function to the worker.
174 * If a task function with the same name already exists, it is replaced.
176 * @param name - The name of the task function to add.
177 * @param fn - The task function to add.
178 * @returns Whether the task function was added or not.
179 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
180 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
181 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `fn` parameter is not a function.
183 public addTaskFunction (
185 fn
: TaskFunction
<Data
, Response
>
187 this.checkTaskFunctionName(name
)
188 if (name
=== DEFAULT_TASK_NAME
) {
190 'Cannot add a task function with the default reserved name'
193 if (typeof fn
!== 'function') {
194 throw new TypeError('fn parameter is not a function')
197 const boundFn
= fn
.bind(this)
199 this.taskFunctions
.get(name
) ===
200 this.taskFunctions
.get(DEFAULT_TASK_NAME
)
202 this.taskFunctions
.set(DEFAULT_TASK_NAME
, boundFn
)
204 this.taskFunctions
.set(name
, boundFn
)
205 this.sendTaskFunctionsListToMainWorker()
213 * Removes a task function from the worker.
215 * @param name - The name of the task function to remove.
216 * @returns Whether the task function existed and was removed or not.
217 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
218 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
219 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the task function used as default task function.
221 public removeTaskFunction (name
: string): boolean {
222 this.checkTaskFunctionName(name
)
223 if (name
=== DEFAULT_TASK_NAME
) {
225 'Cannot remove the task function with the default reserved name'
229 this.taskFunctions
.get(name
) === this.taskFunctions
.get(DEFAULT_TASK_NAME
)
232 'Cannot remove the task function used as the default task function'
235 const deleteStatus
= this.taskFunctions
.delete(name
)
236 this.sendTaskFunctionsListToMainWorker()
241 * Lists the names of the worker's task functions.
243 * @returns The names of the worker's task functions.
245 public listTaskFunctions (): string[] {
246 const names
: string[] = [...this.taskFunctions
.keys()]
247 let defaultTaskFunctionName
: string = DEFAULT_TASK_NAME
248 for (const [name
, fn
] of this.taskFunctions
) {
250 name
!== DEFAULT_TASK_NAME
&&
251 fn
=== this.taskFunctions
.get(DEFAULT_TASK_NAME
)
253 defaultTaskFunctionName
= name
258 names
[names
.indexOf(DEFAULT_TASK_NAME
)],
259 defaultTaskFunctionName
,
261 (name
) => name
!== DEFAULT_TASK_NAME
&& name
!== defaultTaskFunctionName
267 * Sets the default task function to use in the worker.
269 * @param name - The name of the task function to use as default task function.
270 * @returns Whether the default task function was set or not.
271 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
272 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
273 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is a non-existing task function.
275 public setDefaultTaskFunction (name
: string): boolean {
276 this.checkTaskFunctionName(name
)
277 if (name
=== DEFAULT_TASK_NAME
) {
279 'Cannot set the default task function reserved name as the default task function'
282 if (!this.taskFunctions
.has(name
)) {
284 'Cannot set the default task function to a non-existing task function'
288 this.taskFunctions
.set(
290 this.taskFunctions
.get(name
) as TaskFunction
<Data
, Response
>
298 private checkTaskFunctionName (name
: string): void {
299 if (typeof name
!== 'string') {
300 throw new TypeError('name parameter is not a string')
302 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
303 throw new TypeError('name parameter is an empty string')
308 * Handles the ready message sent by the main worker.
310 * @param message - The ready message.
312 protected abstract handleReadyMessage (message
: MessageValue
<Data
>): void
315 * Worker message listener.
317 * @param message - The received message.
319 protected messageListener (message
: MessageValue
<Data
>): void {
320 this.checkMessageWorkerId(message
)
321 if (message
.statistics
!= null) {
322 // Statistics message received
323 this.statistics
= message
.statistics
324 } else if (message
.checkActive
!= null) {
325 // Check active message received
326 message
.checkActive
? this.startCheckActive() : this.stopCheckActive()
327 } else if (message
.taskId
!= null && message
.data
!= null) {
328 // Task message received
330 } else if (message
.kill
=== true) {
331 // Kill message received
332 this.handleKillMessage(message
)
337 * Handles a kill message sent by the main worker.
339 * @param message - The kill message.
341 protected handleKillMessage (message
: MessageValue
<Data
>): void {
342 this.stopCheckActive()
343 if (isAsyncFunction(this.opts
.killHandler
)) {
344 (this.opts
.killHandler
?.() as Promise
<void>)
346 this.sendToMainWorker({ kill
: 'success', workerId
: this.id
})
350 this.sendToMainWorker({ kill
: 'failure', workerId
: this.id
})
355 .catch(EMPTY_FUNCTION
)
358 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
359 this.opts
.killHandler
?.() as void
360 this.sendToMainWorker({ kill
: 'success', workerId
: this.id
})
362 this.sendToMainWorker({ kill
: 'failure', workerId
: this.id
})
370 * Check if the message worker id is set and matches the worker id.
372 * @param message - The message to check.
373 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the message worker id is not set or does not match the worker id.
375 private checkMessageWorkerId (message
: MessageValue
<Data
>): void {
376 if (message
.workerId
== null) {
377 throw new Error('Message worker id is not set')
378 } else if (message
.workerId
!= null && message
.workerId
!== this.id
) {
380 `Message worker id ${message.workerId} does not match the worker id ${this.id}`
386 * Starts the worker check active interval.
388 private startCheckActive (): void {
389 this.lastTaskTimestamp
= performance
.now()
390 this.activeInterval
= setInterval(
391 this.checkActive
.bind(this),
392 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
) / 2
397 * Stops the worker check active interval.
399 private stopCheckActive (): void {
400 if (this.activeInterval
!= null) {
401 clearInterval(this.activeInterval
)
402 delete this.activeInterval
407 * Checks if the worker should be terminated, because its living too long.
409 private checkActive (): void {
411 performance
.now() - this.lastTaskTimestamp
>
412 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
)
414 this.sendToMainWorker({ kill
: this.opts
.killBehavior
, workerId
: this.id
})
419 * Returns the main worker.
421 * @returns Reference to the main worker.
423 protected getMainWorker (): MainWorker
{
424 if (this.mainWorker
== null) {
425 throw new Error('Main worker not set')
427 return this.mainWorker
431 * Sends a message to main worker.
433 * @param message - The response message.
435 protected abstract sendToMainWorker (
436 message
: MessageValue
<Response
, Data
>
440 * Sends the list of task function names to the main worker.
442 protected sendTaskFunctionsListToMainWorker (): void {
443 this.sendToMainWorker({
444 taskFunctions
: this.listTaskFunctions(),
450 * Handles an error and convert it to a string so it can be sent back to the main worker.
452 * @param e - The error raised by the worker.
453 * @returns The error message.
455 protected handleError (e
: Error | string): string {
456 return e
instanceof Error ? e
.message
: e
460 * Runs the given task.
462 * @param task - The task to execute.
463 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found.
465 protected run (task
: Task
<Data
>): void {
466 const fn
= this.getTaskFunction(task
.name
)
467 if (isAsyncFunction(fn
)) {
468 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, task
)
470 this.runInAsyncScope(this.runSync
.bind(this), this, fn
, task
)
475 * Runs the given task function synchronously.
477 * @param fn - Task function that will be executed.
478 * @param task - Input data for the task function.
481 fn
: TaskSyncFunction
<Data
, Response
>,
484 const { name
, taskId
, data
} = task
486 let taskPerformance
= this.beginTaskPerformance(name
)
488 taskPerformance
= this.endTaskPerformance(taskPerformance
)
489 this.sendToMainWorker({
496 const errorMessage
= this.handleError(e
as Error | string)
497 this.sendToMainWorker({
499 name
: name
as string,
500 message
: errorMessage
,
507 this.updateLastTaskTimestamp()
512 * Runs the given task function asynchronously.
514 * @param fn - Task function that will be executed.
515 * @param task - Input data for the task function.
518 fn
: TaskAsyncFunction
<Data
, Response
>,
521 const { name
, taskId
, data
} = task
522 let taskPerformance
= this.beginTaskPerformance(name
)
525 taskPerformance
= this.endTaskPerformance(taskPerformance
)
526 this.sendToMainWorker({
535 const errorMessage
= this.handleError(e
as Error | string)
536 this.sendToMainWorker({
538 name
: name
as string,
539 message
: errorMessage
,
547 this.updateLastTaskTimestamp()
549 .catch(EMPTY_FUNCTION
)
553 * Gets the task function with the given name.
555 * @param name - Name of the task function that will be returned.
556 * @returns The task function.
557 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found.
559 private getTaskFunction (name
?: string): TaskFunction
<Data
, Response
> {
560 name
= name
?? DEFAULT_TASK_NAME
561 const fn
= this.taskFunctions
.get(name
)
563 throw new Error(`Task function '${name}' not found`)
568 private beginTaskPerformance (name
?: string): TaskPerformance
{
569 this.checkStatistics()
571 name
: name
?? DEFAULT_TASK_NAME
,
572 timestamp
: performance
.now(),
573 ...(this.statistics
.elu
&& { elu
: performance
.eventLoopUtilization() })
577 private endTaskPerformance (
578 taskPerformance
: TaskPerformance
580 this.checkStatistics()
583 ...(this.statistics
.runTime
&& {
584 runTime
: performance
.now() - taskPerformance
.timestamp
586 ...(this.statistics
.elu
&& {
587 elu
: performance
.eventLoopUtilization(taskPerformance
.elu
)
592 private checkStatistics (): void {
593 if (this.statistics
== null) {
594 throw new Error('Performance statistics computation requirements not set')
598 private updateLastTaskTimestamp (): void {
599 if (this.activeInterval
!= null) {
600 this.lastTaskTimestamp
= performance
.now()