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