fix: fix worker function type definition and validation
[poolifier.git] / src / worker / abstract-worker.ts
1 import { AsyncResource } from 'node:async_hooks'
2 import type { Worker } from 'node:cluster'
3 import type { MessagePort } from 'node: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'
8
9 const DEFAULT_MAX_INACTIVE_TIME = 60000
10 const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT
11
12 /**
13 * Base class that implements some shared logic for all poolifier workers.
14 *
15 * @typeParam MainWorker - Type of main worker.
16 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be serializable data.
17 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be serializable data.
18 */
19 export abstract class AbstractWorker<
20 MainWorker extends Worker | MessagePort,
21 Data = unknown,
22 Response = unknown
23 > extends AsyncResource {
24 /**
25 * Timestamp of the last task processed by this worker.
26 */
27 protected lastTaskTimestamp!: number
28 /**
29 * Handler id of the `aliveInterval` worker alive check.
30 */
31 protected readonly aliveInterval?: NodeJS.Timeout
32 /**
33 * Constructs a new poolifier worker.
34 *
35 * @param type - The type of async event.
36 * @param isMain - Whether this is the main worker or not.
37 * @param fn - Function processed by the worker when the pool's `execution` function is invoked.
38 * @param mainWorker - Reference to main worker.
39 * @param opts - Options for the worker.
40 */
41 public constructor (
42 type: string,
43 protected readonly isMain: boolean,
44 fn: (data: Data) => Response | Promise<Response>,
45 protected mainWorker: MainWorker | undefined | null,
46 protected readonly opts: WorkerOptions = {
47 /**
48 * The kill behavior option on this worker or its default value.
49 */
50 killBehavior: DEFAULT_KILL_BEHAVIOR,
51 /**
52 * The maximum time to keep this worker alive while idle.
53 * The pool automatically checks and terminates this worker when the time expires.
54 */
55 maxInactiveTime: DEFAULT_MAX_INACTIVE_TIME
56 }
57 ) {
58 super(type)
59 this.checkWorkerOptions(this.opts)
60 this.checkFunctionInput(fn)
61 if (!this.isMain) {
62 this.lastTaskTimestamp = performance.now()
63 this.aliveInterval = setInterval(
64 this.checkAlive.bind(this),
65 (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2
66 )
67 this.checkAlive.bind(this)()
68 }
69
70 this.mainWorker?.on(
71 'message',
72 (message: MessageValue<Data, MainWorker>) => {
73 this.messageListener(message, fn)
74 }
75 )
76 }
77
78 /**
79 * Worker message listener.
80 *
81 * @param message - Message received.
82 * @param fn - Function processed by the worker when the pool's `execution` function is invoked.
83 */
84 protected messageListener (
85 message: MessageValue<Data, MainWorker>,
86 fn: (data: Data) => Response | Promise<Response>
87 ): void {
88 if (message.id != null && message.data != null) {
89 // Task message received
90 if (this.opts.async === true) {
91 this.runInAsyncScope(this.runAsync.bind(this), this, fn, message)
92 } else {
93 this.runInAsyncScope(this.run.bind(this), this, fn, message)
94 }
95 } else if (message.parent != null) {
96 // Main worker reference message received
97 this.mainWorker = message.parent
98 } else if (message.kill != null) {
99 // Kill message received
100 this.aliveInterval != null && clearInterval(this.aliveInterval)
101 this.emitDestroy()
102 }
103 }
104
105 private checkWorkerOptions (opts: WorkerOptions): void {
106 this.opts.killBehavior = opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR
107 this.opts.maxInactiveTime =
108 opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME
109 this.opts.async = opts.async ?? false
110 }
111
112 /**
113 * Checks if the `fn` parameter is passed to the constructor.
114 *
115 * @param fn - The function that should be defined.
116 */
117 private checkFunctionInput (
118 fn: (data: Data) => Response | Promise<Response>
119 ): void {
120 if (fn == null) throw new Error('fn parameter is mandatory')
121 if (typeof fn !== 'function') {
122 throw new TypeError('fn parameter is not a function')
123 }
124 if (fn.constructor.name === 'AsyncFunction' && this.opts.async === false) {
125 throw new Error(
126 'fn parameter is an async function, please set the async option to true'
127 )
128 }
129 }
130
131 /**
132 * Returns the main worker.
133 *
134 * @returns Reference to the main worker.
135 */
136 protected getMainWorker (): MainWorker {
137 if (this.mainWorker == null) {
138 throw new Error('Main worker was not set')
139 }
140 return this.mainWorker
141 }
142
143 /**
144 * Sends a message to the main worker.
145 *
146 * @param message - The response message.
147 */
148 protected abstract sendToMainWorker (message: MessageValue<Response>): void
149
150 /**
151 * Checks if the worker should be terminated, because its living too long.
152 */
153 protected checkAlive (): void {
154 if (
155 performance.now() - this.lastTaskTimestamp >
156 (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME)
157 ) {
158 this.sendToMainWorker({ kill: this.opts.killBehavior })
159 }
160 }
161
162 /**
163 * Handles an error and convert it to a string so it can be sent back to the main worker.
164 *
165 * @param e - The error raised by the worker.
166 * @returns Message of the error.
167 */
168 protected handleError (e: Error | string): string {
169 return e as string
170 }
171
172 /**
173 * Runs the given function synchronously.
174 *
175 * @param fn - Function that will be executed.
176 * @param message - Input data for the given function.
177 */
178 protected run (
179 fn: (data?: Data) => Response,
180 message: MessageValue<Data>
181 ): void {
182 try {
183 const startTimestamp = performance.now()
184 const res = fn(message.data)
185 const runTime = performance.now() - startTimestamp
186 this.sendToMainWorker({
187 data: res,
188 id: message.id,
189 runTime
190 })
191 } catch (e) {
192 const err = this.handleError(e as Error)
193 this.sendToMainWorker({ error: err, id: message.id })
194 } finally {
195 !this.isMain && (this.lastTaskTimestamp = performance.now())
196 }
197 }
198
199 /**
200 * Runs the given function asynchronously.
201 *
202 * @param fn - Function that will be executed.
203 * @param message - Input data for the given function.
204 */
205 protected runAsync (
206 fn: (data?: Data) => Promise<Response>,
207 message: MessageValue<Data>
208 ): void {
209 const startTimestamp = performance.now()
210 fn(message.data)
211 .then(res => {
212 const runTime = performance.now() - startTimestamp
213 this.sendToMainWorker({
214 data: res,
215 id: message.id,
216 runTime
217 })
218 return null
219 })
220 .catch(e => {
221 const err = this.handleError(e as Error)
222 this.sendToMainWorker({ error: err, id: message.id })
223 })
224 .finally(() => {
225 !this.isMain && (this.lastTaskTimestamp = performance.now())
226 })
227 .catch(EMPTY_FUNCTION)
228 }
229 }