1 import { AsyncResource
} from
'async_hooks'
2 import type { Worker
} from
'cluster'
3 import type { MessagePort
} from
'worker_threads'
4 import type { MessageValue
} from
'../utility-types'
5 import { EMPTY_FUNCTION
} from
'../utils'
10 } from
'./worker-options'
11 import { KillBehaviors
} from
'./worker-options'
13 const DEFAULT_MAX_INACTIVE_TIME
= 60000
14 const DEFAULT_KILL_BEHAVIOR
: KillBehavior
= KillBehaviors
.SOFT
17 * Base class that implements some shared logic for all poolifier workers.
19 * @typeParam MainWorker - Type of main worker.
20 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be serializable data.
21 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be serializable data.
23 export abstract class AbstractWorker
<
24 MainWorker
extends Worker
| MessagePort
,
27 > extends AsyncResource
{
29 * Timestamp of the last task processed by this worker.
31 protected lastTaskTimestamp
!: number
33 * Handler Id of the `aliveInterval` worker alive check.
35 protected readonly aliveInterval
?: NodeJS
.Timeout
37 * Options for the worker.
39 public readonly opts
: WorkerOptions
41 * Constructs a new poolifier worker.
43 * @param type - The type of async event.
44 * @param isMain - Whether this is the main worker or not.
45 * @param fn - Function processed by the worker when the pool's `execution` function is invoked.
46 * @param mainWorker - Reference to main worker.
47 * @param opts - Options for the worker.
52 fn
: (data
: Data
) => Response
,
53 protected mainWorker
: MainWorker
| undefined | null,
54 opts
: WorkerOptions
= {
56 * The kill behavior option on this Worker or its default value.
58 killBehavior
: DEFAULT_KILL_BEHAVIOR
,
60 * The maximum time to keep this worker alive while idle.
61 * The pool automatically checks and terminates this worker when the time expires.
63 maxInactiveTime
: DEFAULT_MAX_INACTIVE_TIME
68 this.checkFunctionInput(fn
)
69 this.checkWorkerOptions(this.opts
)
70 if (!isMain
&& isKillBehavior(KillBehaviors
.HARD
, this.opts
.killBehavior
)) {
71 this.lastTaskTimestamp
= Date.now()
72 this.aliveInterval
= setInterval(
73 this.checkAlive
.bind(this),
74 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
) / 2
76 this.checkAlive
.bind(this)()
79 this.mainWorker
?.on('message', (value
: MessageValue
<Data
, MainWorker
>) => {
80 this.messageListener(value
, fn
)
84 protected messageListener (
85 value
: MessageValue
<Data
, MainWorker
>,
86 fn
: (data
: Data
) => Response
88 if (value
.data
!== undefined && value
.id
!== undefined) {
89 // Here you will receive messages
90 if (this.opts
.async === true) {
91 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, value
)
93 this.runInAsyncScope(this.run
.bind(this), this, fn
, value
)
95 } else if (value
.parent !== undefined) {
96 // Save a reference of the main worker to communicate with it
97 // This will be received once
98 this.mainWorker
= value
.parent
99 } else if (value
.kill
!== undefined) {
100 // Here is time to kill this worker, just clearing the interval
101 if (this.aliveInterval
!= null) clearInterval(this.aliveInterval
)
106 private checkWorkerOptions (opts
: WorkerOptions
): void {
107 this.opts
.killBehavior
= opts
.killBehavior
?? DEFAULT_KILL_BEHAVIOR
108 this.opts
.maxInactiveTime
=
109 opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
110 this.opts
.async = opts
.async ?? false
114 * Checks if the `fn` parameter is passed to the constructor.
116 * @param fn - The function that should be defined.
118 private checkFunctionInput (fn
: (data
: Data
) => Response
): void {
119 if (fn
== null) throw new Error('fn parameter is mandatory')
120 if (typeof fn
!== 'function') {
121 throw new TypeError('fn parameter is not a function')
126 * Returns the main worker.
128 * @returns Reference to the main worker.
130 protected getMainWorker (): MainWorker
{
131 if (this.mainWorker
== null) {
132 throw new Error('Main worker was not set')
134 return this.mainWorker
138 * Sends a message to the main worker.
140 * @param message - The response message.
142 protected abstract sendToMainWorker (message
: MessageValue
<Response
>): void
145 * Checks if the worker should be terminated, because its living too long.
147 protected checkAlive (): void {
149 Date.now() - this.lastTaskTimestamp
>
150 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
)
152 this.sendToMainWorker({ kill
: this.opts
.killBehavior
})
157 * Handles an error and convert it to a string so it can be sent back to the main worker.
159 * @param e - The error raised by the worker.
160 * @returns Message of the error.
162 protected handleError (e
: Error | string): string {
167 * Runs the given function synchronously.
169 * @param fn - Function that will be executed.
170 * @param value - Input data for the given function.
173 fn
: (data
?: Data
) => Response
,
174 value
: MessageValue
<Data
>
177 const startTaskTimestamp
= Date.now()
178 const res
= fn(value
.data
)
179 const taskRunTime
= Date.now() - startTaskTimestamp
180 this.sendToMainWorker({ data
: res
, id
: value
.id
, taskRunTime
})
182 const err
= this.handleError(e
as Error)
183 this.sendToMainWorker({ error
: err
, id
: value
.id
})
185 isKillBehavior(KillBehaviors
.HARD
, this.opts
.killBehavior
) &&
186 (this.lastTaskTimestamp
= Date.now())
191 * Runs the given function asynchronously.
193 * @param fn - Function that will be executed.
194 * @param value - Input data for the given function.
197 fn
: (data
?: Data
) => Promise
<Response
>,
198 value
: MessageValue
<Data
>
200 const startTaskTimestamp
= Date.now()
203 const taskRunTime
= Date.now() - startTaskTimestamp
204 this.sendToMainWorker({ data
: res
, id
: value
.id
, taskRunTime
})
208 const err
= this.handleError(e
as Error)
209 this.sendToMainWorker({ error
: err
, id
: value
.id
})
212 isKillBehavior(KillBehaviors
.HARD
, this.opts
.killBehavior
) &&
213 (this.lastTaskTimestamp
= Date.now())
215 .catch(EMPTY_FUNCTION
)