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'
6 import type { KillBehavior
, WorkerOptions
} from
'./worker-options'
7 import { KillBehaviors
} from
'./worker-options'
9 const DEFAULT_MAX_INACTIVE_TIME
= 1000 * 60
10 const DEFAULT_KILL_BEHAVIOR
: KillBehavior
= KillBehaviors
.SOFT
13 * Base class containing some shared logic for all poolifier workers.
15 * @template MainWorker Type of main worker.
16 * @template Data Type of data this worker receives from pool's execution. This can only be serializable data.
17 * @template Response Type of response the worker sends back to the main worker. This can only be serializable data.
19 export abstract class AbstractWorker
<
20 MainWorker
extends Worker
| MessagePort
,
23 > extends AsyncResource
{
25 * Timestamp of the last task processed by this worker.
27 protected lastTaskTimestamp
: number
29 * Handler Id of the `aliveInterval` worker alive check.
31 protected readonly aliveInterval
?: NodeJS
.Timeout
34 * Constructs a new poolifier worker.
36 * @param type The type of async event.
37 * @param isMain Whether this is the main worker or not.
38 * @param fn Function processed by the worker when the pool's `execution` function is invoked.
39 * @param mainWorker Reference to main worker.
40 * @param opts Options for the worker.
45 fn
: (data
: Data
) => Response
,
46 protected mainWorker
: MainWorker
| undefined | null,
47 public readonly opts
: WorkerOptions
= {
49 * The kill behavior option on this Worker or its default value.
51 killBehavior
: DEFAULT_KILL_BEHAVIOR
,
53 * The maximum time to keep this worker alive while idle.
54 * The pool automatically checks and terminates this worker when the time expires.
56 maxInactiveTime
: DEFAULT_MAX_INACTIVE_TIME
60 this.checkFunctionInput(fn
)
61 this.checkWorkerOptions(this.opts
)
62 this.lastTaskTimestamp
= Date.now()
63 // Keep the worker active
65 this.aliveInterval
= setInterval(
66 this.checkAlive
.bind(this),
67 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
) / 2
69 this.checkAlive
.bind(this)()
72 this.mainWorker
?.on('message', (value
: MessageValue
<Data
, MainWorker
>) => {
73 if (value
?.data
&& value
.id
) {
74 // Here you will receive messages
75 if (this.opts
.async) {
76 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, value
)
78 this.runInAsyncScope(this.run
.bind(this), this, fn
, value
)
80 } else if (value
.parent) {
81 // Save a reference of the main worker to communicate with it
82 // This will be received once
83 this.mainWorker
= value
.parent
84 } else if (value
.kill
) {
85 // Here is time to kill this worker, just clearing the interval
86 if (this.aliveInterval
) clearInterval(this.aliveInterval
)
92 private checkWorkerOptions (opts
: WorkerOptions
) {
93 this.opts
.killBehavior
= opts
.killBehavior
?? DEFAULT_KILL_BEHAVIOR
94 this.opts
.maxInactiveTime
=
95 opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
97 * Whether the worker is working asynchronously or not.
99 this.opts
.async = !!opts
.async
103 * Check if the `fn` parameter is passed to the constructor.
105 * @param fn The function that should be defined.
107 private checkFunctionInput (fn
: (data
: Data
) => Response
): void {
108 if (!fn
) throw new Error('fn parameter is mandatory')
112 * Returns the main worker.
114 * @returns Reference to the main worker.
116 protected getMainWorker (): MainWorker
{
117 if (!this.mainWorker
) {
118 throw new Error('Main worker was not set')
120 return this.mainWorker
124 * Send a message to the main worker.
126 * @param message The response message.
128 protected abstract sendToMainWorker (message
: MessageValue
<Response
>): void
131 * Check to see if the worker should be terminated, because its living too long.
133 protected checkAlive (): void {
135 Date.now() - this.lastTaskTimestamp
>
136 (this.opts
.maxInactiveTime
?? DEFAULT_MAX_INACTIVE_TIME
)
138 this.sendToMainWorker({ kill
: this.opts
.killBehavior
})
143 * Handle an error and convert it to a string so it can be sent back to the main worker.
145 * @param e The error raised by the worker.
146 * @returns Message of the error.
148 protected handleError (e
: Error | string): string {
153 * Run the given function synchronously.
155 * @param fn Function that will be executed.
156 * @param value Input data for the given function.
159 fn
: (data
?: Data
) => Response
,
160 value
: MessageValue
<Data
>
163 const res
= fn(value
.data
)
164 this.sendToMainWorker({ data
: res
, id
: value
.id
})
166 const err
= this.handleError(e
as Error)
167 this.sendToMainWorker({ error
: err
, id
: value
.id
})
169 this.lastTaskTimestamp
= Date.now()
174 * Run the given function asynchronously.
176 * @param fn Function that will be executed.
177 * @param value Input data for the given function.
180 fn
: (data
?: Data
) => Promise
<Response
>,
181 value
: MessageValue
<Data
>
185 this.sendToMainWorker({ data
: res
, id
: value
.id
})
189 const err
= this.handleError(e
)
190 this.sendToMainWorker({ error
: err
, id
: value
.id
})
193 this.lastTaskTimestamp
= Date.now()
195 .catch(EMPTY_FUNCTION
)