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.js'
16 import { KillBehaviors
, type WorkerOptions
} from
'./worker-options.js'
20 TaskFunctionOperationResult
,
23 } from
'./task-functions.js'
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
81 * Constructs a new poolifier worker.
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.
89 protected readonly isMain
: boolean,
90 private readonly mainWorker
: MainWorker
,
91 taskFunctions
: TaskFunction
<Data
, Response
> | TaskFunctions
<Data
, Response
>,
92 protected opts
: WorkerOptions
= DEFAULT_WORKER_OPTIONS
94 if (this.isMain
== null) {
95 throw new Error('isMain parameter is mandatory')
97 this.checkTaskFunctions(taskFunctions
)
98 this.checkWorkerOptions(this.opts
)
100 // Should be once() but Node.js on windows has a bug that prevents it from working
101 this.getMainWorker().on('message', this.handleReadyMessage
.bind(this))
105 private checkWorkerOptions (opts
: WorkerOptions
): void {
106 checkValidWorkerOptions(opts
)
107 this.opts
= { ...DEFAULT_WORKER_OPTIONS
, ...opts
}
111 * Checks if the `taskFunctions` parameter is passed to the constructor and valid.
113 * @param taskFunctions - The task function(s) parameter that should be checked.
115 private checkTaskFunctions (
116 taskFunctions
: TaskFunction
<Data
, Response
> | TaskFunctions
<Data
, Response
>
118 if (taskFunctions
== null) {
119 throw new Error('taskFunctions parameter is mandatory')
121 this.taskFunctions
= new Map
<string, TaskFunction
<Data
, Response
>>()
122 if (typeof taskFunctions
=== 'function') {
123 const boundFn
= taskFunctions
.bind(this)
124 this.taskFunctions
.set(DEFAULT_TASK_NAME
, boundFn
)
125 this.taskFunctions
.set(
126 typeof taskFunctions
.name
=== 'string' &&
127 taskFunctions
.name
.trim().length
> 0
132 } else if (isPlainObject(taskFunctions
)) {
133 let firstEntry
= true
134 for (const [name
, fn
] of Object.entries(taskFunctions
)) {
135 checkValidTaskFunctionEntry
<Data
, Response
>(name
, fn
)
136 const boundFn
= fn
.bind(this)
138 this.taskFunctions
.set(DEFAULT_TASK_NAME
, boundFn
)
141 this.taskFunctions
.set(name
, boundFn
)
144 throw new Error('taskFunctions parameter object is empty')
148 'taskFunctions parameter is not a function or a plain object'
154 * Checks if the worker has a task function with the given name.
156 * @param name - The name of the task function to check.
157 * @returns Whether the worker has a task function with the given name or not.
159 public hasTaskFunction (name
: string): TaskFunctionOperationResult
{
161 checkTaskFunctionName(name
)
163 return { status: false, error
: error
as Error }
165 return { status: this.taskFunctions
.has(name
) }
169 * Adds a task function to the worker.
170 * If a task function with the same name already exists, it is replaced.
172 * @param name - The name of the task function to add.
173 * @param fn - The task function to add.
174 * @returns Whether the task function was added or not.
176 public addTaskFunction (
178 fn
: TaskFunction
<Data
, Response
>
179 ): TaskFunctionOperationResult
{
181 checkTaskFunctionName(name
)
182 if (name
=== DEFAULT_TASK_NAME
) {
184 'Cannot add a task function with the default reserved name'
187 if (typeof fn
!== 'function') {
188 throw new TypeError('fn parameter is not a function')
190 const boundFn
= fn
.bind(this)
192 this.taskFunctions
.get(name
) ===
193 this.taskFunctions
.get(DEFAULT_TASK_NAME
)
195 this.taskFunctions
.set(DEFAULT_TASK_NAME
, boundFn
)
197 this.taskFunctions
.set(name
, boundFn
)
198 this.sendTaskFunctionNamesToMainWorker()
199 return { status: true }
201 return { status: false, error
: error
as Error }
206 * Removes a task function from the worker.
208 * @param name - The name of the task function to remove.
209 * @returns Whether the task function existed and was removed or not.
211 public removeTaskFunction (name
: string): TaskFunctionOperationResult
{
213 checkTaskFunctionName(name
)
214 if (name
=== DEFAULT_TASK_NAME
) {
216 'Cannot remove the task function with the default reserved name'
220 this.taskFunctions
.get(name
) ===
221 this.taskFunctions
.get(DEFAULT_TASK_NAME
)
224 'Cannot remove the task function used as the default task function'
227 const deleteStatus
= this.taskFunctions
.delete(name
)
228 this.sendTaskFunctionNamesToMainWorker()
229 return { status: deleteStatus
}
231 return { status: false, error
: error
as Error }
236 * Lists the names of the worker's task functions.
238 * @returns The names of the worker's task functions.
240 public listTaskFunctionNames (): string[] {
241 const names
: string[] = [...this.taskFunctions
.keys()]
242 let defaultTaskFunctionName
: string = DEFAULT_TASK_NAME
243 for (const [name
, fn
] of this.taskFunctions
) {
245 name
!== DEFAULT_TASK_NAME
&&
246 fn
=== this.taskFunctions
.get(DEFAULT_TASK_NAME
)
248 defaultTaskFunctionName
= name
253 names
[names
.indexOf(DEFAULT_TASK_NAME
)],
254 defaultTaskFunctionName
,
256 name
=> name
!== DEFAULT_TASK_NAME
&& name
!== defaultTaskFunctionName
262 * Sets the default task function to use in the worker.
264 * @param name - The name of the task function to use as default task function.
265 * @returns Whether the default task function was set or not.
267 public setDefaultTaskFunction (name
: string): TaskFunctionOperationResult
{
269 checkTaskFunctionName(name
)
270 if (name
=== DEFAULT_TASK_NAME
) {
272 'Cannot set the default task function reserved name as the default task function'
275 if (!this.taskFunctions
.has(name
)) {
277 'Cannot set the default task function to a non-existing task function'
280 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
281 this.taskFunctions
.set(DEFAULT_TASK_NAME
, this.taskFunctions
.get(name
)!)
282 this.sendTaskFunctionNamesToMainWorker()
283 return { status: true }
285 return { status: false, error
: error
as Error }
290 * Handles the ready message sent by the main worker.
292 * @param message - The ready message.
294 protected abstract handleReadyMessage (message
: MessageValue
<Data
>): void
297 * Worker message listener.
299 * @param message - The received message.
301 protected messageListener (message
: MessageValue
<Data
>): void {
302 this.checkMessageWorkerId(message
)
303 if (message
.statistics
!= null) {
304 // Statistics message received
305 this.statistics
= message
.statistics
306 } else if (message
.checkActive
!= null) {
307 // Check active message received
308 message
.checkActive
? this.startCheckActive() : this.stopCheckActive()
309 } else if (message
.taskFunctionOperation
!= null) {
310 // Task function operation message received
311 this.handleTaskFunctionOperationMessage(message
)
312 } else if (message
.taskId
!= null && message
.data
!= null) {
313 // Task message received
315 } else if (message
.kill
=== true) {
316 // Kill message received
317 this.handleKillMessage(message
)
321 protected handleTaskFunctionOperationMessage (
322 message
: MessageValue
<Data
>
324 const { taskFunctionOperation
, taskFunctionName
, taskFunction
} = message
325 let response
!: TaskFunctionOperationResult
326 switch (taskFunctionOperation
) {
328 response
= this.addTaskFunction(
329 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
331 // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func, @typescript-eslint/no-non-null-assertion
332 new Function(`return ${taskFunction!}`)() as TaskFunction
<
339 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
340 response
= this.removeTaskFunction(taskFunctionName
!)
343 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
344 response
= this.setDefaultTaskFunction(taskFunctionName
!)
347 response
= { status: false, error
: new Error('Unknown task operation') }
350 this.sendToMainWorker({
351 taskFunctionOperation
,
352 taskFunctionOperationStatus
: response
.status,
354 ...(!response
.status &&
355 response
?.error
!= null && {
357 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
358 name
: taskFunctionName
!,
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 readonly 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 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
495 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
496 message
: `Task function '${name!}' not found`,
503 const fn
= this.taskFunctions
.get(taskFunctionName
)
504 if (isAsyncFunction(fn
)) {
505 this.runAsync(fn
as TaskAsyncFunction
<Data
, Response
>, task
)
507 this.runSync(fn
as TaskSyncFunction
<Data
, Response
>, task
)
512 * Runs the given task function synchronously.
514 * @param fn - Task function that will be executed.
515 * @param task - Input data for the task function.
517 protected readonly runSync
= (
518 fn
: TaskSyncFunction
<Data
, Response
>,
521 const { name
, taskId
, data
} = task
523 let taskPerformance
= this.beginTaskPerformance(name
)
525 taskPerformance
= this.endTaskPerformance(taskPerformance
)
526 this.sendToMainWorker({
532 this.sendToMainWorker({
534 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
536 message
: this.handleError(error
as Error | string),
542 this.updateLastTaskTimestamp()
547 * Runs the given task function asynchronously.
549 * @param fn - Task function that will be executed.
550 * @param task - Input data for the task function.
552 protected readonly runAsync
= (
553 fn
: TaskAsyncFunction
<Data
, Response
>,
556 const { name
, taskId
, data
} = task
557 let taskPerformance
= this.beginTaskPerformance(name
)
560 taskPerformance
= this.endTaskPerformance(taskPerformance
)
561 this.sendToMainWorker({
569 this.sendToMainWorker({
571 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
573 message
: this.handleError(error
as Error | string),
580 this.updateLastTaskTimestamp()
582 .catch(EMPTY_FUNCTION
)
585 private beginTaskPerformance (name
?: string): TaskPerformance
{
586 this.checkStatistics()
588 name
: name
?? DEFAULT_TASK_NAME
,
589 timestamp
: performance
.now(),
590 ...(this.statistics
.elu
&& { elu
: performance
.eventLoopUtilization() })
594 private endTaskPerformance (
595 taskPerformance
: TaskPerformance
597 this.checkStatistics()
600 ...(this.statistics
.runTime
&& {
601 runTime
: performance
.now() - taskPerformance
.timestamp
603 ...(this.statistics
.elu
&& {
604 elu
: performance
.eventLoopUtilization(taskPerformance
.elu
)
609 private checkStatistics (): void {
610 if (this.statistics
== null) {
611 throw new Error('Performance statistics computation requirements not set')
615 private updateLastTaskTimestamp (): void {
616 if (this.activeInterval
!= null) {
617 this.lastTaskTimestamp
= performance
.now()