1 import type { Worker
} from
'node:cluster'
2 import { performance
} from
'node:perf_hooks'
3 import type { MessagePort
} from
'node:worker_threads'
8 TaskFunctionProperties
,
11 } from
'../utility-types.js'
13 buildTaskFunctionProperties
,
23 TaskFunctionOperationResult
,
26 } from
'./task-functions.js'
28 checkTaskFunctionName
,
29 checkValidTaskFunctionObjectEntry
,
30 checkValidWorkerOptions
,
32 import { KillBehaviors
, type WorkerOptions
} from
'./worker-options.js'
34 const DEFAULT_MAX_INACTIVE_TIME
= 60000
35 const DEFAULT_WORKER_OPTIONS
: WorkerOptions
= {
37 * The kill behavior option on this worker or its default value.
39 killBehavior
: KillBehaviors
.SOFT
,
41 * The maximum time to keep this worker active while idle.
42 * The pool automatically checks and terminates this worker when the time expires.
44 maxInactiveTime
: DEFAULT_MAX_INACTIVE_TIME
,
46 * The function to call when the worker is killed.
48 killHandler
: EMPTY_FUNCTION
,
52 * Base class that implements some shared logic for all poolifier workers.
53 * @typeParam MainWorker - Type of main worker.
54 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
55 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
57 export abstract class AbstractWorker
<
58 MainWorker
extends Worker
| MessagePort
,
65 protected abstract id
: number
67 * Task function object(s) processed by the worker when the pool's `execution` function is invoked.
69 protected taskFunctions
!: Map
<string, TaskFunctionObject
<Data
, Response
>>
71 * Timestamp of the last task processed by this worker.
73 protected lastTaskTimestamp
!: number
75 * Performance statistics computation requirements.
77 protected statistics
?: WorkerStatistics
79 * Handler id of the `activeInterval` worker activity check.
81 protected activeInterval
?: NodeJS
.Timeout
84 * Constructs a new poolifier worker.
85 * @param isMain - Whether this is the main worker or not.
86 * @param mainWorker - Reference to main worker.
87 * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function.
88 * @param opts - Options for the worker.
91 protected readonly isMain
: boolean | undefined,
92 private readonly mainWorker
: MainWorker
| undefined | null,
93 taskFunctions
: TaskFunction
<Data
, Response
> | TaskFunctions
<Data
, Response
>,
94 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 // Should be once() but Node.js on windows has a bug that prevents it from working
103 this.getMainWorker().on('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.
114 * @param taskFunctions - The task function(s) parameter that should be checked.
116 private checkTaskFunctions (
118 | TaskFunction
<Data
, Response
>
119 | TaskFunctions
<Data
, Response
>
122 if (taskFunctions
== null) {
123 throw new Error('taskFunctions parameter is mandatory')
125 this.taskFunctions
= new Map
<string, TaskFunctionObject
<Data
, Response
>>()
126 if (typeof taskFunctions
=== 'function') {
127 const fnObj
= { taskFunction
: taskFunctions
.bind(this) }
128 this.taskFunctions
.set(DEFAULT_TASK_NAME
, fnObj
)
129 this.taskFunctions
.set(
130 typeof taskFunctions
.name
=== 'string' &&
131 taskFunctions
.name
.trim().length
> 0
136 } else if (isPlainObject(taskFunctions
)) {
137 let firstEntry
= true
138 for (let [name
, fnObj
] of Object.entries(taskFunctions
)) {
139 if (typeof fnObj
=== 'function') {
140 fnObj
= { taskFunction
: fnObj
} satisfies TaskFunctionObject
<
145 checkValidTaskFunctionObjectEntry
<Data
, Response
>(name
, fnObj
)
146 fnObj
.taskFunction
= fnObj
.taskFunction
.bind(this)
148 this.taskFunctions
.set(DEFAULT_TASK_NAME
, fnObj
)
151 this.taskFunctions
.set(name
, fnObj
)
154 throw new Error('taskFunctions parameter object is empty')
158 'taskFunctions parameter is not a function or a plain object'
164 * Checks if the worker has a task function with the given name.
165 * @param name - The name of the task function to check.
166 * @returns Whether the worker has a task function with the given name or not.
168 public hasTaskFunction (name
: string): TaskFunctionOperationResult
{
170 checkTaskFunctionName(name
)
172 return { status: false, error
: error
as Error }
174 return { status: this.taskFunctions
.has(name
) }
178 * Adds a task function to the worker.
179 * If a task function with the same name already exists, it is replaced.
180 * @param name - The name of the task function to add.
181 * @param fn - The task function to add.
182 * @returns Whether the task function was added or not.
184 public addTaskFunction (
186 fn
: TaskFunction
<Data
, Response
> | TaskFunctionObject
<Data
, Response
>
187 ): TaskFunctionOperationResult
{
189 checkTaskFunctionName(name
)
190 if (name
=== DEFAULT_TASK_NAME
) {
192 'Cannot add a task function with the default reserved name'
195 if (typeof fn
=== 'function') {
196 fn
= { taskFunction
: fn
} satisfies TaskFunctionObject
<Data
, Response
>
198 checkValidTaskFunctionObjectEntry
<Data
, Response
>(name
, fn
)
199 fn
.taskFunction
= fn
.taskFunction
.bind(this)
201 this.taskFunctions
.get(name
) ===
202 this.taskFunctions
.get(DEFAULT_TASK_NAME
)
204 this.taskFunctions
.set(DEFAULT_TASK_NAME
, fn
)
206 this.taskFunctions
.set(name
, fn
)
207 this.sendTaskFunctionsPropertiesToMainWorker()
208 return { status: true }
210 return { status: false, error
: error
as Error }
215 * Removes a task function from the worker.
216 * @param name - The name of the task function to remove.
217 * @returns Whether the task function existed and was removed or not.
219 public removeTaskFunction (name
: string): TaskFunctionOperationResult
{
221 checkTaskFunctionName(name
)
222 if (name
=== DEFAULT_TASK_NAME
) {
224 'Cannot remove the task function with the default reserved name'
228 this.taskFunctions
.get(name
) ===
229 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.sendTaskFunctionsPropertiesToMainWorker()
237 return { status: deleteStatus
}
239 return { status: false, error
: error
as Error }
244 * Lists the properties of the worker's task functions.
245 * @returns The properties of the worker's task functions.
247 public listTaskFunctionsProperties (): TaskFunctionProperties
[] {
248 let defaultTaskFunctionName
= DEFAULT_TASK_NAME
249 for (const [name
, fnObj
] of this.taskFunctions
) {
251 name
!== DEFAULT_TASK_NAME
&&
252 fnObj
=== this.taskFunctions
.get(DEFAULT_TASK_NAME
)
254 defaultTaskFunctionName
= name
258 const taskFunctionsProperties
: TaskFunctionProperties
[] = []
259 for (const [name
, fnObj
] of this.taskFunctions
) {
260 if (name
=== DEFAULT_TASK_NAME
|| name
=== defaultTaskFunctionName
) {
263 taskFunctionsProperties
.push(buildTaskFunctionProperties(name
, fnObj
))
266 buildTaskFunctionProperties(
268 this.taskFunctions
.get(DEFAULT_TASK_NAME
)
270 buildTaskFunctionProperties(
271 defaultTaskFunctionName
,
272 this.taskFunctions
.get(defaultTaskFunctionName
)
274 ...taskFunctionsProperties
,
279 * Sets the default task function to use in the worker.
280 * @param name - The name of the task function to use as default task function.
281 * @returns Whether the default task function was set or not.
283 public setDefaultTaskFunction (name
: string): TaskFunctionOperationResult
{
285 checkTaskFunctionName(name
)
286 if (name
=== DEFAULT_TASK_NAME
) {
288 'Cannot set the default task function reserved name as the default task function'
291 if (!this.taskFunctions
.has(name
)) {
293 'Cannot set the default task function to a non-existing task function'
296 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
297 this.taskFunctions
.set(DEFAULT_TASK_NAME
, this.taskFunctions
.get(name
)!)
298 this.sendTaskFunctionsPropertiesToMainWorker()
299 return { status: true }
301 return { status: false, error
: error
as Error }
306 * Handles the ready message sent by the main worker.
307 * @param message - The ready message.
309 protected abstract handleReadyMessage (message
: MessageValue
<Data
>): void
312 * Worker message listener.
313 * @param message - The received message.
315 protected messageListener (message
: MessageValue
<Data
>): void {
316 this.checkMessageWorkerId(message
)
320 taskFunctionOperation
,
325 if (statistics
!= null) {
326 // Statistics message received
327 this.statistics
= statistics
328 } else if (checkActive
!= null) {
329 // Check active message received
330 checkActive
? this.startCheckActive() : this.stopCheckActive()
331 } else if (taskFunctionOperation
!= null) {
332 // Task function operation message received
333 this.handleTaskFunctionOperationMessage(message
)
334 } else if (taskId
!= null && data
!= null) {
335 // Task message received
337 } else if (kill
=== true) {
338 // Kill message received
339 this.handleKillMessage(message
)
343 protected handleTaskFunctionOperationMessage (
344 message
: MessageValue
<Data
>
346 const { taskFunctionOperation
, taskFunctionProperties
, taskFunction
} =
348 if (taskFunctionProperties
== null) {
350 'Cannot handle task function operation message without task function properties'
353 let response
: TaskFunctionOperationResult
354 switch (taskFunctionOperation
) {
356 response
= this.addTaskFunction(taskFunctionProperties
.name
, {
357 // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
358 taskFunction
: new Function(
359 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
360 `return ${taskFunction!}`
361 )() as TaskFunction
<Data
, Response
>,
362 ...(taskFunctionProperties
.priority
!= null && {
363 priority
: taskFunctionProperties
.priority
,
365 ...(taskFunctionProperties
.strategy
!= null && {
366 strategy
: taskFunctionProperties
.strategy
,
371 response
= this.removeTaskFunction(taskFunctionProperties
.name
)
374 response
= this.setDefaultTaskFunction(taskFunctionProperties
.name
)
377 response
= { status: false, error
: new Error('Unknown task operation') }
380 const { status, error
} = response
381 this.sendToMainWorker({
382 taskFunctionOperation
,
383 taskFunctionOperationStatus
: status,
384 taskFunctionProperties
,
388 name
: taskFunctionProperties
.name
,
389 message
: this.handleError(error
as Error | string),
396 * Handles a kill message sent by the main worker.
397 * @param message - The kill message.
399 protected handleKillMessage (message
: MessageValue
<Data
>): void {
400 this.stopCheckActive()
401 if (isAsyncFunction(this.opts
.killHandler
)) {
402 ;(this.opts
.killHandler
as () => Promise
<void>)()
404 this.sendToMainWorker({ kill
: 'success' })
408 this.sendToMainWorker({ kill
: 'failure' })
412 ;(this.opts
.killHandler
as (() => void) | undefined)?.()
413 this.sendToMainWorker({ kill
: 'success' })
415 this.sendToMainWorker({ kill
: 'failure' })
421 * Check if the message worker id is set and matches the worker id.
422 * @param message - The message to check.
423 * @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.
425 private checkMessageWorkerId (message
: MessageValue
<Data
>): void {
426 if (message
.workerId
== null) {
427 throw new Error('Message worker id is not set')
428 } else if (message
.workerId
!== this.id
) {
430 `Message worker id ${message.workerId.toString()} does not match the worker id ${this.id.toString()}`
436 * Starts the worker check active interval.
438 private startCheckActive (): void {
439 this.lastTaskTimestamp
= performance
.now()
440 this.activeInterval
= setInterval(
441 this.checkActive
.bind(this),
442 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
) / 2
447 * Stops the worker check active interval.
449 private stopCheckActive (): void {
450 if (this.activeInterval
!= null) {
451 clearInterval(this.activeInterval
)
452 delete this.activeInterval
457 * Checks if the worker should be terminated, because its living too long.
459 private checkActive (): void {
461 performance
.now() - this.lastTaskTimestamp
>
462 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
)
464 this.sendToMainWorker({ kill
: this.opts
.killBehavior
})
469 * 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.
482 * @param message - The response message.
484 protected abstract sendToMainWorker (
485 message
: MessageValue
<Response
, Data
>
489 * Sends task functions properties to the main worker.
491 protected sendTaskFunctionsPropertiesToMainWorker (): void {
492 this.sendToMainWorker({
493 taskFunctionsProperties
: this.listTaskFunctionsProperties(),
498 * Handles an error and convert it to a string so it can be sent back to the main worker.
499 * @param error - The error raised by the worker.
500 * @returns The error message.
502 protected handleError (error
: Error | string): string {
503 return error
instanceof Error ? error
.message
: error
507 * Runs the given task.
508 * @param task - The task to execute.
510 protected readonly run
= (task
: Task
<Data
>): void => {
511 const { name
, taskId
, data
} = task
512 const taskFunctionName
= name
?? DEFAULT_TASK_NAME
513 if (!this.taskFunctions
.has(taskFunctionName
)) {
514 this.sendToMainWorker({
516 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
518 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
519 message
: `Task function '${name!}' not found`,
526 const fn
= this.taskFunctions
.get(taskFunctionName
)?.taskFunction
527 if (isAsyncFunction(fn
)) {
528 this.runAsync(fn
as TaskAsyncFunction
<Data
, Response
>, task
)
530 this.runSync(fn
as TaskSyncFunction
<Data
, Response
>, task
)
535 * Runs the given task function synchronously.
536 * @param fn - Task function that will be executed.
537 * @param task - Input data for the task function.
539 protected readonly runSync
= (
540 fn
: TaskSyncFunction
<Data
, Response
>,
543 const { name
, taskId
, data
} = task
545 let taskPerformance
= this.beginTaskPerformance(name
)
547 taskPerformance
= this.endTaskPerformance(taskPerformance
)
548 this.sendToMainWorker({
554 this.sendToMainWorker({
556 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
558 message
: this.handleError(error
as Error | string),
564 this.updateLastTaskTimestamp()
569 * Runs the given task function asynchronously.
570 * @param fn - Task function that will be executed.
571 * @param task - Input data for the task function.
573 protected readonly runAsync
= (
574 fn
: TaskAsyncFunction
<Data
, Response
>,
577 const { name
, taskId
, data
} = task
578 let taskPerformance
= this.beginTaskPerformance(name
)
581 taskPerformance
= this.endTaskPerformance(taskPerformance
)
582 this.sendToMainWorker({
589 .catch((error
: unknown
) => {
590 this.sendToMainWorker({
592 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
594 message
: this.handleError(error
as Error | string),
601 this.updateLastTaskTimestamp()
603 .catch(EMPTY_FUNCTION
)
606 private beginTaskPerformance (name
?: string): TaskPerformance
{
607 if (this.statistics
== null) {
608 throw new Error('Performance statistics computation requirements not set')
611 name
: name
?? DEFAULT_TASK_NAME
,
612 timestamp
: performance
.now(),
613 ...(this.statistics
.elu
&& {
614 elu
: performance
.eventLoopUtilization(),
619 private endTaskPerformance (
620 taskPerformance
: TaskPerformance
622 if (this.statistics
== null) {
623 throw new Error('Performance statistics computation requirements not set')
627 ...(this.statistics
.runTime
&& {
628 runTime
: performance
.now() - taskPerformance
.timestamp
,
630 ...(this.statistics
.elu
&& {
631 elu
: performance
.eventLoopUtilization(taskPerformance
.elu
),
636 private updateLastTaskTimestamp (): void {
637 if (this.activeInterval
!= null) {
638 this.lastTaskTimestamp
= performance
.now()