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
=== 'has') {
359 response
= this.hasTaskFunction(taskFunctionName
as string)
360 } else if (taskFunctionOperation
=== 'add') {
361 response
= this.addTaskFunction(
362 taskFunctionName
as string,
363 // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
364 new Function(`return ${taskFunction as string}`)() as TaskFunction
<
369 } else if (taskFunctionOperation
=== 'remove') {
370 response
= this.removeTaskFunction(taskFunctionName
as string)
371 } else if (taskFunctionOperation
=== 'default') {
372 response
= this.setDefaultTaskFunction(taskFunctionName
as string)
374 this.sendToMainWorker({
375 taskFunctionOperation
,
376 taskFunctionOperationStatus
: response
.status,
378 name
: taskFunctionName
as string,
379 message
: this.handleError(response
.error
as Error | string)
386 * Handles a kill message sent by the main worker.
388 * @param message - The kill message.
390 protected handleKillMessage (message
: MessageValue
<Data
>): void {
391 this.stopCheckActive()
392 if (isAsyncFunction(this.opts
.killHandler
)) {
393 (this.opts
.killHandler
?.() as Promise
<void>)
395 this.sendToMainWorker({ kill
: 'success', workerId
: this.id
})
399 this.sendToMainWorker({ kill
: 'failure', workerId
: this.id
})
404 .catch(EMPTY_FUNCTION
)
407 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
408 this.opts
.killHandler
?.() as void
409 this.sendToMainWorker({ kill
: 'success', workerId
: this.id
})
411 this.sendToMainWorker({ kill
: 'failure', workerId
: this.id
})
419 * Check if the message worker id is set and matches the worker id.
421 * @param message - The message to check.
422 * @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.
424 private checkMessageWorkerId (message
: MessageValue
<Data
>): void {
425 if (message
.workerId
== null) {
426 throw new Error('Message worker id is not set')
427 } else if (message
.workerId
!= null && message
.workerId
!== this.id
) {
429 `Message worker id ${message.workerId} does not match the worker id ${this.id}`
435 * Starts the worker check active interval.
437 private startCheckActive (): void {
438 this.lastTaskTimestamp
= performance
.now()
439 this.activeInterval
= setInterval(
440 this.checkActive
.bind(this),
441 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
) / 2
446 * Stops the worker check active interval.
448 private stopCheckActive (): void {
449 if (this.activeInterval
!= null) {
450 clearInterval(this.activeInterval
)
451 delete this.activeInterval
456 * Checks if the worker should be terminated, because its living too long.
458 private checkActive (): void {
460 performance
.now() - this.lastTaskTimestamp
>
461 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
)
463 this.sendToMainWorker({ kill
: this.opts
.killBehavior
, workerId
: this.id
})
468 * Returns the main worker.
470 * @returns Reference to the main worker.
471 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the main worker is not set.
473 protected getMainWorker (): MainWorker
{
474 if (this.mainWorker
== null) {
475 throw new Error('Main worker not set')
477 return this.mainWorker
481 * Sends a message to main worker.
483 * @param message - The response message.
485 protected abstract sendToMainWorker (
486 message
: MessageValue
<Response
, Data
>
490 * Sends the list of task function names to the main worker.
492 protected sendTaskFunctionsListToMainWorker (): void {
493 this.sendToMainWorker({
494 taskFunctionNames
: this.listTaskFunctionNames(),
500 * Handles an error and convert it to a string so it can be sent back to the main worker.
502 * @param error - The error raised by the worker.
503 * @returns The error message.
505 protected handleError (error
: Error | string): string {
506 return error
instanceof Error ? error
.message
: error
510 * Runs the given task.
512 * @param task - The task to execute.
513 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found.
515 protected run (task
: Task
<Data
>): void {
516 const { name
, taskId
, data
} = task
517 const fn
= this.taskFunctions
.get(name
?? DEFAULT_TASK_NAME
)
519 this.sendToMainWorker({
521 name
: name
as string,
522 message
: `Task function '${name as string}' not found`,
530 if (isAsyncFunction(fn
)) {
531 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, task
)
533 this.runInAsyncScope(this.runSync
.bind(this), this, fn
, task
)
538 * Runs the given task function synchronously.
540 * @param fn - Task function that will be executed.
541 * @param task - Input data for the task function.
544 fn
: TaskSyncFunction
<Data
, Response
>,
547 const { name
, taskId
, data
} = task
549 let taskPerformance
= this.beginTaskPerformance(name
)
551 taskPerformance
= this.endTaskPerformance(taskPerformance
)
552 this.sendToMainWorker({
559 this.sendToMainWorker({
561 name
: name
as string,
562 message
: this.handleError(error
as Error | string),
569 this.updateLastTaskTimestamp()
574 * Runs the given task function asynchronously.
576 * @param fn - Task function that will be executed.
577 * @param task - Input data for the task function.
580 fn
: TaskAsyncFunction
<Data
, Response
>,
583 const { name
, taskId
, data
} = task
584 let taskPerformance
= this.beginTaskPerformance(name
)
587 taskPerformance
= this.endTaskPerformance(taskPerformance
)
588 this.sendToMainWorker({
597 this.sendToMainWorker({
599 name
: name
as string,
600 message
: this.handleError(error
as Error | string),
608 this.updateLastTaskTimestamp()
610 .catch(EMPTY_FUNCTION
)
613 private beginTaskPerformance (name
?: string): TaskPerformance
{
614 this.checkStatistics()
616 name
: name
?? DEFAULT_TASK_NAME
,
617 timestamp
: performance
.now(),
618 ...(this.statistics
.elu
&& { elu
: performance
.eventLoopUtilization() })
622 private endTaskPerformance (
623 taskPerformance
: TaskPerformance
625 this.checkStatistics()
628 ...(this.statistics
.runTime
&& {
629 runTime
: performance
.now() - taskPerformance
.timestamp
631 ...(this.statistics
.elu
&& {
632 elu
: performance
.eventLoopUtilization(taskPerformance
.elu
)
637 private checkStatistics (): void {
638 if (this.statistics
== null) {
639 throw new Error('Performance statistics computation requirements not set')
643 private updateLastTaskTimestamp (): void {
644 if (this.activeInterval
!= null) {
645 this.lastTaskTimestamp
= performance
.now()