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 checkTaskFunctionName
,
27 checkValidTaskFunctionEntry
,
28 checkValidWorkerOptions
31 const DEFAULT_MAX_INACTIVE_TIME
= 60000
32 const DEFAULT_WORKER_OPTIONS
: WorkerOptions
= {
34 * The kill behavior option on this worker or its default value.
36 killBehavior
: KillBehaviors
.SOFT
,
38 * The maximum time to keep this worker active while idle.
39 * The pool automatically checks and terminates this worker when the time expires.
41 maxInactiveTime
: DEFAULT_MAX_INACTIVE_TIME
,
43 * The function to call when the worker is killed.
45 killHandler
: EMPTY_FUNCTION
49 * Base class that implements some shared logic for all poolifier workers.
51 * @typeParam MainWorker - Type of main worker.
52 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
53 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
55 export abstract class AbstractWorker
<
56 MainWorker
extends Worker
| MessagePort
,
59 > extends AsyncResource
{
63 protected abstract id
: number
65 * Task function(s) processed by the worker when the pool's `execution` function is invoked.
67 protected taskFunctions
!: Map
<string, TaskFunction
<Data
, Response
>>
69 * Timestamp of the last task processed by this worker.
71 protected lastTaskTimestamp
!: number
73 * Performance statistics computation requirements.
75 protected statistics
!: WorkerStatistics
77 * Handler id of the `activeInterval` worker activity check.
79 protected activeInterval
?: NodeJS
.Timeout
81 * Constructs a new poolifier worker.
83 * @param type - The type of async event.
84 * @param isMain - Whether this is the main worker or not.
85 * @param mainWorker - Reference to main worker.
86 * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function.
87 * @param opts - Options for the worker.
91 protected readonly isMain
: boolean,
92 private readonly mainWorker
: MainWorker
,
93 taskFunctions
: TaskFunction
<Data
, Response
> | TaskFunctions
<Data
, Response
>,
94 protected opts
: WorkerOptions
= DEFAULT_WORKER_OPTIONS
97 if (this.isMain
== null) {
98 throw new Error('isMain parameter is mandatory')
100 this.checkTaskFunctions(taskFunctions
)
101 this.checkWorkerOptions(this.opts
)
103 this.getMainWorker().once('message', this.handleReadyMessage
.bind(this))
107 private checkWorkerOptions (opts
: WorkerOptions
): void {
108 checkValidWorkerOptions(opts
)
109 this.opts
= { ...DEFAULT_WORKER_OPTIONS
, ...opts
}
113 * Checks if the `taskFunctions` parameter is passed to the constructor and valid.
115 * @param taskFunctions - The task function(s) parameter that should be checked.
117 private checkTaskFunctions (
118 taskFunctions
: TaskFunction
<Data
, Response
> | TaskFunctions
<Data
, Response
>
120 if (taskFunctions
== null) {
121 throw new Error('taskFunctions parameter is mandatory')
123 this.taskFunctions
= new Map
<string, TaskFunction
<Data
, Response
>>()
124 if (typeof taskFunctions
=== 'function') {
125 const boundFn
= taskFunctions
.bind(this)
126 this.taskFunctions
.set(DEFAULT_TASK_NAME
, boundFn
)
127 this.taskFunctions
.set(
128 typeof taskFunctions
.name
=== 'string' &&
129 taskFunctions
.name
.trim().length
> 0
134 } else if (isPlainObject(taskFunctions
)) {
135 let firstEntry
= true
136 for (const [name
, fn
] of Object.entries(taskFunctions
)) {
137 checkValidTaskFunctionEntry
<Data
, Response
>(name
, fn
)
138 const boundFn
= fn
.bind(this)
140 this.taskFunctions
.set(DEFAULT_TASK_NAME
, boundFn
)
143 this.taskFunctions
.set(name
, boundFn
)
146 throw new Error('taskFunctions parameter object is empty')
150 'taskFunctions parameter is not a function or a plain object'
156 * Checks if the worker has a task function with the given name.
158 * @param name - The name of the task function to check.
159 * @returns Whether the worker has a task function with the given name or not.
161 public hasTaskFunction (name
: string): TaskFunctionOperationResult
{
163 checkTaskFunctionName(name
)
165 return { status: false, error
: error
as Error }
167 return { status: this.taskFunctions
.has(name
) }
171 * Adds a task function to the worker.
172 * If a task function with the same name already exists, it is replaced.
174 * @param name - The name of the task function to add.
175 * @param fn - The task function to add.
176 * @returns Whether the task function was added or not.
178 public addTaskFunction (
180 fn
: TaskFunction
<Data
, Response
>
181 ): TaskFunctionOperationResult
{
183 checkTaskFunctionName(name
)
184 if (name
=== DEFAULT_TASK_NAME
) {
186 'Cannot add a task function with the default reserved name'
189 if (typeof fn
!== 'function') {
190 throw new TypeError('fn parameter is not a function')
192 const boundFn
= fn
.bind(this)
194 this.taskFunctions
.get(name
) ===
195 this.taskFunctions
.get(DEFAULT_TASK_NAME
)
197 this.taskFunctions
.set(DEFAULT_TASK_NAME
, boundFn
)
199 this.taskFunctions
.set(name
, boundFn
)
200 this.sendTaskFunctionNamesToMainWorker()
201 return { status: true }
203 return { status: false, error
: error
as Error }
208 * Removes a task function from the worker.
210 * @param name - The name of the task function to remove.
211 * @returns Whether the task function existed and was removed or not.
213 public removeTaskFunction (name
: string): TaskFunctionOperationResult
{
215 checkTaskFunctionName(name
)
216 if (name
=== DEFAULT_TASK_NAME
) {
218 'Cannot remove the task function with the default reserved name'
222 this.taskFunctions
.get(name
) ===
223 this.taskFunctions
.get(DEFAULT_TASK_NAME
)
226 'Cannot remove the task function used as the default task function'
229 const deleteStatus
= this.taskFunctions
.delete(name
)
230 this.sendTaskFunctionNamesToMainWorker()
231 return { status: deleteStatus
}
233 return { status: false, error
: error
as Error }
238 * Lists the names of the worker's task functions.
240 * @returns The names of the worker's task functions.
242 public listTaskFunctionNames (): string[] {
243 const names
: string[] = [...this.taskFunctions
.keys()]
244 let defaultTaskFunctionName
: string = DEFAULT_TASK_NAME
245 for (const [name
, fn
] of this.taskFunctions
) {
247 name
!== DEFAULT_TASK_NAME
&&
248 fn
=== this.taskFunctions
.get(DEFAULT_TASK_NAME
)
250 defaultTaskFunctionName
= name
255 names
[names
.indexOf(DEFAULT_TASK_NAME
)],
256 defaultTaskFunctionName
,
258 name
=> name
!== DEFAULT_TASK_NAME
&& name
!== defaultTaskFunctionName
264 * Sets the default task function to use in the worker.
266 * @param name - The name of the task function to use as default task function.
267 * @returns Whether the default task function was set or not.
269 public setDefaultTaskFunction (name
: string): TaskFunctionOperationResult
{
271 checkTaskFunctionName(name
)
272 if (name
=== DEFAULT_TASK_NAME
) {
274 'Cannot set the default task function reserved name as the default task function'
277 if (!this.taskFunctions
.has(name
)) {
279 'Cannot set the default task function to a non-existing task function'
282 this.taskFunctions
.set(
284 this.taskFunctions
.get(name
) as TaskFunction
<Data
, Response
>
286 this.sendTaskFunctionNamesToMainWorker()
287 return { status: true }
289 return { status: false, error
: error
as Error }
294 * Handles the ready message sent by the main worker.
296 * @param message - The ready message.
298 protected abstract handleReadyMessage (message
: MessageValue
<Data
>): void
301 * Worker message listener.
303 * @param message - The received message.
305 protected messageListener (message
: MessageValue
<Data
>): void {
306 this.checkMessageWorkerId(message
)
307 if (message
.statistics
!= null) {
308 // Statistics message received
309 this.statistics
= message
.statistics
310 } else if (message
.checkActive
!= null) {
311 // Check active message received
312 message
.checkActive
? this.startCheckActive() : this.stopCheckActive()
313 } else if (message
.taskFunctionOperation
!= null) {
314 // Task function operation message received
315 this.handleTaskFunctionOperationMessage(message
)
316 } else if (message
.taskId
!= null && message
.data
!= null) {
317 // Task message received
319 } else if (message
.kill
=== true) {
320 // Kill message received
321 this.handleKillMessage(message
)
325 protected handleTaskFunctionOperationMessage (
326 message
: MessageValue
<Data
>
328 const { taskFunctionOperation
, taskFunctionName
, taskFunction
} = message
329 let response
!: TaskFunctionOperationResult
330 switch (taskFunctionOperation
) {
332 response
= this.addTaskFunction(
333 taskFunctionName
as string,
334 // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
335 new Function(`return ${taskFunction as string}`)() as TaskFunction
<
342 response
= this.removeTaskFunction(taskFunctionName
as string)
345 response
= this.setDefaultTaskFunction(taskFunctionName
as string)
348 response
= { status: false, error
: new Error('Unknown task operation') }
351 this.sendToMainWorker({
352 taskFunctionOperation
,
353 taskFunctionOperationStatus
: response
.status,
355 ...(!response
.status &&
356 response
?.error
!= null && {
358 name
: taskFunctionName
as string,
359 message
: this.handleError(response
.error
as Error | string)
366 * Handles a kill message sent by the main worker.
368 * @param message - The kill message.
370 protected handleKillMessage (message
: MessageValue
<Data
>): void {
371 this.stopCheckActive()
372 if (isAsyncFunction(this.opts
.killHandler
)) {
373 (this.opts
.killHandler
?.() as Promise
<void>)
375 this.sendToMainWorker({ kill
: 'success' })
379 this.sendToMainWorker({ kill
: 'failure' })
384 .catch(EMPTY_FUNCTION
)
387 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
388 this.opts
.killHandler
?.() as void
389 this.sendToMainWorker({ kill
: 'success' })
391 this.sendToMainWorker({ kill
: 'failure' })
399 * Check if the message worker id is set and matches the worker id.
401 * @param message - The message to check.
402 * @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.
404 private checkMessageWorkerId (message
: MessageValue
<Data
>): void {
405 if (message
.workerId
== null) {
406 throw new Error('Message worker id is not set')
407 } else if (message
.workerId
!= null && message
.workerId
!== this.id
) {
409 `Message worker id ${message.workerId} does not match the worker id ${this.id}`
415 * Starts the worker check active interval.
417 private startCheckActive (): void {
418 this.lastTaskTimestamp
= performance
.now()
419 this.activeInterval
= setInterval(
420 this.checkActive
.bind(this),
421 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
) / 2
426 * Stops the worker check active interval.
428 private stopCheckActive (): void {
429 if (this.activeInterval
!= null) {
430 clearInterval(this.activeInterval
)
431 delete this.activeInterval
436 * Checks if the worker should be terminated, because its living too long.
438 private checkActive (): void {
440 performance
.now() - this.lastTaskTimestamp
>
441 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
)
443 this.sendToMainWorker({ kill
: this.opts
.killBehavior
})
448 * Returns the main worker.
450 * @returns Reference to the main worker.
451 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the main worker is not set.
453 protected getMainWorker (): MainWorker
{
454 if (this.mainWorker
== null) {
455 throw new Error('Main worker not set')
457 return this.mainWorker
461 * Sends a message to main worker.
463 * @param message - The response message.
465 protected abstract sendToMainWorker (
466 message
: MessageValue
<Response
, Data
>
470 * Sends task function names to the main worker.
472 protected sendTaskFunctionNamesToMainWorker (): void {
473 this.sendToMainWorker({
474 taskFunctionNames
: this.listTaskFunctionNames()
479 * Handles an error and convert it to a string so it can be sent back to the main worker.
481 * @param error - The error raised by the worker.
482 * @returns The error message.
484 protected handleError (error
: Error | string): string {
485 return error
instanceof Error ? error
.message
: error
489 * Runs the given task.
491 * @param task - The task to execute.
493 protected run (task
: Task
<Data
>): void {
494 const { name
, taskId
, data
} = task
495 const fn
= this.taskFunctions
.get(name
?? DEFAULT_TASK_NAME
)
497 this.sendToMainWorker({
499 name
: name
as string,
500 message
: `Task function '${name as string}' not found`,
507 if (isAsyncFunction(fn
)) {
508 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, task
)
510 this.runInAsyncScope(this.runSync
.bind(this), this, fn
, task
)
515 * Runs the given task function synchronously.
517 * @param fn - Task function that will be executed.
518 * @param task - Input data for the task function.
521 fn
: TaskSyncFunction
<Data
, Response
>,
524 const { name
, taskId
, data
} = task
526 let taskPerformance
= this.beginTaskPerformance(name
)
528 taskPerformance
= this.endTaskPerformance(taskPerformance
)
529 this.sendToMainWorker({
535 this.sendToMainWorker({
537 name
: name
as string,
538 message
: this.handleError(error
as Error | string),
544 this.updateLastTaskTimestamp()
549 * Runs the given task function asynchronously.
551 * @param fn - Task function that will be executed.
552 * @param task - Input data for the task function.
555 fn
: TaskAsyncFunction
<Data
, Response
>,
558 const { name
, taskId
, data
} = task
559 let taskPerformance
= this.beginTaskPerformance(name
)
562 taskPerformance
= this.endTaskPerformance(taskPerformance
)
563 this.sendToMainWorker({
571 this.sendToMainWorker({
573 name
: name
as string,
574 message
: this.handleError(error
as Error | string),
581 this.updateLastTaskTimestamp()
583 .catch(EMPTY_FUNCTION
)
586 private beginTaskPerformance (name
?: string): TaskPerformance
{
587 this.checkStatistics()
589 name
: name
?? DEFAULT_TASK_NAME
,
590 timestamp
: performance
.now(),
591 ...(this.statistics
.elu
&& { elu
: performance
.eventLoopUtilization() })
595 private endTaskPerformance (
596 taskPerformance
: TaskPerformance
598 this.checkStatistics()
601 ...(this.statistics
.runTime
&& {
602 runTime
: performance
.now() - taskPerformance
.timestamp
604 ...(this.statistics
.elu
&& {
605 elu
: performance
.eventLoopUtilization(taskPerformance
.elu
)
610 private checkStatistics (): void {
611 if (this.statistics
== null) {
612 throw new Error('Performance statistics computation requirements not set')
616 private updateLastTaskTimestamp (): void {
617 if (this.activeInterval
!= null) {
618 this.lastTaskTimestamp
= performance
.now()