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