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'
21 TaskFunctionOperationResult
,
24 } from
'./task-functions'
26 const DEFAULT_MAX_INACTIVE_TIME
= 60000
27 const DEFAULT_WORKER_OPTIONS
: WorkerOptions
= {
29 * The kill behavior option on this worker or its default value.
31 killBehavior
: KillBehaviors
.SOFT
,
33 * The maximum time to keep this worker active while idle.
34 * The pool automatically checks and terminates this worker when the time expires.
36 maxInactiveTime
: DEFAULT_MAX_INACTIVE_TIME
,
38 * The function to call when the worker is killed.
40 killHandler
: EMPTY_FUNCTION
44 * Base class that implements some shared logic for all poolifier workers.
46 * @typeParam MainWorker - Type of main worker.
47 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
48 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
50 export abstract class AbstractWorker
<
51 MainWorker
extends Worker
| MessagePort
,
54 > extends AsyncResource
{
58 protected abstract id
: number
60 * Task function(s) processed by the worker when the pool's `execution` function is invoked.
62 protected taskFunctions
!: Map
<string, TaskFunction
<Data
, Response
>>
64 * Timestamp of the last task processed by this worker.
66 protected lastTaskTimestamp
!: number
68 * Performance statistics computation requirements.
70 protected statistics
!: WorkerStatistics
72 * Handler id of the `activeInterval` worker activity check.
74 protected activeInterval
?: NodeJS
.Timeout
76 * Constructs a new poolifier worker.
78 * @param type - The type of async event.
79 * @param isMain - Whether this is the main worker or not.
80 * @param mainWorker - Reference to main worker.
81 * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function.
82 * @param opts - Options for the worker.
86 protected readonly isMain
: boolean,
87 private readonly mainWorker
: MainWorker
,
88 taskFunctions
: TaskFunction
<Data
, Response
> | TaskFunctions
<Data
, Response
>,
89 protected opts
: WorkerOptions
= DEFAULT_WORKER_OPTIONS
92 if (this.isMain
== null) {
93 throw new Error('isMain parameter is mandatory')
95 this.checkTaskFunctions(taskFunctions
)
96 this.checkWorkerOptions(this.opts
)
98 this.getMainWorker().on('message', this.handleReadyMessage
.bind(this))
102 private checkWorkerOptions (opts
: WorkerOptions
): void {
103 if (opts
!= null && !isPlainObject(opts
)) {
104 throw new TypeError('opts worker options parameter is not a plain object')
107 opts
?.killBehavior
!= null &&
108 !Object.values(KillBehaviors
).includes(opts
.killBehavior
)
111 `killBehavior option '${opts.killBehavior}' is not valid`
115 opts
?.maxInactiveTime
!= null &&
116 !Number.isSafeInteger(opts
.maxInactiveTime
)
118 throw new TypeError('maxInactiveTime option is not an integer')
120 if (opts
?.maxInactiveTime
!= null && opts
.maxInactiveTime
< 5) {
122 'maxInactiveTime option is not a positive integer greater or equal than 5'
125 if (opts
?.killHandler
!= null && typeof opts
.killHandler
!== 'function') {
126 throw new TypeError('killHandler option is not a function')
128 if (opts
?.async != null) {
129 throw new Error('async option is deprecated')
131 this.opts
= { ...DEFAULT_WORKER_OPTIONS
, ...opts
}
134 private checkValidTaskFunctionEntry (
136 fn
: TaskFunction
<Data
, Response
>
138 if (typeof name
!== 'string') {
140 'A taskFunctions parameter object key is not a string'
143 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
145 'A taskFunctions parameter object key is an empty string'
148 if (typeof fn
!== 'function') {
150 'A taskFunctions parameter object value is not a function'
156 * Checks if the `taskFunctions` parameter is passed to the constructor and valid.
158 * @param taskFunctions - The task function(s) parameter that should be checked.
160 private checkTaskFunctions (
161 taskFunctions
: TaskFunction
<Data
, Response
> | TaskFunctions
<Data
, Response
>
163 if (taskFunctions
== null) {
164 throw new Error('taskFunctions parameter is mandatory')
166 this.taskFunctions
= new Map
<string, TaskFunction
<Data
, Response
>>()
167 if (typeof taskFunctions
=== 'function') {
168 const boundFn
= taskFunctions
.bind(this)
169 this.taskFunctions
.set(DEFAULT_TASK_NAME
, boundFn
)
170 this.taskFunctions
.set(
171 typeof taskFunctions
.name
=== 'string' &&
172 taskFunctions
.name
.trim().length
> 0
177 } else if (isPlainObject(taskFunctions
)) {
178 let firstEntry
= true
179 for (const [name
, fn
] of Object.entries(taskFunctions
)) {
180 this.checkValidTaskFunctionEntry(name
, fn
)
181 const boundFn
= fn
.bind(this)
183 this.taskFunctions
.set(DEFAULT_TASK_NAME
, boundFn
)
186 this.taskFunctions
.set(name
, boundFn
)
189 throw new Error('taskFunctions parameter object is empty')
193 'taskFunctions parameter is not a function or a plain object'
199 * Checks if the worker has a task function with the given name.
201 * @param name - The name of the task function to check.
202 * @returns Whether the worker has a task function with the given name or not.
204 public hasTaskFunction (name
: string): TaskFunctionOperationResult
{
206 this.checkTaskFunctionName(name
)
208 return { status: false, error
: error
as Error }
210 return { status: this.taskFunctions
.has(name
) }
214 * Adds a task function to the worker.
215 * If a task function with the same name already exists, it is replaced.
217 * @param name - The name of the task function to add.
218 * @param fn - The task function to add.
219 * @returns Whether the task function was added or not.
221 public addTaskFunction (
223 fn
: TaskFunction
<Data
, Response
>
224 ): TaskFunctionOperationResult
{
226 this.checkTaskFunctionName(name
)
227 if (name
=== DEFAULT_TASK_NAME
) {
229 'Cannot add a task function with the default reserved name'
232 if (typeof fn
!== 'function') {
233 throw new TypeError('fn parameter is not a function')
235 const boundFn
= fn
.bind(this)
237 this.taskFunctions
.get(name
) ===
238 this.taskFunctions
.get(DEFAULT_TASK_NAME
)
240 this.taskFunctions
.set(DEFAULT_TASK_NAME
, boundFn
)
242 this.taskFunctions
.set(name
, boundFn
)
243 this.sendTaskFunctionNamesToMainWorker()
244 return { status: true }
246 return { status: false, error
: error
as Error }
251 * Removes a task function from the worker.
253 * @param name - The name of the task function to remove.
254 * @returns Whether the task function existed and was removed or not.
256 public removeTaskFunction (name
: string): TaskFunctionOperationResult
{
258 this.checkTaskFunctionName(name
)
259 if (name
=== DEFAULT_TASK_NAME
) {
261 'Cannot remove the task function with the default reserved name'
265 this.taskFunctions
.get(name
) ===
266 this.taskFunctions
.get(DEFAULT_TASK_NAME
)
269 'Cannot remove the task function used as the default task function'
272 const deleteStatus
= this.taskFunctions
.delete(name
)
273 this.sendTaskFunctionNamesToMainWorker()
274 return { status: deleteStatus
}
276 return { status: false, error
: error
as Error }
281 * Lists the names of the worker's task functions.
283 * @returns The names of the worker's task functions.
285 public listTaskFunctionNames (): string[] {
286 const names
: string[] = [...this.taskFunctions
.keys()]
287 let defaultTaskFunctionName
: string = DEFAULT_TASK_NAME
288 for (const [name
, fn
] of this.taskFunctions
) {
290 name
!== DEFAULT_TASK_NAME
&&
291 fn
=== this.taskFunctions
.get(DEFAULT_TASK_NAME
)
293 defaultTaskFunctionName
= name
298 names
[names
.indexOf(DEFAULT_TASK_NAME
)],
299 defaultTaskFunctionName
,
301 name
=> name
!== DEFAULT_TASK_NAME
&& name
!== defaultTaskFunctionName
307 * Sets the default task function to use in the worker.
309 * @param name - The name of the task function to use as default task function.
310 * @returns Whether the default task function was set or not.
312 public setDefaultTaskFunction (name
: string): TaskFunctionOperationResult
{
314 this.checkTaskFunctionName(name
)
315 if (name
=== DEFAULT_TASK_NAME
) {
317 'Cannot set the default task function reserved name as the default task function'
320 if (!this.taskFunctions
.has(name
)) {
322 'Cannot set the default task function to a non-existing task function'
325 this.taskFunctions
.set(
327 this.taskFunctions
.get(name
) as TaskFunction
<Data
, Response
>
329 this.sendTaskFunctionNamesToMainWorker()
330 return { status: true }
332 return { status: false, error
: error
as Error }
336 private checkTaskFunctionName (name
: string): void {
337 if (typeof name
!== 'string') {
338 throw new TypeError('name parameter is not a string')
340 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
341 throw new TypeError('name parameter is an empty string')
346 * Handles the ready message sent by the main worker.
348 * @param message - The ready message.
350 protected abstract handleReadyMessage (message
: MessageValue
<Data
>): void
353 * Worker message listener.
355 * @param message - The received message.
357 protected messageListener (message
: MessageValue
<Data
>): void {
358 this.checkMessageWorkerId(message
)
359 if (message
.statistics
!= null) {
360 // Statistics message received
361 this.statistics
= message
.statistics
362 } else if (message
.checkActive
!= null) {
363 // Check active message received
364 message
.checkActive
? this.startCheckActive() : this.stopCheckActive()
365 } else if (message
.taskFunctionOperation
!= null) {
366 // Task function operation message received
367 this.handleTaskFunctionOperationMessage(message
)
368 } else if (message
.taskId
!= null && message
.data
!= null) {
369 // Task message received
371 } else if (message
.kill
=== true) {
372 // Kill message received
373 this.handleKillMessage(message
)
377 protected handleTaskFunctionOperationMessage (
378 message
: MessageValue
<Data
>
380 const { taskFunctionOperation
, taskFunctionName
, taskFunction
} = message
381 let response
!: TaskFunctionOperationResult
382 if (taskFunctionOperation
=== 'add') {
383 response
= this.addTaskFunction(
384 taskFunctionName
as string,
385 // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
386 new Function(`return ${taskFunction as string}`)() as TaskFunction
<
391 } else if (taskFunctionOperation
=== 'remove') {
392 response
= this.removeTaskFunction(taskFunctionName
as string)
393 } else if (taskFunctionOperation
=== 'default') {
394 response
= this.setDefaultTaskFunction(taskFunctionName
as string)
396 this.sendToMainWorker({
397 taskFunctionOperation
,
398 taskFunctionOperationStatus
: response
.status,
400 ...(!response
.status &&
401 response
?.error
!= null && {
403 name
: taskFunctionName
as string,
404 message
: this.handleError(response
.error
as Error | string)
411 * Handles a kill message sent by the main worker.
413 * @param message - The kill message.
415 protected handleKillMessage (message
: MessageValue
<Data
>): void {
416 this.stopCheckActive()
417 if (isAsyncFunction(this.opts
.killHandler
)) {
418 (this.opts
.killHandler
?.() as Promise
<void>)
420 this.sendToMainWorker({ kill
: 'success' })
424 this.sendToMainWorker({ kill
: 'failure' })
429 .catch(EMPTY_FUNCTION
)
432 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
433 this.opts
.killHandler
?.() as void
434 this.sendToMainWorker({ kill
: 'success' })
436 this.sendToMainWorker({ kill
: 'failure' })
444 * Check if the message worker id is set and matches the worker id.
446 * @param message - The message to check.
447 * @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.
449 private checkMessageWorkerId (message
: MessageValue
<Data
>): void {
450 if (message
.workerId
== null) {
451 throw new Error('Message worker id is not set')
452 } else if (message
.workerId
!= null && message
.workerId
!== this.id
) {
454 `Message worker id ${message.workerId} does not match the worker id ${this.id}`
460 * Starts the worker check active interval.
462 private startCheckActive (): void {
463 this.lastTaskTimestamp
= performance
.now()
464 this.activeInterval
= setInterval(
465 this.checkActive
.bind(this),
466 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
) / 2
471 * Stops the worker check active interval.
473 private stopCheckActive (): void {
474 if (this.activeInterval
!= null) {
475 clearInterval(this.activeInterval
)
476 delete this.activeInterval
481 * Checks if the worker should be terminated, because its living too long.
483 private checkActive (): void {
485 performance
.now() - this.lastTaskTimestamp
>
486 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
)
488 this.sendToMainWorker({ kill
: this.opts
.killBehavior
})
493 * Returns the main worker.
495 * @returns Reference to the main worker.
496 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the main worker is not set.
498 protected getMainWorker (): MainWorker
{
499 if (this.mainWorker
== null) {
500 throw new Error('Main worker not set')
502 return this.mainWorker
506 * Sends a message to main worker.
508 * @param message - The response message.
510 protected abstract sendToMainWorker (
511 message
: MessageValue
<Response
, Data
>
515 * Sends task function names to the main worker.
517 protected sendTaskFunctionNamesToMainWorker (): void {
518 this.sendToMainWorker({
519 taskFunctionNames
: this.listTaskFunctionNames()
524 * Handles an error and convert it to a string so it can be sent back to the main worker.
526 * @param error - The error raised by the worker.
527 * @returns The error message.
529 protected handleError (error
: Error | string): string {
530 return error
instanceof Error ? error
.message
: error
534 * Runs the given task.
536 * @param task - The task to execute.
537 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found.
539 protected run (task
: Task
<Data
>): void {
540 const { name
, taskId
, data
} = task
541 const fn
= this.taskFunctions
.get(name
?? DEFAULT_TASK_NAME
)
543 this.sendToMainWorker({
545 name
: name
as string,
546 message
: `Task function '${name as string}' not found`,
553 if (isAsyncFunction(fn
)) {
554 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, task
)
556 this.runInAsyncScope(this.runSync
.bind(this), this, fn
, task
)
561 * Runs the given task function synchronously.
563 * @param fn - Task function that will be executed.
564 * @param task - Input data for the task function.
567 fn
: TaskSyncFunction
<Data
, Response
>,
570 const { name
, taskId
, data
} = task
572 let taskPerformance
= this.beginTaskPerformance(name
)
574 taskPerformance
= this.endTaskPerformance(taskPerformance
)
575 this.sendToMainWorker({
581 this.sendToMainWorker({
583 name
: name
as string,
584 message
: this.handleError(error
as Error | string),
590 this.updateLastTaskTimestamp()
595 * Runs the given task function asynchronously.
597 * @param fn - Task function that will be executed.
598 * @param task - Input data for the task function.
601 fn
: TaskAsyncFunction
<Data
, Response
>,
604 const { name
, taskId
, data
} = task
605 let taskPerformance
= this.beginTaskPerformance(name
)
608 taskPerformance
= this.endTaskPerformance(taskPerformance
)
609 this.sendToMainWorker({
617 this.sendToMainWorker({
619 name
: name
as string,
620 message
: this.handleError(error
as Error | string),
627 this.updateLastTaskTimestamp()
629 .catch(EMPTY_FUNCTION
)
632 private beginTaskPerformance (name
?: string): TaskPerformance
{
633 this.checkStatistics()
635 name
: name
?? DEFAULT_TASK_NAME
,
636 timestamp
: performance
.now(),
637 ...(this.statistics
.elu
&& { elu
: performance
.eventLoopUtilization() })
641 private endTaskPerformance (
642 taskPerformance
: TaskPerformance
644 this.checkStatistics()
647 ...(this.statistics
.runTime
&& {
648 runTime
: performance
.now() - taskPerformance
.timestamp
650 ...(this.statistics
.elu
&& {
651 elu
: performance
.eventLoopUtilization(taskPerformance
.elu
)
656 private checkStatistics (): void {
657 if (this.statistics
== null) {
658 throw new Error('Performance statistics computation requirements not set')
662 private updateLastTaskTimestamp (): void {
663 if (this.activeInterval
!= null) {
664 this.lastTaskTimestamp
= performance
.now()