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