c50cfbb081599ed955561edea56a634c93d885ef
[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 { WorkerOptions } from './worker-options'
6 import { killBehaviorEnumeration } from './worker-options'
7
8 const defaultMaxInactiveTime = 1000 * 60
9 const defaultKillBehavior = killBehaviorEnumeration.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: string
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 * Constructs a new poolifier worker.
46 *
47 * @param type The type of async event.
48 * @param isMain Whether this is the main worker or not.
49 * @param fn Function processed by the worker when the pool's `execution` function is invoked.
50 * @param mainWorker Reference to main worker.
51 * @param opts Options for the worker.
52 */
53 public constructor (
54 type: string,
55 isMain: boolean,
56 fn: (data: Data) => Response,
57 protected mainWorker?: MainWorker | null,
58 public readonly opts: WorkerOptions = {
59 killBehavior: defaultKillBehavior,
60 maxInactiveTime: defaultMaxInactiveTime
61 }
62 ) {
63 super(type)
64 this.killBehavior = this.opts.killBehavior ?? defaultKillBehavior
65 this.maxInactiveTime = this.opts.maxInactiveTime ?? defaultMaxInactiveTime
66 this.async = !!this.opts.async
67 this.lastTask = Date.now()
68 if (!fn) throw new Error('fn parameter is mandatory')
69 // Keep the worker active
70 if (!isMain) {
71 this.interval = setInterval(
72 this.checkAlive.bind(this),
73 this.maxInactiveTime / 2
74 )
75 this.checkAlive.bind(this)()
76 }
77
78 this.mainWorker?.on('message', (value: MessageValue<Data, MainWorker>) => {
79 if (value?.data && value.id) {
80 // Here you will receive messages
81 if (this.async) {
82 this.runInAsyncScope(this.runAsync.bind(this), this, fn, value)
83 } else {
84 this.runInAsyncScope(this.run.bind(this), this, fn, value)
85 }
86 } else if (value.parent) {
87 // Save a reference of the main worker to communicate with it
88 // This will be received once
89 this.mainWorker = value.parent
90 } else if (value.kill) {
91 // Here is time to kill this worker, just clearing the interval
92 if (this.interval) clearInterval(this.interval)
93 this.emitDestroy()
94 }
95 })
96 }
97
98 /**
99 * Returns the main worker.
100 *
101 * @returns Reference to the main worker.
102 */
103 protected getMainWorker (): MainWorker {
104 if (!this.mainWorker) {
105 throw new Error('Main worker was not set')
106 }
107 return this.mainWorker
108 }
109
110 /**
111 * Send a message to the main worker.
112 *
113 * @param message The response message.
114 */
115 protected abstract sendToMainWorker (message: MessageValue<Response>): void
116
117 /**
118 * Check to see if the worker should be terminated, because its living too long.
119 */
120 protected checkAlive (): void {
121 if (Date.now() - this.lastTask > this.maxInactiveTime) {
122 this.sendToMainWorker({ kill: this.killBehavior })
123 }
124 }
125
126 /**
127 * Handle an error and convert it to a string so it can be sent back to the main worker.
128 *
129 * @param e The error raised by the worker.
130 * @returns Message of the error.
131 */
132 protected handleError (e: Error | string): string {
133 return (e as unknown) as string
134 }
135
136 /**
137 * Run the given function synchronously.
138 *
139 * @param fn Function that will be executed.
140 * @param value Input data for the given function.
141 */
142 protected run (
143 fn: (data?: Data) => Response,
144 value: MessageValue<Data>
145 ): void {
146 try {
147 const res = fn(value.data)
148 this.sendToMainWorker({ data: res, id: value.id })
149 this.lastTask = Date.now()
150 } catch (e) {
151 const err = this.handleError(e)
152 this.sendToMainWorker({ error: err, id: value.id })
153 this.lastTask = Date.now()
154 }
155 }
156
157 /**
158 * Run the given function asynchronously.
159 *
160 * @param fn Function that will be executed.
161 * @param value Input data for the given function.
162 */
163 protected runAsync (
164 fn: (data?: Data) => Promise<Response>,
165 value: MessageValue<Data>
166 ): void {
167 fn(value.data)
168 .then(res => {
169 this.sendToMainWorker({ data: res, id: value.id })
170 this.lastTask = Date.now()
171 return null
172 })
173 .catch(e => {
174 const err = this.handleError(e)
175 this.sendToMainWorker({ error: err, id: value.id })
176 this.lastTask = Date.now()
177 })
178 }
179 }