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 interface TaskFunctionOperationReturnType
{
30 const DEFAULT_MAX_INACTIVE_TIME
= 60000
31 const DEFAULT_WORKER_OPTIONS
: WorkerOptions
= {
33 * The kill behavior option on this worker or its default value.
35 killBehavior
: KillBehaviors
.SOFT
,
37 * The maximum time to keep this worker active while idle.
38 * The pool automatically checks and terminates this worker when the time expires.
40 maxInactiveTime
: DEFAULT_MAX_INACTIVE_TIME
,
42 * The function to call when the worker is killed.
44 killHandler
: EMPTY_FUNCTION
48 * Base class that implements some shared logic for all poolifier workers.
50 * @typeParam MainWorker - Type of main worker.
51 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
52 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
54 export abstract class AbstractWorker
<
55 MainWorker
extends Worker
| MessagePort
,
58 > extends AsyncResource
{
62 protected abstract id
: number
64 * Task function(s) processed by the worker when the pool's `execution` function is invoked.
66 protected taskFunctions
!: Map
<string, TaskFunction
<Data
, Response
>>
68 * Timestamp of the last task processed by this worker.
70 protected lastTaskTimestamp
!: number
72 * Performance statistics computation requirements.
74 protected statistics
!: WorkerStatistics
76 * Handler id of the `activeInterval` worker activity check.
78 protected activeInterval
?: NodeJS
.Timeout
80 * Constructs a new poolifier worker.
82 * @param type - The type of async event.
83 * @param isMain - Whether this is the main worker or not.
84 * @param mainWorker - Reference to main worker.
85 * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function.
86 * @param opts - Options for the worker.
90 protected readonly isMain
: boolean,
91 private readonly mainWorker
: MainWorker
,
92 taskFunctions
: TaskFunction
<Data
, Response
> | TaskFunctions
<Data
, Response
>,
93 protected opts
: WorkerOptions
= DEFAULT_WORKER_OPTIONS
96 if (this.isMain
== null) {
97 throw new Error('isMain parameter is mandatory')
99 this.checkTaskFunctions(taskFunctions
)
100 this.checkWorkerOptions(this.opts
)
102 this.getMainWorker().on('message', this.handleReadyMessage
.bind(this))
106 private checkWorkerOptions (opts
: WorkerOptions
): void {
107 this.opts
= { ...DEFAULT_WORKER_OPTIONS
, ...opts
}
108 delete this.opts
.async
111 private checkValidTaskFunction (
113 fn
: TaskFunction
<Data
, Response
>
115 if (typeof name
!== 'string') {
117 'A taskFunctions parameter object key is not a string'
120 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
122 'A taskFunctions parameter object key is an empty string'
125 if (typeof fn
!== 'function') {
127 'A taskFunctions parameter object value is not a function'
133 * Checks if the `taskFunctions` parameter is passed to the constructor.
135 * @param taskFunctions - The task function(s) parameter that should be checked.
137 private checkTaskFunctions (
138 taskFunctions
: TaskFunction
<Data
, Response
> | TaskFunctions
<Data
, Response
>
140 if (taskFunctions
== null) {
141 throw new Error('taskFunctions parameter is mandatory')
143 this.taskFunctions
= new Map
<string, TaskFunction
<Data
, Response
>>()
144 if (typeof taskFunctions
=== 'function') {
145 const boundFn
= taskFunctions
.bind(this)
146 this.taskFunctions
.set(DEFAULT_TASK_NAME
, boundFn
)
147 this.taskFunctions
.set(
148 typeof taskFunctions
.name
=== 'string' &&
149 taskFunctions
.name
.trim().length
> 0
154 } else if (isPlainObject(taskFunctions
)) {
155 let firstEntry
= true
156 for (const [name
, fn
] of Object.entries(taskFunctions
)) {
157 this.checkValidTaskFunction(name
, fn
)
158 const boundFn
= fn
.bind(this)
160 this.taskFunctions
.set(DEFAULT_TASK_NAME
, boundFn
)
163 this.taskFunctions
.set(name
, boundFn
)
166 throw new Error('taskFunctions parameter object is empty')
170 'taskFunctions parameter is not a function or a plain object'
176 * Checks if the worker has a task function with the given name.
178 * @param name - The name of the task function to check.
179 * @returns Whether the worker has a task function with the given name or not.
181 public hasTaskFunction (name
: string): TaskFunctionOperationReturnType
{
183 this.checkTaskFunctionName(name
)
185 return { status: false, error
: error
as Error }
187 return { status: this.taskFunctions
.has(name
) }
191 * Adds a task function to the worker.
192 * If a task function with the same name already exists, it is replaced.
194 * @param name - The name of the task function to add.
195 * @param fn - The task function to add.
196 * @returns Whether the task function was added or not.
198 public addTaskFunction (
200 fn
: TaskFunction
<Data
, Response
>
201 ): TaskFunctionOperationReturnType
{
203 this.checkTaskFunctionName(name
)
204 if (name
=== DEFAULT_TASK_NAME
) {
206 'Cannot add a task function with the default reserved name'
209 if (typeof fn
!== 'function') {
210 throw new TypeError('fn parameter is not a function')
212 const boundFn
= fn
.bind(this)
214 this.taskFunctions
.get(name
) ===
215 this.taskFunctions
.get(DEFAULT_TASK_NAME
)
217 this.taskFunctions
.set(DEFAULT_TASK_NAME
, boundFn
)
219 this.taskFunctions
.set(name
, boundFn
)
220 this.sendTaskFunctionsListToMainWorker()
221 return { status: true }
223 return { status: false, error
: error
as Error }
228 * Removes a task function from the worker.
230 * @param name - The name of the task function to remove.
231 * @returns Whether the task function existed and was removed or not.
233 public removeTaskFunction (name
: string): TaskFunctionOperationReturnType
{
235 this.checkTaskFunctionName(name
)
236 if (name
=== DEFAULT_TASK_NAME
) {
238 'Cannot remove the task function with the default reserved name'
242 this.taskFunctions
.get(name
) ===
243 this.taskFunctions
.get(DEFAULT_TASK_NAME
)
246 'Cannot remove the task function used as the default task function'
249 const deleteStatus
= this.taskFunctions
.delete(name
)
250 this.sendTaskFunctionsListToMainWorker()
251 return { status: deleteStatus
}
253 return { status: false, error
: error
as Error }
258 * Lists the names of the worker's task functions.
260 * @returns The names of the worker's task functions.
262 public listTaskFunctionNames (): string[] {
263 const names
: string[] = [...this.taskFunctions
.keys()]
264 let defaultTaskFunctionName
: string = DEFAULT_TASK_NAME
265 for (const [name
, fn
] of this.taskFunctions
) {
267 name
!== DEFAULT_TASK_NAME
&&
268 fn
=== this.taskFunctions
.get(DEFAULT_TASK_NAME
)
270 defaultTaskFunctionName
= name
275 names
[names
.indexOf(DEFAULT_TASK_NAME
)],
276 defaultTaskFunctionName
,
278 name
=> name
!== DEFAULT_TASK_NAME
&& name
!== defaultTaskFunctionName
284 * Sets the default task function to use in the worker.
286 * @param name - The name of the task function to use as default task function.
287 * @returns Whether the default task function was set or not.
289 public setDefaultTaskFunction (name
: string): TaskFunctionOperationReturnType
{
291 this.checkTaskFunctionName(name
)
292 if (name
=== DEFAULT_TASK_NAME
) {
294 'Cannot set the default task function reserved name as the default task function'
297 if (!this.taskFunctions
.has(name
)) {
299 'Cannot set the default task function to a non-existing task function'
302 this.taskFunctions
.set(
304 this.taskFunctions
.get(name
) as TaskFunction
<Data
, Response
>
306 return { status: true }
308 return { status: false, error
: error
as Error }
312 private checkTaskFunctionName (name
: string): void {
313 if (typeof name
!== 'string') {
314 throw new TypeError('name parameter is not a string')
316 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
317 throw new TypeError('name parameter is an empty string')
322 * Handles the ready message sent by the main worker.
324 * @param message - The ready message.
326 protected abstract handleReadyMessage (message
: MessageValue
<Data
>): void
329 * Worker message listener.
331 * @param message - The received message.
333 protected messageListener (message
: MessageValue
<Data
>): void {
334 this.checkMessageWorkerId(message
)
335 if (message
.statistics
!= null) {
336 // Statistics message received
337 this.statistics
= message
.statistics
338 } else if (message
.checkActive
!= null) {
339 // Check active message received
340 message
.checkActive
? this.startCheckActive() : this.stopCheckActive()
341 } else if (message
.taskFunctionOperation
!= null) {
342 // Task function operation message received
343 this.handleTaskFunctionOperationMessage(message
)
344 } else if (message
.taskId
!= null && message
.data
!= null) {
345 // Task message received
347 } else if (message
.kill
=== true) {
348 // Kill message received
349 this.handleKillMessage(message
)
353 protected handleTaskFunctionOperationMessage (
354 message
: MessageValue
<Data
>
356 const { taskFunctionOperation
, taskFunction
, taskFunctionName
} = message
357 let response
!: TaskFunctionOperationReturnType
358 if (taskFunctionOperation
=== 'add') {
359 response
= this.addTaskFunction(
360 taskFunctionName
as string,
361 // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
362 new Function(`return ${taskFunction as string}`)() as TaskFunction
<
367 } else if (taskFunctionOperation
=== 'remove') {
368 response
= this.removeTaskFunction(taskFunctionName
as string)
369 } else if (taskFunctionOperation
=== 'default') {
370 response
= this.setDefaultTaskFunction(taskFunctionName
as string)
372 this.sendToMainWorker({
373 taskFunctionOperation
,
374 taskFunctionOperationStatus
: response
.status,
376 name
: taskFunctionName
as string,
377 message
: this.handleError(response
.error
as Error | string)
384 * Handles a kill message sent by the main worker.
386 * @param message - The kill message.
388 protected handleKillMessage (message
: MessageValue
<Data
>): void {
389 this.stopCheckActive()
390 if (isAsyncFunction(this.opts
.killHandler
)) {
391 (this.opts
.killHandler
?.() as Promise
<void>)
393 this.sendToMainWorker({ kill
: 'success', workerId
: this.id
})
397 this.sendToMainWorker({ kill
: 'failure', workerId
: this.id
})
402 .catch(EMPTY_FUNCTION
)
405 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
406 this.opts
.killHandler
?.() as void
407 this.sendToMainWorker({ kill
: 'success', workerId
: this.id
})
409 this.sendToMainWorker({ kill
: 'failure', workerId
: this.id
})
417 * Check if the message worker id is set and matches the worker id.
419 * @param message - The message to check.
420 * @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.
422 private checkMessageWorkerId (message
: MessageValue
<Data
>): void {
423 if (message
.workerId
== null) {
424 throw new Error('Message worker id is not set')
425 } else if (message
.workerId
!= null && message
.workerId
!== this.id
) {
427 `Message worker id ${message.workerId} does not match the worker id ${this.id}`
433 * Starts the worker check active interval.
435 private startCheckActive (): void {
436 this.lastTaskTimestamp
= performance
.now()
437 this.activeInterval
= setInterval(
438 this.checkActive
.bind(this),
439 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
) / 2
444 * Stops the worker check active interval.
446 private stopCheckActive (): void {
447 if (this.activeInterval
!= null) {
448 clearInterval(this.activeInterval
)
449 delete this.activeInterval
454 * Checks if the worker should be terminated, because its living too long.
456 private checkActive (): void {
458 performance
.now() - this.lastTaskTimestamp
>
459 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
)
461 this.sendToMainWorker({ kill
: this.opts
.killBehavior
, workerId
: this.id
})
466 * Returns the main worker.
468 * @returns Reference to the main worker.
469 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the main worker is not set.
471 protected getMainWorker (): MainWorker
{
472 if (this.mainWorker
== null) {
473 throw new Error('Main worker not set')
475 return this.mainWorker
479 * Sends a message to main worker.
481 * @param message - The response message.
483 protected abstract sendToMainWorker (
484 message
: MessageValue
<Response
, Data
>
488 * Sends the list of task function names to the main worker.
490 protected sendTaskFunctionsListToMainWorker (): void {
491 this.sendToMainWorker({
492 taskFunctionNames
: this.listTaskFunctionNames(),
498 * Handles an error and convert it to a string so it can be sent back to the main worker.
500 * @param error - The error raised by the worker.
501 * @returns The error message.
503 protected handleError (error
: Error | string): string {
504 return error
instanceof Error ? error
.message
: error
508 * Runs the given task.
510 * @param task - The task to execute.
511 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found.
513 protected run (task
: Task
<Data
>): void {
514 const { name
, taskId
, data
} = task
515 const fn
= this.taskFunctions
.get(name
?? DEFAULT_TASK_NAME
)
517 this.sendToMainWorker({
519 name
: name
as string,
520 message
: `Task function '${name as string}' not found`,
528 if (isAsyncFunction(fn
)) {
529 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, task
)
531 this.runInAsyncScope(this.runSync
.bind(this), this, fn
, task
)
536 * Runs the given task function synchronously.
538 * @param fn - Task function that will be executed.
539 * @param task - Input data for the task function.
542 fn
: TaskSyncFunction
<Data
, Response
>,
545 const { name
, taskId
, data
} = task
547 let taskPerformance
= this.beginTaskPerformance(name
)
549 taskPerformance
= this.endTaskPerformance(taskPerformance
)
550 this.sendToMainWorker({
557 this.sendToMainWorker({
559 name
: name
as string,
560 message
: this.handleError(error
as Error | string),
567 this.updateLastTaskTimestamp()
572 * Runs the given task function asynchronously.
574 * @param fn - Task function that will be executed.
575 * @param task - Input data for the task function.
578 fn
: TaskAsyncFunction
<Data
, Response
>,
581 const { name
, taskId
, data
} = task
582 let taskPerformance
= this.beginTaskPerformance(name
)
585 taskPerformance
= this.endTaskPerformance(taskPerformance
)
586 this.sendToMainWorker({
595 this.sendToMainWorker({
597 name
: name
as string,
598 message
: this.handleError(error
as Error | string),
606 this.updateLastTaskTimestamp()
608 .catch(EMPTY_FUNCTION
)
611 private beginTaskPerformance (name
?: string): TaskPerformance
{
612 this.checkStatistics()
614 name
: name
?? DEFAULT_TASK_NAME
,
615 timestamp
: performance
.now(),
616 ...(this.statistics
.elu
&& { elu
: performance
.eventLoopUtilization() })
620 private endTaskPerformance (
621 taskPerformance
: TaskPerformance
623 this.checkStatistics()
626 ...(this.statistics
.runTime
&& {
627 runTime
: performance
.now() - taskPerformance
.timestamp
629 ...(this.statistics
.elu
&& {
630 elu
: performance
.eventLoopUtilization(taskPerformance
.elu
)
635 private checkStatistics (): void {
636 if (this.statistics
== null) {
637 throw new Error('Performance statistics computation requirements not set')
641 private updateLastTaskTimestamp (): void {
642 if (this.activeInterval
!= null) {
643 this.lastTaskTimestamp
= performance
.now()