1 import type { Worker
} from
'node:cluster'
2 import type { MessagePort
} from
'node:worker_threads'
3 import { performance
} from
'node:perf_hooks'
9 } from
'../utility-types'
16 import { KillBehaviors
, type WorkerOptions
} from
'./worker-options'
20 TaskFunctionOperationResult
,
23 } from
'./task-functions'
25 checkTaskFunctionName
,
26 checkValidTaskFunctionEntry
,
27 checkValidWorkerOptions
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
,
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 // 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.
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' })
383 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
384 this.opts
.killHandler
?.() as void
385 this.sendToMainWorker({ kill
: 'success' })
387 this.sendToMainWorker({ kill
: 'failure' })
393 * Check if the message worker id is set and matches the worker id.
395 * @param message - The message to check.
396 * @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.
398 private checkMessageWorkerId (message
: MessageValue
<Data
>): void {
399 if (message
.workerId
== null) {
400 throw new Error('Message worker id is not set')
401 } else if (message
.workerId
!== this.id
) {
403 `Message worker id ${message.workerId} does not match the worker id ${this.id}`
409 * Starts the worker check active interval.
411 private startCheckActive (): void {
412 this.lastTaskTimestamp
= performance
.now()
413 this.activeInterval
= setInterval(
414 this.checkActive
.bind(this),
415 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
) / 2
420 * Stops the worker check active interval.
422 private stopCheckActive (): void {
423 if (this.activeInterval
!= null) {
424 clearInterval(this.activeInterval
)
425 delete this.activeInterval
430 * Checks if the worker should be terminated, because its living too long.
432 private checkActive (): void {
434 performance
.now() - this.lastTaskTimestamp
>
435 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
)
437 this.sendToMainWorker({ kill
: this.opts
.killBehavior
})
442 * Returns the main worker.
444 * @returns Reference to the main worker.
445 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the main worker is not set.
447 protected getMainWorker (): MainWorker
{
448 if (this.mainWorker
== null) {
449 throw new Error('Main worker not set')
451 return this.mainWorker
455 * Sends a message to main worker.
457 * @param message - The response message.
459 protected abstract sendToMainWorker (
460 message
: MessageValue
<Response
, Data
>
464 * Sends task function names to the main worker.
466 protected sendTaskFunctionNamesToMainWorker (): void {
467 this.sendToMainWorker({
468 taskFunctionNames
: this.listTaskFunctionNames()
473 * Handles an error and convert it to a string so it can be sent back to the main worker.
475 * @param error - The error raised by the worker.
476 * @returns The error message.
478 protected handleError (error
: Error | string): string {
479 return error
instanceof Error ? error
.message
: error
483 * Runs the given task.
485 * @param task - The task to execute.
487 protected run (task
: Task
<Data
>): void {
488 const { name
, taskId
, data
} = task
489 const taskFunctionName
= name
?? DEFAULT_TASK_NAME
490 if (!this.taskFunctions
.has(taskFunctionName
)) {
491 this.sendToMainWorker({
493 name
: name
as string,
494 message
: `Task function '${name as string}' not found`,
501 const fn
= this.taskFunctions
.get(taskFunctionName
)
502 if (isAsyncFunction(fn
)) {
503 this.runAsync(fn
as TaskAsyncFunction
<Data
, Response
>, task
)
505 this.runSync(fn
as TaskSyncFunction
<Data
, Response
>, task
)
510 * Runs the given task function synchronously.
512 * @param fn - Task function that will be executed.
513 * @param task - Input data for the task function.
516 fn
: TaskSyncFunction
<Data
, Response
>,
519 const { name
, taskId
, data
} = task
521 let taskPerformance
= this.beginTaskPerformance(name
)
523 taskPerformance
= this.endTaskPerformance(taskPerformance
)
524 this.sendToMainWorker({
530 this.sendToMainWorker({
532 name
: name
as string,
533 message
: this.handleError(error
as Error | string),
539 this.updateLastTaskTimestamp()
544 * Runs the given task function asynchronously.
546 * @param fn - Task function that will be executed.
547 * @param task - Input data for the task function.
550 fn
: TaskAsyncFunction
<Data
, Response
>,
553 const { name
, taskId
, data
} = task
554 let taskPerformance
= this.beginTaskPerformance(name
)
557 taskPerformance
= this.endTaskPerformance(taskPerformance
)
558 this.sendToMainWorker({
566 this.sendToMainWorker({
568 name
: name
as string,
569 message
: this.handleError(error
as Error | string),
576 this.updateLastTaskTimestamp()
578 .catch(EMPTY_FUNCTION
)
581 private beginTaskPerformance (name
?: string): TaskPerformance
{
582 this.checkStatistics()
584 name
: name
?? DEFAULT_TASK_NAME
,
585 timestamp
: performance
.now(),
586 ...(this.statistics
.elu
&& { elu
: performance
.eventLoopUtilization() })
590 private endTaskPerformance (
591 taskPerformance
: TaskPerformance
593 this.checkStatistics()
596 ...(this.statistics
.runTime
&& {
597 runTime
: performance
.now() - taskPerformance
.timestamp
599 ...(this.statistics
.elu
&& {
600 elu
: performance
.eventLoopUtilization(taskPerformance
.elu
)
605 private checkStatistics (): void {
606 if (this.statistics
== null) {
607 throw new Error('Performance statistics computation requirements not set')
611 private updateLastTaskTimestamp (): void {
612 if (this.activeInterval
!= null) {
613 this.lastTaskTimestamp
= performance
.now()