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'
21 } from
'./worker-options'
27 } from
'./worker-functions'
29 const DEFAULT_MAX_INACTIVE_TIME
= 60000
30 const DEFAULT_KILL_BEHAVIOR
: KillBehavior
= KillBehaviors
.SOFT
33 * Base class that implements some shared logic for all poolifier workers.
35 * @typeParam MainWorker - Type of main worker.
36 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
37 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
39 export abstract class AbstractWorker
<
40 MainWorker
extends Worker
| MessagePort
,
43 > extends AsyncResource
{
47 protected abstract id
: number
49 * Task function(s) processed by the worker when the pool's `execution` function is invoked.
51 protected taskFunctions
!: Map
<string, WorkerFunction
<Data
, Response
>>
53 * Timestamp of the last task processed by this worker.
55 protected lastTaskTimestamp
!: number
57 * Performance statistics computation requirements.
59 protected statistics
!: WorkerStatistics
61 * Handler id of the `aliveInterval` worker alive check.
63 protected aliveInterval
?: NodeJS
.Timeout
65 * Constructs a new poolifier worker.
67 * @param type - The type of async event.
68 * @param isMain - Whether this is the main worker or not.
69 * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function.
70 * @param mainWorker - Reference to main worker.
71 * @param opts - Options for the worker.
75 protected readonly isMain
: boolean,
77 | WorkerFunction
<Data
, Response
>
78 | TaskFunctions
<Data
, Response
>,
79 protected readonly mainWorker
: MainWorker
,
80 protected readonly opts
: WorkerOptions
= {
82 * The kill behavior option on this worker or its default value.
84 killBehavior
: DEFAULT_KILL_BEHAVIOR
,
86 * The maximum time to keep this worker alive while idle.
87 * The pool automatically checks and terminates this worker when the time expires.
89 maxInactiveTime
: DEFAULT_MAX_INACTIVE_TIME
93 this.checkWorkerOptions(this.opts
)
94 this.checkTaskFunctions(taskFunctions
)
96 this.mainWorker
?.on('message', this.messageListener
.bind(this))
100 private checkWorkerOptions (opts
: WorkerOptions
): void {
101 this.opts
.killBehavior
= opts
.killBehavior
?? DEFAULT_KILL_BEHAVIOR
102 this.opts
.maxInactiveTime
=
103 opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
104 delete this.opts
.async
108 * Checks if the `taskFunctions` parameter is passed to the constructor.
110 * @param taskFunctions - The task function(s) parameter that should be checked.
112 private checkTaskFunctions (
114 | WorkerFunction
<Data
, Response
>
115 | TaskFunctions
<Data
, Response
>
117 if (taskFunctions
== null) {
118 throw new Error('taskFunctions parameter is mandatory')
120 this.taskFunctions
= new Map
<string, WorkerFunction
<Data
, Response
>>()
121 if (typeof taskFunctions
=== 'function') {
122 this.taskFunctions
.set(DEFAULT_TASK_NAME
, taskFunctions
.bind(this))
123 } else if (isPlainObject(taskFunctions
)) {
124 let firstEntry
= true
125 for (const [name
, fn
] of Object.entries(taskFunctions
)) {
126 if (typeof name
!== 'string') {
128 'A taskFunctions parameter object key is not a string'
131 if (typeof fn
!== 'function') {
133 'A taskFunctions parameter object value is not a function'
136 this.taskFunctions
.set(name
, fn
.bind(this))
138 this.taskFunctions
.set(DEFAULT_TASK_NAME
, fn
.bind(this))
143 throw new Error('taskFunctions parameter object is empty')
147 'taskFunctions parameter is not a function or a plain object'
153 * Checks if the worker has a task function with the given name.
155 * @param name - The name of the task function to check.
156 * @returns Whether the worker has a task function with the given name or not.
157 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string.
159 public hasTaskFunction (name
: string): boolean {
160 if (typeof name
!== 'string') {
161 throw new TypeError('name parameter is not a string')
163 return this.taskFunctions
.has(name
)
167 * Adds a task function to the worker.
168 * If a task function with the same name already exists, it is replaced.
170 * @param name - The name of the task function to add.
171 * @param fn - The task function to add.
172 * @returns Whether the task function was added or not.
173 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string.
174 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
175 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `fn` parameter is not a function.
177 public addTaskFunction (
179 fn
: WorkerFunction
<Data
, Response
>
181 if (typeof name
!== 'string') {
182 throw new TypeError('name parameter is not a string')
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')
194 this.taskFunctions
.get(name
) ===
195 this.taskFunctions
.get(DEFAULT_TASK_NAME
)
197 this.taskFunctions
.set(DEFAULT_TASK_NAME
, fn
.bind(this))
199 this.taskFunctions
.set(name
, fn
.bind(this))
207 * Removes a task function from the worker.
209 * @param name - The name of the task function to remove.
210 * @returns Whether the task function existed and was removed or not.
211 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string.
212 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
213 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the task function used as default task function.
215 public removeTaskFunction (name
: string): boolean {
216 if (typeof name
!== 'string') {
217 throw new TypeError('name parameter is not a string')
219 if (name
=== DEFAULT_TASK_NAME
) {
221 'Cannot remove the task function with the default reserved name'
225 this.taskFunctions
.get(name
) === this.taskFunctions
.get(DEFAULT_TASK_NAME
)
228 'Cannot remove the task function used as the default task function'
231 return this.taskFunctions
.delete(name
)
235 * Sets the default task function to use when no task function name is specified.
237 * @param name - The name of the task function to use as default task function.
238 * @returns Whether the default task function was set or not.
239 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string.
240 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
241 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is a non-existing task function.
243 public setDefaultTaskFunction (name
: string): boolean {
244 if (typeof name
!== 'string') {
245 throw new TypeError('name parameter is not a string')
247 if (name
=== DEFAULT_TASK_NAME
) {
249 'Cannot set the default task function reserved name as the default task function'
252 if (!this.taskFunctions
.has(name
)) {
254 'Cannot set the default task function to a non-existing task function'
258 this.taskFunctions
.set(
260 this.taskFunctions
.get(name
)?.bind(this) as WorkerFunction
<
272 * Worker message listener.
274 * @param message - Message received.
276 protected messageListener (message
: MessageValue
<Data
, Data
>): void {
277 if (message
.workerId
=== this.id
) {
278 if (message
.ready
!= null) {
279 // Startup message received
281 } else if (message
.statistics
!= null) {
282 // Statistics message received
283 this.statistics
= message
.statistics
284 } else if (message
.checkAlive
!= null) {
285 // Check alive message received
286 message
.checkAlive
? this.startCheckAlive() : this.stopCheckAlive()
287 } else if (message
.id
!= null && message
.data
!= null) {
288 // Task message received
290 } else if (message
.kill
=== true) {
291 // Kill message received
292 this.stopCheckAlive()
299 * Notifies the main worker that this worker is ready to process tasks.
301 protected workerReady (): void {
302 !this.isMain
&& this.sendToMainWorker({ ready
: true, workerId
: this.id
})
306 * Starts the worker alive check interval.
308 private startCheckAlive (): void {
309 this.lastTaskTimestamp
= performance
.now()
310 this.aliveInterval
= setInterval(
311 this.checkAlive
.bind(this),
312 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
) / 2
314 this.checkAlive
.bind(this)()
318 * Stops the worker alive check interval.
320 private stopCheckAlive (): void {
321 this.aliveInterval
!= null && clearInterval(this.aliveInterval
)
325 * Checks if the worker should be terminated, because its living too long.
327 private checkAlive (): void {
329 performance
.now() - this.lastTaskTimestamp
>
330 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
)
332 this.sendToMainWorker({ kill
: this.opts
.killBehavior
, workerId
: this.id
})
337 * Returns the main worker.
339 * @returns Reference to the main worker.
341 protected getMainWorker (): MainWorker
{
342 if (this.mainWorker
== null) {
343 throw new Error('Main worker not set')
345 return this.mainWorker
349 * Sends a message to the main worker.
351 * @param message - The response message.
353 protected abstract sendToMainWorker (
354 message
: MessageValue
<Response
, Data
>
358 * Handles an error and convert it to a string so it can be sent back to the main worker.
360 * @param e - The error raised by the worker.
361 * @returns The error message.
363 protected handleError (e
: Error | string): string {
364 return e
instanceof Error ? e
.message
: e
368 * Runs the given task.
370 * @param task - The task to execute.
371 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found.
373 protected run (task
: Task
<Data
>): void {
374 const fn
= this.getTaskFunction(task
.name
)
375 if (isAsyncFunction(fn
)) {
376 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, task
)
378 this.runInAsyncScope(this.runSync
.bind(this), this, fn
, task
)
383 * Runs the given function synchronously.
385 * @param fn - Task function that will be executed.
386 * @param task - Input data for the task function.
389 fn
: WorkerSyncFunction
<Data
, Response
>,
393 let taskPerformance
= this.beginTaskPerformance(task
.name
)
394 const res
= fn(task
.data
)
395 taskPerformance
= this.endTaskPerformance(taskPerformance
)
396 this.sendToMainWorker({
403 const errorMessage
= this.handleError(e
as Error | string)
404 this.sendToMainWorker({
406 name
: task
.name
?? DEFAULT_TASK_NAME
,
407 message
: errorMessage
,
414 if (!this.isMain
&& this.aliveInterval
!= null) {
415 this.lastTaskTimestamp
= performance
.now()
421 * Runs the given function asynchronously.
423 * @param fn - Task function that will be executed.
424 * @param task - Input data for the task function.
427 fn
: WorkerAsyncFunction
<Data
, Response
>,
430 let taskPerformance
= this.beginTaskPerformance(task
.name
)
433 taskPerformance
= this.endTaskPerformance(taskPerformance
)
434 this.sendToMainWorker({
443 const errorMessage
= this.handleError(e
as Error | string)
444 this.sendToMainWorker({
446 name
: task
.name
?? DEFAULT_TASK_NAME
,
447 message
: errorMessage
,
455 if (!this.isMain
&& this.aliveInterval
!= null) {
456 this.lastTaskTimestamp
= performance
.now()
459 .catch(EMPTY_FUNCTION
)
463 * Gets the task function with the given name.
465 * @param name - Name of the task function that will be returned.
466 * @returns The task function.
467 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found.
469 private getTaskFunction (name
?: string): WorkerFunction
<Data
, Response
> {
470 name
= name
?? DEFAULT_TASK_NAME
471 const fn
= this.taskFunctions
.get(name
)
473 throw new Error(`Task function '${name}' not found`)
478 private beginTaskPerformance (name
?: string): TaskPerformance
{
479 this.checkStatistics()
481 name
: name
?? DEFAULT_TASK_NAME
,
482 timestamp
: performance
.now(),
483 ...(this.statistics
.elu
&& { elu
: performance
.eventLoopUtilization() })
487 private endTaskPerformance (
488 taskPerformance
: TaskPerformance
490 this.checkStatistics()
493 ...(this.statistics
.runTime
&& {
494 runTime
: performance
.now() - taskPerformance
.timestamp
496 ...(this.statistics
.elu
&& {
497 elu
: performance
.eventLoopUtilization(taskPerformance
.elu
)
502 private checkStatistics (): void {
503 if (this.statistics
== null) {
504 throw new Error('Performance statistics computation requirements not set')