1 import { AsyncResource
} from
'node:async_hooks'
2 import type { Worker
} from
'node:cluster'
3 import type { MessagePort
} from
'node:worker_threads'
10 } from
'../utility-types'
11 import { EMPTY_FUNCTION
, isPlainObject
} from
'../utils'
16 } from
'./worker-options'
18 const DEFAULT_FUNCTION_NAME
= 'default'
19 const DEFAULT_MAX_INACTIVE_TIME
= 60000
20 const DEFAULT_KILL_BEHAVIOR
: KillBehavior
= KillBehaviors
.SOFT
23 * Base class that implements some shared logic for all poolifier workers.
25 * @typeParam MainWorker - Type of main worker.
26 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be serializable data.
27 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be serializable data.
29 export abstract class AbstractWorker
<
30 MainWorker
extends Worker
| MessagePort
,
33 > extends AsyncResource
{
35 * Task function(s) processed by the worker when the pool's `execution` function is invoked.
37 protected taskFunctions
!: Map
<string, WorkerFunction
<Data
, Response
>>
39 * Timestamp of the last task processed by this worker.
41 protected lastTaskTimestamp
!: number
43 * Handler id of the `aliveInterval` worker alive check.
45 protected readonly aliveInterval
?: NodeJS
.Timeout
47 * Constructs a new poolifier worker.
49 * @param type - The type of async event.
50 * @param isMain - Whether this is the main worker or not.
51 * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function.
52 * @param mainWorker - Reference to main worker.
53 * @param opts - Options for the worker.
57 protected readonly isMain
: boolean,
59 | WorkerFunction
<Data
, Response
>
60 | TaskFunctions
<Data
, Response
>,
61 protected mainWorker
: MainWorker
| undefined | null,
62 protected readonly opts
: WorkerOptions
= {
64 * The kill behavior option on this worker or its default value.
66 killBehavior
: DEFAULT_KILL_BEHAVIOR
,
68 * The maximum time to keep this worker alive while idle.
69 * The pool automatically checks and terminates this worker when the time expires.
71 maxInactiveTime
: DEFAULT_MAX_INACTIVE_TIME
75 this.checkWorkerOptions(this.opts
)
76 this.checkTaskFunctions(taskFunctions
)
78 this.lastTaskTimestamp
= performance
.now()
79 this.aliveInterval
= setInterval(
80 this.checkAlive
.bind(this),
81 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
) / 2
83 this.checkAlive
.bind(this)()
86 this.mainWorker
?.on('message', this.messageListener
.bind(this))
89 private checkWorkerOptions (opts
: WorkerOptions
): void {
90 this.opts
.killBehavior
= opts
.killBehavior
?? DEFAULT_KILL_BEHAVIOR
91 this.opts
.maxInactiveTime
=
92 opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
93 delete this.opts
.async
97 * Checks if the `taskFunctions` parameter is passed to the constructor.
99 * @param taskFunctions - The task function(s) parameter that should be checked.
101 private checkTaskFunctions (
103 | WorkerFunction
<Data
, Response
>
104 | TaskFunctions
<Data
, Response
>
106 if (taskFunctions
== null) {
107 throw new Error('taskFunctions parameter is mandatory')
109 this.taskFunctions
= new Map
<string, WorkerFunction
<Data
, Response
>>()
110 if (typeof taskFunctions
=== 'function') {
111 this.taskFunctions
.set(DEFAULT_FUNCTION_NAME
, taskFunctions
.bind(this))
112 } else if (isPlainObject(taskFunctions
)) {
113 let firstEntry
= true
114 for (const [name
, fn
] of Object.entries(taskFunctions
)) {
115 if (typeof fn
!== 'function') {
117 'A taskFunctions parameter object value is not a function'
120 this.taskFunctions
.set(name
, fn
.bind(this))
122 this.taskFunctions
.set(DEFAULT_FUNCTION_NAME
, fn
.bind(this))
127 throw new Error('taskFunctions parameter object is empty')
131 'taskFunctions parameter is not a function or a plain object'
137 * Worker message listener.
139 * @param message - Message received.
141 protected messageListener (message
: MessageValue
<Data
, MainWorker
>): void {
142 if (message
.id
!= null && message
.data
!= null) {
143 // Task message received
144 const fn
= this.getTaskFunction(message
.name
)
145 if (fn
?.constructor
.name
=== 'AsyncFunction') {
146 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, message
)
148 this.runInAsyncScope(this.runSync
.bind(this), this, fn
, message
)
150 } else if (message
.parent != null) {
151 // Main worker reference message received
152 this.mainWorker
= message
.parent
153 } else if (message
.kill
!= null) {
154 // Kill message received
155 this.aliveInterval
!= null && clearInterval(this.aliveInterval
)
161 * Returns the main worker.
163 * @returns Reference to the main worker.
165 protected getMainWorker (): MainWorker
{
166 if (this.mainWorker
== null) {
167 throw new Error('Main worker was not set')
169 return this.mainWorker
173 * Sends a message to the main worker.
175 * @param message - The response message.
177 protected abstract sendToMainWorker (message
: MessageValue
<Response
>): void
180 * Checks if the worker should be terminated, because its living too long.
182 protected checkAlive (): void {
184 performance
.now() - this.lastTaskTimestamp
>
185 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
)
187 this.sendToMainWorker({ kill
: this.opts
.killBehavior
})
192 * Handles an error and convert it to a string so it can be sent back to the main worker.
194 * @param e - The error raised by the worker.
195 * @returns Message of the error.
197 protected handleError (e
: Error | string): string {
202 * Runs the given function synchronously.
204 * @param fn - Function that will be executed.
205 * @param message - Input data for the given function.
208 fn
: WorkerSyncFunction
<Data
, Response
>,
209 message
: MessageValue
<Data
>
212 const startTimestamp
= performance
.now()
213 const waitTime
= startTimestamp
- (message
.submissionTimestamp
?? 0)
214 const res
= fn(message
.data
)
215 const runTime
= performance
.now() - startTimestamp
216 this.sendToMainWorker({
223 const err
= this.handleError(e
as Error)
224 this.sendToMainWorker({
226 errorData
: message
.data
,
230 !this.isMain
&& (this.lastTaskTimestamp
= performance
.now())
235 * Runs the given function asynchronously.
237 * @param fn - Function that will be executed.
238 * @param message - Input data for the given function.
241 fn
: WorkerAsyncFunction
<Data
, Response
>,
242 message
: MessageValue
<Data
>
244 const startTimestamp
= performance
.now()
245 const waitTime
= startTimestamp
- (message
.submissionTimestamp
?? 0)
248 const runTime
= performance
.now() - startTimestamp
249 this.sendToMainWorker({
258 const err
= this.handleError(e
as Error)
259 this.sendToMainWorker({
261 errorData
: message
.data
,
266 !this.isMain
&& (this.lastTaskTimestamp
= performance
.now())
268 .catch(EMPTY_FUNCTION
)
272 * Gets the task function in the given scope.
274 * @param name - Name of the function that will be returned.
276 private getTaskFunction (name
?: string): WorkerFunction
<Data
, Response
> {
277 name
= name
?? DEFAULT_FUNCTION_NAME
278 const fn
= this.taskFunctions
.get(name
)
280 throw new Error(`Task function '${name}' not found`)