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'
10 import { EMPTY_FUNCTION
, isAsyncFunction
, isPlainObject
} from
'../utils'
15 } from
'./worker-options'
21 } from
'./worker-functions'
23 const DEFAULT_FUNCTION_NAME
= 'default'
24 const DEFAULT_MAX_INACTIVE_TIME
= 60000
25 const DEFAULT_KILL_BEHAVIOR
: KillBehavior
= KillBehaviors
.SOFT
28 * Base class that implements some shared logic for all poolifier workers.
30 * @typeParam MainWorker - Type of main worker.
31 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
32 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
34 export abstract class AbstractWorker
<
35 MainWorker
extends Worker
| MessagePort
,
38 > extends AsyncResource
{
42 protected abstract id
: number
44 * Task function(s) processed by the worker when the pool's `execution` function is invoked.
46 protected taskFunctions
!: Map
<string, WorkerFunction
<Data
, Response
>>
48 * Timestamp of the last task processed by this worker.
50 protected lastTaskTimestamp
!: number
52 * Performance statistics computation requirements.
54 protected statistics
!: WorkerStatistics
56 * Handler id of the `aliveInterval` worker alive check.
58 protected aliveInterval
?: NodeJS
.Timeout
60 * Constructs a new poolifier worker.
62 * @param type - The type of async event.
63 * @param isMain - Whether this is the main worker or not.
64 * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function.
65 * @param mainWorker - Reference to main worker.
66 * @param opts - Options for the worker.
70 protected readonly isMain
: boolean,
72 | WorkerFunction
<Data
, Response
>
73 | TaskFunctions
<Data
, Response
>,
74 protected readonly mainWorker
: MainWorker
,
75 protected readonly opts
: WorkerOptions
= {
77 * The kill behavior option on this worker or its default value.
79 killBehavior
: DEFAULT_KILL_BEHAVIOR
,
81 * The maximum time to keep this worker alive while idle.
82 * The pool automatically checks and terminates this worker when the time expires.
84 maxInactiveTime
: DEFAULT_MAX_INACTIVE_TIME
88 this.checkWorkerOptions(this.opts
)
89 this.checkTaskFunctions(taskFunctions
)
91 this.mainWorker
?.on('message', this.messageListener
.bind(this))
95 private checkWorkerOptions (opts
: WorkerOptions
): void {
96 this.opts
.killBehavior
= opts
.killBehavior
?? DEFAULT_KILL_BEHAVIOR
97 this.opts
.maxInactiveTime
=
98 opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
99 delete this.opts
.async
103 * Checks if the `taskFunctions` parameter is passed to the constructor.
105 * @param taskFunctions - The task function(s) parameter that should be checked.
107 private checkTaskFunctions (
109 | WorkerFunction
<Data
, Response
>
110 | TaskFunctions
<Data
, Response
>
112 if (taskFunctions
== null) {
113 throw new Error('taskFunctions parameter is mandatory')
115 this.taskFunctions
= new Map
<string, WorkerFunction
<Data
, Response
>>()
116 if (typeof taskFunctions
=== 'function') {
117 this.taskFunctions
.set(DEFAULT_FUNCTION_NAME
, taskFunctions
.bind(this))
118 } else if (isPlainObject(taskFunctions
)) {
119 let firstEntry
= true
120 for (const [name
, fn
] of Object.entries(taskFunctions
)) {
121 if (typeof fn
!== 'function') {
123 'A taskFunctions parameter object value is not a function'
126 this.taskFunctions
.set(name
, fn
.bind(this))
128 this.taskFunctions
.set(DEFAULT_FUNCTION_NAME
, fn
.bind(this))
133 throw new Error('taskFunctions parameter object is empty')
137 'taskFunctions parameter is not a function or a plain object'
143 * Worker message listener.
145 * @param message - Message received.
147 protected messageListener (message
: MessageValue
<Data
, Data
>): void {
148 if (message
.ready
!= null && message
.workerId
=== this.id
) {
149 // Startup message received
151 } else if (message
.statistics
!= null) {
152 // Statistics message received
153 this.statistics
= message
.statistics
154 } else if (message
.checkAlive
!= null) {
155 // Check alive message received
156 message
.checkAlive
? this.startCheckAlive() : this.stopCheckAlive()
157 } else if (message
.id
!= null && message
.data
!= null) {
158 // Task message received
159 const fn
= this.getTaskFunction(message
.name
)
160 if (isAsyncFunction(fn
)) {
161 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, message
)
163 this.runInAsyncScope(this.runSync
.bind(this), this, fn
, message
)
165 } else if (message
.kill
=== true) {
166 // Kill message received
167 this.stopCheckAlive()
173 * Notifies the main worker that this worker is ready to process tasks.
175 protected workerReady (): void {
176 !this.isMain
&& this.sendToMainWorker({ ready
: true, workerId
: this.id
})
180 * Starts the worker alive check interval.
182 private startCheckAlive (): void {
183 this.lastTaskTimestamp
= performance
.now()
184 this.aliveInterval
= setInterval(
185 this.checkAlive
.bind(this),
186 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
) / 2
188 this.checkAlive
.bind(this)()
192 * Stops the worker alive check interval.
194 private stopCheckAlive (): void {
195 this.aliveInterval
!= null && clearInterval(this.aliveInterval
)
199 * Checks if the worker should be terminated, because its living too long.
201 private checkAlive (): void {
203 performance
.now() - this.lastTaskTimestamp
>
204 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
)
206 this.sendToMainWorker({ kill
: this.opts
.killBehavior
})
211 * Returns the main worker.
213 * @returns Reference to the main worker.
215 protected getMainWorker (): MainWorker
{
216 if (this.mainWorker
== null) {
217 throw new Error('Main worker not set')
219 return this.mainWorker
223 * Sends a message to the main worker.
225 * @param message - The response message.
227 protected abstract sendToMainWorker (
228 message
: MessageValue
<Response
, Data
>
232 * Handles an error and convert it to a string so it can be sent back to the main worker.
234 * @param e - The error raised by the worker.
235 * @returns The error message.
237 protected handleError (e
: Error | string): string {
238 return e
instanceof Error ? e
.message
: e
242 * Runs the given function synchronously.
244 * @param fn - Function that will be executed.
245 * @param message - Input data for the given function.
248 fn
: WorkerSyncFunction
<Data
, Response
>,
249 message
: MessageValue
<Data
>
252 let taskPerformance
= this.beginTaskPerformance()
253 const res
= fn(message
.data
)
254 taskPerformance
= this.endTaskPerformance(taskPerformance
)
255 this.sendToMainWorker({
262 const errorMessage
= this.handleError(e
as Error | string)
263 this.sendToMainWorker({
266 message
: errorMessage
,
272 if (!this.isMain
&& this.aliveInterval
!= null) {
273 this.lastTaskTimestamp
= performance
.now()
279 * Runs the given function asynchronously.
281 * @param fn - Function that will be executed.
282 * @param message - Input data for the given function.
285 fn
: WorkerAsyncFunction
<Data
, Response
>,
286 message
: MessageValue
<Data
>
288 let taskPerformance
= this.beginTaskPerformance()
291 taskPerformance
= this.endTaskPerformance(taskPerformance
)
292 this.sendToMainWorker({
301 const errorMessage
= this.handleError(e
as Error | string)
302 this.sendToMainWorker({
305 message
: errorMessage
,
312 if (!this.isMain
&& this.aliveInterval
!= null) {
313 this.lastTaskTimestamp
= performance
.now()
316 .catch(EMPTY_FUNCTION
)
320 * Gets the task function in the given scope.
322 * @param name - Name of the function that will be returned.
324 private getTaskFunction (name
?: string): WorkerFunction
<Data
, Response
> {
325 name
= name
?? DEFAULT_FUNCTION_NAME
326 const fn
= this.taskFunctions
.get(name
)
328 throw new Error(`Task function '${name}' not found`)
333 private beginTaskPerformance (): TaskPerformance
{
334 this.checkStatistics()
336 timestamp
: performance
.now(),
337 ...(this.statistics
.elu
&& { elu
: performance
.eventLoopUtilization() })
341 private endTaskPerformance (
342 taskPerformance
: TaskPerformance
344 this.checkStatistics()
347 ...(this.statistics
.runTime
&& {
348 runTime
: performance
.now() - taskPerformance
.timestamp
350 ...(this.statistics
.elu
&& {
351 elu
: performance
.eventLoopUtilization(taskPerformance
.elu
)
356 private checkStatistics (): void {
357 if (this.statistics
== null) {
358 throw new Error('Performance statistics computation requirements not set')