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
, 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 serializable data.
32 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be serializable data.
34 export abstract class AbstractWorker
<
35 MainWorker
extends Worker
| MessagePort
,
38 > extends AsyncResource
{
40 * Task function(s) processed by the worker when the pool's `execution` function is invoked.
42 protected taskFunctions
!: Map
<string, WorkerFunction
<Data
, Response
>>
44 * Timestamp of the last task processed by this worker.
46 protected lastTaskTimestamp
!: number
48 * Performance statistics computation.
50 protected statistics
!: WorkerStatistics
52 * Handler id of the `aliveInterval` worker alive check.
54 protected readonly aliveInterval
?: NodeJS
.Timeout
56 * Constructs a new poolifier worker.
58 * @param type - The type of async event.
59 * @param isMain - Whether this is the main worker or not.
60 * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function.
61 * @param mainWorker - Reference to main worker.
62 * @param opts - Options for the worker.
66 protected readonly isMain
: boolean,
68 | WorkerFunction
<Data
, Response
>
69 | TaskFunctions
<Data
, Response
>,
70 protected mainWorker
: MainWorker
| undefined | null,
71 protected readonly opts
: WorkerOptions
= {
73 * The kill behavior option on this worker or its default value.
75 killBehavior
: DEFAULT_KILL_BEHAVIOR
,
77 * The maximum time to keep this worker alive while idle.
78 * The pool automatically checks and terminates this worker when the time expires.
80 maxInactiveTime
: DEFAULT_MAX_INACTIVE_TIME
84 this.checkWorkerOptions(this.opts
)
85 this.checkTaskFunctions(taskFunctions
)
87 this.lastTaskTimestamp
= performance
.now()
88 this.aliveInterval
= setInterval(
89 this.checkAlive
.bind(this),
90 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
) / 2
92 this.checkAlive
.bind(this)()
94 this.mainWorker
?.on('message', this.messageListener
.bind(this))
97 private checkWorkerOptions (opts
: WorkerOptions
): void {
98 this.opts
.killBehavior
= opts
.killBehavior
?? DEFAULT_KILL_BEHAVIOR
99 this.opts
.maxInactiveTime
=
100 opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
101 delete this.opts
.async
105 * Checks if the `taskFunctions` parameter is passed to the constructor.
107 * @param taskFunctions - The task function(s) parameter that should be checked.
109 private checkTaskFunctions (
111 | WorkerFunction
<Data
, Response
>
112 | TaskFunctions
<Data
, Response
>
114 if (taskFunctions
== null) {
115 throw new Error('taskFunctions parameter is mandatory')
117 this.taskFunctions
= new Map
<string, WorkerFunction
<Data
, Response
>>()
118 if (typeof taskFunctions
=== 'function') {
119 this.taskFunctions
.set(DEFAULT_FUNCTION_NAME
, taskFunctions
.bind(this))
120 } else if (isPlainObject(taskFunctions
)) {
121 let firstEntry
= true
122 for (const [name
, fn
] of Object.entries(taskFunctions
)) {
123 if (typeof fn
!== 'function') {
125 'A taskFunctions parameter object value is not a function'
128 this.taskFunctions
.set(name
, fn
.bind(this))
130 this.taskFunctions
.set(DEFAULT_FUNCTION_NAME
, fn
.bind(this))
135 throw new Error('taskFunctions parameter object is empty')
139 'taskFunctions parameter is not a function or a plain object'
145 * Worker message listener.
147 * @param message - Message received.
149 protected messageListener (message
: MessageValue
<Data
, MainWorker
>): void {
150 if (message
.id
!= null && message
.data
!= null) {
151 // Task message received
152 const fn
= this.getTaskFunction(message
.name
)
153 if (fn
?.constructor
.name
=== 'AsyncFunction') {
154 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, message
)
156 this.runInAsyncScope(this.runSync
.bind(this), this, fn
, message
)
158 } else if (message
.parent != null) {
159 // Main worker reference message received
160 this.mainWorker
= message
.parent
161 } else if (message
.kill
!= null) {
162 // Kill message received
163 this.aliveInterval
!= null && clearInterval(this.aliveInterval
)
165 } else if (message
.statistics
!= null) {
166 // Statistics message received
167 this.statistics
= message
.statistics
172 * Returns the main worker.
174 * @returns Reference to the main worker.
176 protected getMainWorker (): MainWorker
{
177 if (this.mainWorker
== null) {
178 throw new Error('Main worker was not set')
180 return this.mainWorker
184 * Sends a message to the main worker.
186 * @param message - The response message.
188 protected abstract sendToMainWorker (message
: MessageValue
<Response
>): void
191 * Checks if the worker should be terminated, because its living too long.
193 protected checkAlive (): void {
195 performance
.now() - this.lastTaskTimestamp
>
196 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
)
198 this.sendToMainWorker({ kill
: this.opts
.killBehavior
})
203 * Handles an error and convert it to a string so it can be sent back to the main worker.
205 * @param e - The error raised by the worker.
206 * @returns Message of the error.
208 protected handleError (e
: Error | string): string {
213 * Runs the given function synchronously.
215 * @param fn - Function that will be executed.
216 * @param message - Input data for the given function.
219 fn
: WorkerSyncFunction
<Data
, Response
>,
220 message
: MessageValue
<Data
>
223 let taskPerformance
= this.beginTaskPerformance(message
)
224 const res
= fn(message
.data
)
225 taskPerformance
= this.endTaskPerformance(taskPerformance
)
226 this.sendToMainWorker({
232 const err
= this.handleError(e
as Error)
233 this.sendToMainWorker({
235 errorData
: message
.data
,
239 !this.isMain
&& (this.lastTaskTimestamp
= performance
.now())
244 * Runs the given function asynchronously.
246 * @param fn - Function that will be executed.
247 * @param message - Input data for the given function.
250 fn
: WorkerAsyncFunction
<Data
, Response
>,
251 message
: MessageValue
<Data
>
253 let taskPerformance
= this.beginTaskPerformance(message
)
256 taskPerformance
= this.endTaskPerformance(taskPerformance
)
257 this.sendToMainWorker({
265 const err
= this.handleError(e
as Error)
266 this.sendToMainWorker({
268 errorData
: message
.data
,
273 !this.isMain
&& (this.lastTaskTimestamp
= performance
.now())
275 .catch(EMPTY_FUNCTION
)
279 * Gets the task function in the given scope.
281 * @param name - Name of the function that will be returned.
283 private getTaskFunction (name
?: string): WorkerFunction
<Data
, Response
> {
284 name
= name
?? DEFAULT_FUNCTION_NAME
285 const fn
= this.taskFunctions
.get(name
)
287 throw new Error(`Task function '${name}' not found`)
292 private beginTaskPerformance (message
: MessageValue
<Data
>): TaskPerformance
{
293 const timestamp
= performance
.now()
296 ...(this.statistics
.waitTime
&& {
297 waitTime
: timestamp
- (message
.timestamp
?? timestamp
)
299 ...(this.statistics
.elu
&& { elu
: performance
.eventLoopUtilization() })
303 private endTaskPerformance (
304 taskPerformance
: TaskPerformance
308 ...(this.statistics
.runTime
&& {
309 runTime
: performance
.now() - taskPerformance
.timestamp
311 ...(this.statistics
.elu
&& {
312 elu
: performance
.eventLoopUtilization(taskPerformance
.elu
)