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'
9 } from
'../utility-types'
20 } from
'./worker-options'
26 } from
'./worker-functions'
28 const DEFAULT_MAX_INACTIVE_TIME
= 60000
29 const DEFAULT_KILL_BEHAVIOR
: KillBehavior
= KillBehaviors
.SOFT
32 * Base class that implements some shared logic for all poolifier workers.
34 * @typeParam MainWorker - Type of main worker.
35 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
36 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
38 export abstract class AbstractWorker
<
39 MainWorker
extends Worker
| MessagePort
,
42 > extends AsyncResource
{
46 protected abstract id
: number
48 * Task function(s) processed by the worker when the pool's `execution` function is invoked.
50 protected taskFunctions
!: Map
<string, WorkerFunction
<Data
, Response
>>
52 * Timestamp of the last task processed by this worker.
54 protected lastTaskTimestamp
!: number
56 * Performance statistics computation requirements.
58 protected statistics
!: WorkerStatistics
60 * Handler id of the `aliveInterval` worker alive check.
62 protected aliveInterval
?: NodeJS
.Timeout
64 * Constructs a new poolifier worker.
66 * @param type - The type of async event.
67 * @param isMain - Whether this is the main worker or not.
68 * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function.
69 * @param mainWorker - Reference to main worker.
70 * @param opts - Options for the worker.
74 protected readonly isMain
: boolean,
76 | WorkerFunction
<Data
, Response
>
77 | TaskFunctions
<Data
, Response
>,
78 protected readonly mainWorker
: MainWorker
,
79 protected readonly opts
: WorkerOptions
= {
81 * The kill behavior option on this worker or its default value.
83 killBehavior
: DEFAULT_KILL_BEHAVIOR
,
85 * The maximum time to keep this worker alive while idle.
86 * The pool automatically checks and terminates this worker when the time expires.
88 maxInactiveTime
: DEFAULT_MAX_INACTIVE_TIME
92 this.checkWorkerOptions(this.opts
)
93 this.checkTaskFunctions(taskFunctions
)
95 this.mainWorker
?.on('message', this.messageListener
.bind(this))
99 private checkWorkerOptions (opts
: WorkerOptions
): void {
100 this.opts
.killBehavior
= opts
.killBehavior
?? DEFAULT_KILL_BEHAVIOR
101 this.opts
.maxInactiveTime
=
102 opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
103 delete this.opts
.async
107 * Checks if the `taskFunctions` parameter is passed to the constructor.
109 * @param taskFunctions - The task function(s) parameter that should be checked.
111 private checkTaskFunctions (
113 | WorkerFunction
<Data
, Response
>
114 | TaskFunctions
<Data
, Response
>
116 if (taskFunctions
== null) {
117 throw new Error('taskFunctions parameter is mandatory')
119 this.taskFunctions
= new Map
<string, WorkerFunction
<Data
, Response
>>()
120 if (typeof taskFunctions
=== 'function') {
121 this.taskFunctions
.set(DEFAULT_TASK_NAME
, taskFunctions
.bind(this))
122 } else if (isPlainObject(taskFunctions
)) {
123 let firstEntry
= true
124 for (const [name
, fn
] of Object.entries(taskFunctions
)) {
125 if (typeof fn
!== 'function') {
127 'A taskFunctions parameter object value is not a function'
130 this.taskFunctions
.set(name
, fn
.bind(this))
132 this.taskFunctions
.set(DEFAULT_TASK_NAME
, fn
.bind(this))
137 throw new Error('taskFunctions parameter object is empty')
141 'taskFunctions parameter is not a function or a plain object'
147 * Checks if the worker has a task function with the given name.
149 * @param name - The name of the task function to check.
150 * @returns Whether the worker has a task function with the given name or not.
151 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string.
153 public hasTaskFunction (name
: string): boolean {
154 if (typeof name
!== 'string') {
155 throw new TypeError('name parameter is not a string')
157 return this.taskFunctions
.has(name
)
161 * Adds a task function to the worker.
162 * If a task function with the same name already exists, it is replaced.
164 * @param name - The name of the task function to add.
165 * @param fn - The task function to add.
166 * @returns Whether the task function was added or not.
167 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string.
168 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
169 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `fn` parameter is not a function.
171 public addTaskFunction (
173 fn
: WorkerFunction
<Data
, Response
>
175 if (typeof name
!== 'string') {
176 throw new TypeError('name parameter is not a string')
178 if (name
=== DEFAULT_TASK_NAME
) {
180 'Cannot add a task function with the default reserved name'
183 if (typeof fn
!== 'function') {
184 throw new TypeError('fn parameter is not a function')
188 this.taskFunctions
.get(name
) ===
189 this.taskFunctions
.get(DEFAULT_TASK_NAME
)
191 this.taskFunctions
.set(DEFAULT_TASK_NAME
, fn
.bind(this))
193 this.taskFunctions
.set(name
, fn
.bind(this))
201 * Removes a task function from the worker.
203 * @param name - The name of the task function to remove.
204 * @returns Whether the task function existed and was removed or not.
205 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string.
206 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
207 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the task function used as default task function.
209 public removeTaskFunction (name
: string): boolean {
210 if (typeof name
!== 'string') {
211 throw new TypeError('name parameter is not a string')
213 if (name
=== DEFAULT_TASK_NAME
) {
215 'Cannot remove the task function with the default reserved name'
219 this.taskFunctions
.get(name
) === this.taskFunctions
.get(DEFAULT_TASK_NAME
)
222 'Cannot remove the task function used as the default task function'
225 return this.taskFunctions
.delete(name
)
229 * Sets the default task function to use when no task function name is specified.
231 * @param name - The name of the task function to use as default task function.
232 * @returns Whether the default task function was set or not.
233 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string.
234 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
235 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is a non-existing task function.
237 public setDefaultTaskFunction (name
: string): boolean {
238 if (typeof name
!== 'string') {
239 throw new TypeError('name parameter is not a string')
241 if (name
=== DEFAULT_TASK_NAME
) {
243 'Cannot set the default task function reserved name as the default task function'
246 if (!this.taskFunctions
.has(name
)) {
248 'Cannot set the default task function to a non-existing task function'
252 this.taskFunctions
.set(
254 this.taskFunctions
.get(name
)?.bind(this) as WorkerFunction
<
266 * Worker message listener.
268 * @param message - Message received.
270 protected messageListener (message
: MessageValue
<Data
, Data
>): void {
271 if (message
.workerId
=== this.id
) {
272 if (message
.ready
!= null) {
273 // Startup message received
275 } else if (message
.statistics
!= null) {
276 // Statistics message received
277 this.statistics
= message
.statistics
278 } else if (message
.checkAlive
!= null) {
279 // Check alive message received
280 message
.checkAlive
? this.startCheckAlive() : this.stopCheckAlive()
281 } else if (message
.id
!= null && message
.data
!= null) {
282 // Task message received
283 const fn
= this.getTaskFunction(message
.name
)
284 if (isAsyncFunction(fn
)) {
285 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, message
)
287 this.runInAsyncScope(this.runSync
.bind(this), this, fn
, message
)
289 } else if (message
.kill
=== true) {
290 // Kill message received
291 this.stopCheckAlive()
298 * Notifies the main worker that this worker is ready to process tasks.
300 protected workerReady (): void {
301 !this.isMain
&& this.sendToMainWorker({ ready
: true, workerId
: this.id
})
305 * Starts the worker alive check interval.
307 private startCheckAlive (): void {
308 this.lastTaskTimestamp
= performance
.now()
309 this.aliveInterval
= setInterval(
310 this.checkAlive
.bind(this),
311 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
) / 2
313 this.checkAlive
.bind(this)()
317 * Stops the worker alive check interval.
319 private stopCheckAlive (): void {
320 this.aliveInterval
!= null && clearInterval(this.aliveInterval
)
324 * Checks if the worker should be terminated, because its living too long.
326 private checkAlive (): void {
328 performance
.now() - this.lastTaskTimestamp
>
329 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
)
331 this.sendToMainWorker({ kill
: this.opts
.killBehavior
, workerId
: this.id
})
336 * Returns the main worker.
338 * @returns Reference to the main worker.
340 protected getMainWorker (): MainWorker
{
341 if (this.mainWorker
== null) {
342 throw new Error('Main worker not set')
344 return this.mainWorker
348 * Sends a message to the main worker.
350 * @param message - The response message.
352 protected abstract sendToMainWorker (
353 message
: MessageValue
<Response
, Data
>
357 * Handles an error and convert it to a string so it can be sent back to the main worker.
359 * @param e - The error raised by the worker.
360 * @returns The error message.
362 protected handleError (e
: Error | string): string {
363 return e
instanceof Error ? e
.message
: e
367 * Runs the given function synchronously.
369 * @param fn - Function that will be executed.
370 * @param message - Input data for the given function.
373 fn
: WorkerSyncFunction
<Data
, Response
>,
374 message
: MessageValue
<Data
>
377 let taskPerformance
= this.beginTaskPerformance(message
.name
)
378 const res
= fn(message
.data
)
379 taskPerformance
= this.endTaskPerformance(taskPerformance
)
380 this.sendToMainWorker({
387 const errorMessage
= this.handleError(e
as Error | string)
388 this.sendToMainWorker({
390 name
: message
.name
?? DEFAULT_TASK_NAME
,
391 message
: errorMessage
,
398 if (!this.isMain
&& this.aliveInterval
!= null) {
399 this.lastTaskTimestamp
= performance
.now()
405 * Runs the given function asynchronously.
407 * @param fn - Function that will be executed.
408 * @param message - Input data for the given function.
411 fn
: WorkerAsyncFunction
<Data
, Response
>,
412 message
: MessageValue
<Data
>
414 let taskPerformance
= this.beginTaskPerformance(message
.name
)
417 taskPerformance
= this.endTaskPerformance(taskPerformance
)
418 this.sendToMainWorker({
427 const errorMessage
= this.handleError(e
as Error | string)
428 this.sendToMainWorker({
430 name
: message
.name
?? DEFAULT_TASK_NAME
,
431 message
: errorMessage
,
439 if (!this.isMain
&& this.aliveInterval
!= null) {
440 this.lastTaskTimestamp
= performance
.now()
443 .catch(EMPTY_FUNCTION
)
447 * Gets the task function in the given scope.
449 * @param name - Name of the task function that will be returned.
451 private getTaskFunction (name
?: string): WorkerFunction
<Data
, Response
> {
452 name
= name
?? DEFAULT_TASK_NAME
453 const fn
= this.taskFunctions
.get(name
)
455 throw new Error(`Task function '${name}' not found`)
460 private beginTaskPerformance (name
?: string): TaskPerformance
{
461 this.checkStatistics()
463 name
: name
?? DEFAULT_TASK_NAME
,
464 timestamp
: performance
.now(),
465 ...(this.statistics
.elu
&& { elu
: performance
.eventLoopUtilization() })
469 private endTaskPerformance (
470 taskPerformance
: TaskPerformance
472 this.checkStatistics()
475 ...(this.statistics
.runTime
&& {
476 runTime
: performance
.now() - taskPerformance
.timestamp
478 ...(this.statistics
.elu
&& {
479 elu
: performance
.eventLoopUtilization(taskPerformance
.elu
)
484 private checkStatistics (): void {
485 if (this.statistics
== null) {
486 throw new Error('Performance statistics computation requirements not set')