Commit | Line | Data |
---|---|---|
c97c7edb | 1 | import { AsyncResource } from 'async_hooks' |
838898f1 S |
2 | import type { Worker } from 'cluster' |
3 | import type { MessagePort } from 'worker_threads' | |
1a81f8af | 4 | import type { MessageValue } from '../utility-types' |
6e9d10db | 5 | import { EMPTY_FUNCTION } from '../utils' |
1a81f8af S |
6 | import type { KillBehavior, WorkerOptions } from './worker-options' |
7 | import { KillBehaviors } from './worker-options' | |
4c35177b | 8 | |
1a81f8af S |
9 | const DEFAULT_MAX_INACTIVE_TIME = 1000 * 60 |
10 | const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT | |
c97c7edb | 11 | |
729c563d S |
12 | /** |
13 | * Base class containing some shared logic for all poolifier workers. | |
14 | * | |
15 | * @template MainWorker Type of main worker. | |
deb85c12 JB |
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. | |
729c563d | 18 | */ |
c97c7edb | 19 | export abstract class AbstractWorker< |
838898f1 | 20 | MainWorker extends Worker | MessagePort, |
d3c8a1a8 S |
21 | Data = unknown, |
22 | Response = unknown | |
c97c7edb | 23 | > extends AsyncResource { |
729c563d S |
24 | /** |
25 | * Timestamp of the last task processed by this worker. | |
26 | */ | |
e088a00c | 27 | protected lastTaskTimestamp: number |
729c563d | 28 | /** |
e088a00c | 29 | * Handler Id of the `aliveInterval` worker alive check. |
729c563d | 30 | */ |
e088a00c | 31 | protected readonly aliveInterval?: NodeJS.Timeout |
dc5d0cb3 JB |
32 | /** |
33 | * Options for the worker. | |
34 | */ | |
35 | public readonly opts: WorkerOptions | |
c97c7edb S |
36 | |
37 | /** | |
729c563d | 38 | * Constructs a new poolifier worker. |
c97c7edb S |
39 | * |
40 | * @param type The type of async event. | |
729c563d S |
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. | |
838898f1 | 43 | * @param mainWorker Reference to main worker. |
729c563d | 44 | * @param opts Options for the worker. |
c97c7edb S |
45 | */ |
46 | public constructor ( | |
47 | type: string, | |
48 | isMain: boolean, | |
49 | fn: (data: Data) => Response, | |
7e0d447f | 50 | protected mainWorker: MainWorker | undefined | null, |
59a11fd2 | 51 | opts: WorkerOptions = { |
e088a00c JB |
52 | /** |
53 | * The kill behavior option on this Worker or its default value. | |
54 | */ | |
1a81f8af | 55 | killBehavior: DEFAULT_KILL_BEHAVIOR, |
e088a00c JB |
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 | */ | |
1a81f8af | 60 | maxInactiveTime: DEFAULT_MAX_INACTIVE_TIME |
4c35177b | 61 | } |
c97c7edb S |
62 | ) { |
63 | super(type) | |
59a11fd2 | 64 | this.opts = opts |
c510fea7 | 65 | this.checkFunctionInput(fn) |
e088a00c JB |
66 | this.checkWorkerOptions(this.opts) |
67 | this.lastTaskTimestamp = Date.now() | |
838898f1 | 68 | // Keep the worker active |
c97c7edb | 69 | if (!isMain) { |
e088a00c | 70 | this.aliveInterval = setInterval( |
c97c7edb | 71 | this.checkAlive.bind(this), |
e088a00c | 72 | (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2 |
c97c7edb S |
73 | ) |
74 | this.checkAlive.bind(this)() | |
75 | } | |
838898f1 S |
76 | |
77 | this.mainWorker?.on('message', (value: MessageValue<Data, MainWorker>) => { | |
78 | if (value?.data && value.id) { | |
79 | // Here you will receive messages | |
e088a00c | 80 | if (this.opts.async) { |
838898f1 S |
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 | |
e088a00c | 91 | if (this.aliveInterval) clearInterval(this.aliveInterval) |
838898f1 S |
92 | this.emitDestroy() |
93 | } | |
94 | }) | |
c97c7edb S |
95 | } |
96 | ||
e088a00c JB |
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 | ||
c510fea7 APA |
107 | /** |
108 | * Check if the `fn` parameter is passed to the constructor. | |
109 | * | |
110 | * @param fn The function that should be defined. | |
111 | */ | |
a35560ba | 112 | private checkFunctionInput (fn: (data: Data) => Response): void { |
c510fea7 APA |
113 | if (!fn) throw new Error('fn parameter is mandatory') |
114 | } | |
115 | ||
729c563d S |
116 | /** |
117 | * Returns the main worker. | |
838898f1 S |
118 | * |
119 | * @returns Reference to the main worker. | |
729c563d | 120 | */ |
838898f1 S |
121 | protected getMainWorker (): MainWorker { |
122 | if (!this.mainWorker) { | |
123 | throw new Error('Main worker was not set') | |
124 | } | |
125 | return this.mainWorker | |
126 | } | |
c97c7edb | 127 | |
729c563d S |
128 | /** |
129 | * Send a message to the main worker. | |
130 | * | |
131 | * @param message The response message. | |
132 | */ | |
c97c7edb S |
133 | protected abstract sendToMainWorker (message: MessageValue<Response>): void |
134 | ||
729c563d S |
135 | /** |
136 | * Check to see if the worker should be terminated, because its living too long. | |
137 | */ | |
c97c7edb | 138 | protected checkAlive (): void { |
e088a00c JB |
139 | if ( |
140 | Date.now() - this.lastTaskTimestamp > | |
141 | (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) | |
142 | ) { | |
143 | this.sendToMainWorker({ kill: this.opts.killBehavior }) | |
c97c7edb S |
144 | } |
145 | } | |
146 | ||
729c563d S |
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. | |
50eceb07 | 151 | * @returns Message of the error. |
729c563d | 152 | */ |
c97c7edb | 153 | protected handleError (e: Error | string): string { |
4a34343d | 154 | return e as string |
c97c7edb S |
155 | } |
156 | ||
729c563d S |
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 | */ | |
c97c7edb S |
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 }) | |
c97c7edb | 170 | } catch (e) { |
0a23f635 | 171 | const err = this.handleError(e as Error) |
c97c7edb | 172 | this.sendToMainWorker({ error: err, id: value.id }) |
6e9d10db | 173 | } finally { |
e088a00c | 174 | this.lastTaskTimestamp = Date.now() |
c97c7edb S |
175 | } |
176 | } | |
177 | ||
729c563d S |
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 | */ | |
c97c7edb S |
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 }) | |
c97c7edb S |
191 | return null |
192 | }) | |
193 | .catch(e => { | |
f3636726 | 194 | const err = this.handleError(e as Error) |
c97c7edb | 195 | this.sendToMainWorker({ error: err, id: value.id }) |
6e9d10db JB |
196 | }) |
197 | .finally(() => { | |
e088a00c | 198 | this.lastTaskTimestamp = Date.now() |
c97c7edb | 199 | }) |
6e9d10db | 200 | .catch(EMPTY_FUNCTION) |
c97c7edb S |
201 | } |
202 | } |