refactor: cleanup type import
[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 * Options for the worker.
34 */
35 public readonly opts: WorkerOptions
36 /**
37 * Constructs a new poolifier worker.
38 *
39 * @param type - The type of async event.
40 * @param isMain - Whether this is the main worker or not.
41 * @param fn - Function processed by the worker when the pool's `execution` function is invoked.
42 * @param mainWorker - Reference to main worker.
43 * @param opts - Options for the worker.
44 */
45 public constructor (
46 type: string,
47 protected readonly isMain: boolean,
48 fn: (data: Data) => Response,
49 protected mainWorker: MainWorker | undefined | null,
50 opts: WorkerOptions = {
51 /**
52 * The kill behavior option on this Worker or its default value.
53 */
54 killBehavior: DEFAULT_KILL_BEHAVIOR,
55 /**
56 * The maximum time to keep this worker alive while idle.
57 * The pool automatically checks and terminates this worker when the time expires.
58 */
59 maxInactiveTime: DEFAULT_MAX_INACTIVE_TIME
60 }
61 ) {
62 super(type)
63 this.opts = opts
64 this.checkFunctionInput(fn)
65 this.checkWorkerOptions(this.opts)
66 if (!this.isMain) {
67 this.lastTaskTimestamp = Date.now()
68 this.aliveInterval = setInterval(
69 this.checkAlive.bind(this),
70 (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2
71 )
72 this.checkAlive.bind(this)()
73 }
74
75 this.mainWorker?.on('message', (value: MessageValue<Data, MainWorker>) => {
76 this.messageListener(value, fn)
77 })
78 }
79
80 protected messageListener (
81 value: MessageValue<Data, MainWorker>,
82 fn: (data: Data) => Response
83 ): void {
84 if (value.data !== undefined && value.id !== undefined) {
85 // Here you will receive messages
86 if (this.opts.async === true) {
87 this.runInAsyncScope(this.runAsync.bind(this), this, fn, value)
88 } else {
89 this.runInAsyncScope(this.run.bind(this), this, fn, value)
90 }
91 } else if (value.parent !== undefined) {
92 // Save a reference of the main worker to communicate with it
93 // This will be received once
94 this.mainWorker = value.parent
95 } else if (value.kill !== undefined) {
96 // Here is time to kill this worker, just clearing the interval
97 this.aliveInterval != null && clearInterval(this.aliveInterval)
98 this.emitDestroy()
99 }
100 }
101
102 private checkWorkerOptions (opts: WorkerOptions): void {
103 this.opts.killBehavior = opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR
104 this.opts.maxInactiveTime =
105 opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME
106 this.opts.async = opts.async ?? false
107 }
108
109 /**
110 * Checks if the `fn` parameter is passed to the constructor.
111 *
112 * @param fn - The function that should be defined.
113 */
114 private checkFunctionInput (fn: (data: Data) => Response): void {
115 if (fn == null) throw new Error('fn parameter is mandatory')
116 if (typeof fn !== 'function') {
117 throw new TypeError('fn parameter is not a function')
118 }
119 }
120
121 /**
122 * Returns the main worker.
123 *
124 * @returns Reference to the main worker.
125 */
126 protected getMainWorker (): MainWorker {
127 if (this.mainWorker == null) {
128 throw new Error('Main worker was not set')
129 }
130 return this.mainWorker
131 }
132
133 /**
134 * Sends a message to the main worker.
135 *
136 * @param message - The response message.
137 */
138 protected abstract sendToMainWorker (message: MessageValue<Response>): void
139
140 /**
141 * Checks if the worker should be terminated, because its living too long.
142 */
143 protected checkAlive (): void {
144 if (
145 Date.now() - this.lastTaskTimestamp >
146 (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME)
147 ) {
148 this.sendToMainWorker({ kill: this.opts.killBehavior })
149 }
150 }
151
152 /**
153 * Handles an error and convert it to a string so it can be sent back to the main worker.
154 *
155 * @param e - The error raised by the worker.
156 * @returns Message of the error.
157 */
158 protected handleError (e: Error | string): string {
159 return e as string
160 }
161
162 /**
163 * Runs the given function synchronously.
164 *
165 * @param fn - Function that will be executed.
166 * @param value - Input data for the given function.
167 */
168 protected run (
169 fn: (data?: Data) => Response,
170 value: MessageValue<Data>
171 ): void {
172 try {
173 const startTaskTimestamp = Date.now()
174 const res = fn(value.data)
175 const taskRunTime = Date.now() - startTaskTimestamp
176 this.sendToMainWorker({ data: res, id: value.id, taskRunTime })
177 } catch (e) {
178 const err = this.handleError(e as Error)
179 this.sendToMainWorker({ error: err, id: value.id })
180 } finally {
181 !this.isMain && (this.lastTaskTimestamp = Date.now())
182 }
183 }
184
185 /**
186 * Runs the given function asynchronously.
187 *
188 * @param fn - Function that will be executed.
189 * @param value - Input data for the given function.
190 */
191 protected runAsync (
192 fn: (data?: Data) => Promise<Response>,
193 value: MessageValue<Data>
194 ): void {
195 const startTaskTimestamp = Date.now()
196 fn(value.data)
197 .then(res => {
198 const taskRunTime = Date.now() - startTaskTimestamp
199 this.sendToMainWorker({ data: res, id: value.id, taskRunTime })
200 return null
201 })
202 .catch(e => {
203 const err = this.handleError(e as Error)
204 this.sendToMainWorker({ error: err, id: value.id })
205 })
206 .finally(() => {
207 !this.isMain && (this.lastTaskTimestamp = Date.now())
208 })
209 .catch(EMPTY_FUNCTION)
210 }
211 }