refactor: cleanup worker import
[poolifier.git] / src / worker / abstract-worker.ts
CommitLineData
fc3e6586
JB
1import { AsyncResource } from 'node:async_hooks'
2import type { Worker } from 'node:cluster'
3import type { MessagePort } from 'node:worker_threads'
7b427d73
JB
4import type {
5 MessageValue,
6 TaskFunctions,
7 WorkerAsyncFunction,
8 WorkerFunction,
9 WorkerSyncFunction
48ef9107 10} from '../utility-types'
0d80593b 11import { EMPTY_FUNCTION, isPlainObject } from '../utils'
241f23c2
JB
12import {
13 type KillBehavior,
14 KillBehaviors,
15 type WorkerOptions
16} from './worker-options'
4c35177b 17
ec8fd331 18const DEFAULT_FUNCTION_NAME = 'default'
978aad6f 19const DEFAULT_MAX_INACTIVE_TIME = 60000
1a81f8af 20const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT
c97c7edb 21
729c563d 22/**
ea7a90d3 23 * Base class that implements some shared logic for all poolifier workers.
729c563d 24 *
38e795c1
JB
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.
729c563d 28 */
c97c7edb 29export abstract class AbstractWorker<
838898f1 30 MainWorker extends Worker | MessagePort,
d3c8a1a8
S
31 Data = unknown,
32 Response = unknown
c97c7edb 33> extends AsyncResource {
a86b6df1
JB
34 /**
35 * Task function(s) processed by the worker when the pool's `execution` function is invoked.
36 */
37 protected taskFunctions!: Map<string, WorkerFunction<Data, Response>>
729c563d
S
38 /**
39 * Timestamp of the last task processed by this worker.
40 */
a9d9ea34 41 protected lastTaskTimestamp!: number
729c563d 42 /**
aee46736 43 * Handler id of the `aliveInterval` worker alive check.
729c563d 44 */
e088a00c 45 protected readonly aliveInterval?: NodeJS.Timeout
c97c7edb 46 /**
729c563d 47 * Constructs a new poolifier worker.
c97c7edb 48 *
38e795c1
JB
49 * @param type - The type of async event.
50 * @param isMain - Whether this is the main worker or not.
82888165 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.
38e795c1
JB
52 * @param mainWorker - Reference to main worker.
53 * @param opts - Options for the worker.
c97c7edb
S
54 */
55 public constructor (
56 type: string,
c2ade475 57 protected readonly isMain: boolean,
a86b6df1
JB
58 taskFunctions:
59 | WorkerFunction<Data, Response>
60 | TaskFunctions<Data, Response>,
7e0d447f 61 protected mainWorker: MainWorker | undefined | null,
d99ba5a8 62 protected readonly opts: WorkerOptions = {
e088a00c 63 /**
aee46736 64 * The kill behavior option on this worker or its default value.
e088a00c 65 */
1a81f8af 66 killBehavior: DEFAULT_KILL_BEHAVIOR,
e088a00c
JB
67 /**
68 * The maximum time to keep this worker alive while idle.
69 * The pool automatically checks and terminates this worker when the time expires.
70 */
1a81f8af 71 maxInactiveTime: DEFAULT_MAX_INACTIVE_TIME
4c35177b 72 }
c97c7edb
S
73 ) {
74 super(type)
e088a00c 75 this.checkWorkerOptions(this.opts)
a86b6df1 76 this.checkTaskFunctions(taskFunctions)
1f68cede 77 if (!this.isMain) {
3fafb1b2 78 this.lastTaskTimestamp = performance.now()
e088a00c 79 this.aliveInterval = setInterval(
c97c7edb 80 this.checkAlive.bind(this),
e088a00c 81 (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2
c97c7edb
S
82 )
83 this.checkAlive.bind(this)()
84 }
838898f1 85
82888165 86 this.mainWorker?.on('message', this.messageListener.bind(this))
c97c7edb
S
87 }
88
41aa7dcd
JB
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
571227f4 93 delete this.opts.async
41aa7dcd
JB
94 }
95
96 /**
a86b6df1 97 * Checks if the `taskFunctions` parameter is passed to the constructor.
41aa7dcd 98 *
82888165 99 * @param taskFunctions - The task function(s) parameter that should be checked.
41aa7dcd 100 */
a86b6df1
JB
101 private checkTaskFunctions (
102 taskFunctions:
103 | WorkerFunction<Data, Response>
104 | TaskFunctions<Data, Response>
105 ): void {
ec8fd331
JB
106 if (taskFunctions == null) {
107 throw new Error('taskFunctions parameter is mandatory')
108 }
a86b6df1 109 this.taskFunctions = new Map<string, WorkerFunction<Data, Response>>()
0d80593b
JB
110 if (typeof taskFunctions === 'function') {
111 this.taskFunctions.set(DEFAULT_FUNCTION_NAME, taskFunctions.bind(this))
112 } else if (isPlainObject(taskFunctions)) {
82888165 113 let firstEntry = true
a86b6df1
JB
114 for (const [name, fn] of Object.entries(taskFunctions)) {
115 if (typeof fn !== 'function') {
0d80593b 116 throw new TypeError(
a86b6df1
JB
117 'A taskFunctions parameter object value is not a function'
118 )
119 }
120 this.taskFunctions.set(name, fn.bind(this))
82888165
JB
121 if (firstEntry) {
122 this.taskFunctions.set(DEFAULT_FUNCTION_NAME, fn.bind(this))
123 firstEntry = false
124 }
a86b6df1 125 }
630f0acf
JB
126 if (firstEntry) {
127 throw new Error('taskFunctions parameter object is empty')
128 }
a86b6df1 129 } else {
f34fdabe
JB
130 throw new TypeError(
131 'taskFunctions parameter is not a function or a plain object'
132 )
41aa7dcd
JB
133 }
134 }
135
aee46736
JB
136 /**
137 * Worker message listener.
138 *
139 * @param message - Message received.
aee46736 140 */
a86b6df1 141 protected messageListener (message: MessageValue<Data, MainWorker>): void {
a3f5f781 142 if (message.id != null && message.data != null) {
aee46736 143 // Task message received
c7c04698 144 const fn = this.getTaskFunction(message.name)
a86b6df1 145 if (fn?.constructor.name === 'AsyncFunction') {
aee46736 146 this.runInAsyncScope(this.runAsync.bind(this), this, fn, message)
cf597bc5 147 } else {
70a4f5ea 148 this.runInAsyncScope(this.runSync.bind(this), this, fn, message)
cf597bc5 149 }
aee46736
JB
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
73cff87e 155 this.aliveInterval != null && clearInterval(this.aliveInterval)
cf597bc5
JB
156 this.emitDestroy()
157 }
158 }
159
729c563d
S
160 /**
161 * Returns the main worker.
838898f1
S
162 *
163 * @returns Reference to the main worker.
729c563d 164 */
838898f1 165 protected getMainWorker (): MainWorker {
78cea37e 166 if (this.mainWorker == null) {
838898f1
S
167 throw new Error('Main worker was not set')
168 }
169 return this.mainWorker
170 }
c97c7edb 171
729c563d 172 /**
8accb8d5 173 * Sends a message to the main worker.
729c563d 174 *
38e795c1 175 * @param message - The response message.
729c563d 176 */
c97c7edb
S
177 protected abstract sendToMainWorker (message: MessageValue<Response>): void
178
729c563d 179 /**
a05c10de 180 * Checks if the worker should be terminated, because its living too long.
729c563d 181 */
c97c7edb 182 protected checkAlive (): void {
e088a00c 183 if (
3fafb1b2 184 performance.now() - this.lastTaskTimestamp >
e088a00c
JB
185 (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME)
186 ) {
187 this.sendToMainWorker({ kill: this.opts.killBehavior })
c97c7edb
S
188 }
189 }
190
729c563d 191 /**
8accb8d5 192 * Handles an error and convert it to a string so it can be sent back to the main worker.
729c563d 193 *
38e795c1 194 * @param e - The error raised by the worker.
50eceb07 195 * @returns Message of the error.
729c563d 196 */
c97c7edb 197 protected handleError (e: Error | string): string {
4a34343d 198 return e as string
c97c7edb
S
199 }
200
729c563d 201 /**
8accb8d5 202 * Runs the given function synchronously.
729c563d 203 *
38e795c1 204 * @param fn - Function that will be executed.
aee46736 205 * @param message - Input data for the given function.
729c563d 206 */
70a4f5ea 207 protected runSync (
48ef9107 208 fn: WorkerSyncFunction<Data, Response>,
aee46736 209 message: MessageValue<Data>
c97c7edb
S
210 ): void {
211 try {
3fafb1b2 212 const startTimestamp = performance.now()
09a6305f 213 const waitTime = startTimestamp - (message.submissionTimestamp ?? 0)
aee46736 214 const res = fn(message.data)
3fafb1b2
JB
215 const runTime = performance.now() - startTimestamp
216 this.sendToMainWorker({
217 data: res,
09a6305f 218 runTime,
91ee39ed
JB
219 waitTime,
220 id: message.id
3fafb1b2 221 })
c97c7edb 222 } catch (e) {
0a23f635 223 const err = this.handleError(e as Error)
91ee39ed
JB
224 this.sendToMainWorker({
225 error: err,
226 errorData: message.data,
227 id: message.id
228 })
6e9d10db 229 } finally {
3fafb1b2 230 !this.isMain && (this.lastTaskTimestamp = performance.now())
c97c7edb
S
231 }
232 }
233
729c563d 234 /**
8accb8d5 235 * Runs the given function asynchronously.
729c563d 236 *
38e795c1 237 * @param fn - Function that will be executed.
aee46736 238 * @param message - Input data for the given function.
729c563d 239 */
c97c7edb 240 protected runAsync (
48ef9107 241 fn: WorkerAsyncFunction<Data, Response>,
aee46736 242 message: MessageValue<Data>
c97c7edb 243 ): void {
3fafb1b2 244 const startTimestamp = performance.now()
09a6305f 245 const waitTime = startTimestamp - (message.submissionTimestamp ?? 0)
aee46736 246 fn(message.data)
c97c7edb 247 .then(res => {
3fafb1b2
JB
248 const runTime = performance.now() - startTimestamp
249 this.sendToMainWorker({
250 data: res,
09a6305f 251 runTime,
91ee39ed
JB
252 waitTime,
253 id: message.id
3fafb1b2 254 })
c97c7edb
S
255 return null
256 })
257 .catch(e => {
f3636726 258 const err = this.handleError(e as Error)
91ee39ed
JB
259 this.sendToMainWorker({
260 error: err,
261 errorData: message.data,
262 id: message.id
263 })
6e9d10db
JB
264 })
265 .finally(() => {
3fafb1b2 266 !this.isMain && (this.lastTaskTimestamp = performance.now())
c97c7edb 267 })
6e9d10db 268 .catch(EMPTY_FUNCTION)
c97c7edb 269 }
ec8fd331 270
82888165
JB
271 /**
272 * Gets the task function in the given scope.
273 *
274 * @param name - Name of the function that will be returned.
275 */
ec8fd331
JB
276 private getTaskFunction (name?: string): WorkerFunction<Data, Response> {
277 name = name ?? DEFAULT_FUNCTION_NAME
278 const fn = this.taskFunctions.get(name)
279 if (fn == null) {
ace229a1 280 throw new Error(`Task function '${name}' not found`)
ec8fd331
JB
281 }
282 return fn
283 }
c97c7edb 284}