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 * Worker message listener.
149 * @param message - Message received.
151 protected messageListener (message
: MessageValue
<Data
, Data
>): void {
152 if (message
.workerId
=== this.id
) {
153 if (message
.ready
!= null) {
154 // Startup message received
156 } else if (message
.statistics
!= null) {
157 // Statistics message received
158 this.statistics
= message
.statistics
159 } else if (message
.checkAlive
!= null) {
160 // Check alive message received
161 message
.checkAlive
? this.startCheckAlive() : this.stopCheckAlive()
162 } else if (message
.id
!= null && message
.data
!= null) {
163 // Task message received
164 const fn
= this.getTaskFunction(message
.name
)
165 if (isAsyncFunction(fn
)) {
166 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, message
)
168 this.runInAsyncScope(this.runSync
.bind(this), this, fn
, message
)
170 } else if (message
.kill
=== true) {
171 // Kill message received
172 this.stopCheckAlive()
179 * Notifies the main worker that this worker is ready to process tasks.
181 protected workerReady (): void {
182 !this.isMain
&& this.sendToMainWorker({ ready
: true, workerId
: this.id
})
186 * Starts the worker alive check interval.
188 private startCheckAlive (): void {
189 this.lastTaskTimestamp
= performance
.now()
190 this.aliveInterval
= setInterval(
191 this.checkAlive
.bind(this),
192 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
) / 2
194 this.checkAlive
.bind(this)()
198 * Stops the worker alive check interval.
200 private stopCheckAlive (): void {
201 this.aliveInterval
!= null && clearInterval(this.aliveInterval
)
205 * Checks if the worker should be terminated, because its living too long.
207 private checkAlive (): void {
209 performance
.now() - this.lastTaskTimestamp
>
210 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
)
212 this.sendToMainWorker({ kill
: this.opts
.killBehavior
, workerId
: this.id
})
217 * Returns the main worker.
219 * @returns Reference to the main worker.
221 protected getMainWorker (): MainWorker
{
222 if (this.mainWorker
== null) {
223 throw new Error('Main worker not set')
225 return this.mainWorker
229 * Sends a message to the main worker.
231 * @param message - The response message.
233 protected abstract sendToMainWorker (
234 message
: MessageValue
<Response
, Data
>
238 * Handles an error and convert it to a string so it can be sent back to the main worker.
240 * @param e - The error raised by the worker.
241 * @returns The error message.
243 protected handleError (e
: Error | string): string {
244 return e
instanceof Error ? e
.message
: e
248 * Runs the given function synchronously.
250 * @param fn - Function that will be executed.
251 * @param message - Input data for the given function.
254 fn
: WorkerSyncFunction
<Data
, Response
>,
255 message
: MessageValue
<Data
>
258 let taskPerformance
= this.beginTaskPerformance(message
.name
)
259 const res
= fn(message
.data
)
260 taskPerformance
= this.endTaskPerformance(taskPerformance
)
261 this.sendToMainWorker({
268 const errorMessage
= this.handleError(e
as Error | string)
269 this.sendToMainWorker({
271 name
: message
.name
?? DEFAULT_TASK_NAME
,
272 message
: errorMessage
,
279 if (!this.isMain
&& this.aliveInterval
!= null) {
280 this.lastTaskTimestamp
= performance
.now()
286 * Runs the given function asynchronously.
288 * @param fn - Function that will be executed.
289 * @param message - Input data for the given function.
292 fn
: WorkerAsyncFunction
<Data
, Response
>,
293 message
: MessageValue
<Data
>
295 let taskPerformance
= this.beginTaskPerformance()
298 taskPerformance
= this.endTaskPerformance(taskPerformance
)
299 this.sendToMainWorker({
308 const errorMessage
= this.handleError(e
as Error | string)
309 this.sendToMainWorker({
311 name
: message
.name
?? DEFAULT_TASK_NAME
,
312 message
: errorMessage
,
320 if (!this.isMain
&& this.aliveInterval
!= null) {
321 this.lastTaskTimestamp
= performance
.now()
324 .catch(EMPTY_FUNCTION
)
328 * Gets the task function in the given scope.
330 * @param name - Name of the task function that will be returned.
332 private getTaskFunction (name
?: string): WorkerFunction
<Data
, Response
> {
333 name
= name
?? DEFAULT_TASK_NAME
334 const fn
= this.taskFunctions
.get(name
)
336 throw new Error(`Task function '${name}' not found`)
341 private beginTaskPerformance (name
?: string): TaskPerformance
{
342 this.checkStatistics()
344 name
: name
?? DEFAULT_TASK_NAME
,
345 timestamp
: performance
.now(),
346 ...(this.statistics
.elu
&& { elu
: performance
.eventLoopUtilization() })
350 private endTaskPerformance (
351 taskPerformance
: TaskPerformance
353 this.checkStatistics()
356 ...(this.statistics
.runTime
&& {
357 runTime
: performance
.now() - taskPerformance
.timestamp
359 ...(this.statistics
.elu
&& {
360 elu
: performance
.eventLoopUtilization(taskPerformance
.elu
)
365 private checkStatistics (): void {
366 if (this.statistics
== null) {
367 throw new Error('Performance statistics computation requirements not set')