Fix EPIPE on shutdown
[poolifier.git] / src / worker / abstract-worker.ts
CommitLineData
c97c7edb 1import { AsyncResource } from 'async_hooks'
838898f1
S
2import type { Worker } from 'cluster'
3import type { MessagePort } from 'worker_threads'
1a81f8af
S
4import type { MessageValue } from '../utility-types'
5import type { KillBehavior, WorkerOptions } from './worker-options'
6import { KillBehaviors } from './worker-options'
4c35177b 7
1a81f8af
S
8const DEFAULT_MAX_INACTIVE_TIME = 1000 * 60
9const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT
c97c7edb 10
729c563d
S
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 */
c97c7edb 18export abstract class AbstractWorker<
838898f1 19 MainWorker extends Worker | MessagePort,
d3c8a1a8
S
20 Data = unknown,
21 Response = unknown
c97c7edb 22> extends AsyncResource {
729c563d
S
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 */
c97c7edb 26 protected readonly maxInactiveTime: number
4c35177b 27 /**
28 * The kill behavior set as option on the Worker constructor or a default value.
29 */
fb41d7f7 30 protected readonly killBehavior: KillBehavior
729c563d
S
31 /**
32 * Whether the worker is working asynchronously or not.
33 */
c97c7edb 34 protected readonly async: boolean
729c563d
S
35 /**
36 * Timestamp of the last task processed by this worker.
37 */
c97c7edb 38 protected lastTask: number
729c563d
S
39 /**
40 * Handler ID of the `interval` alive check.
41 */
c97c7edb
S
42 protected readonly interval?: NodeJS.Timeout
43
4dc27476 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
c97c7edb 49 /**
729c563d 50 * Constructs a new poolifier worker.
c97c7edb
S
51 *
52 * @param type The type of async event.
729c563d
S
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.
838898f1 55 * @param mainWorker Reference to main worker.
729c563d 56 * @param opts Options for the worker.
c97c7edb
S
57 */
58 public constructor (
59 type: string,
60 isMain: boolean,
61 fn: (data: Data) => Response,
838898f1 62 protected mainWorker?: MainWorker | null,
4c35177b 63 public readonly opts: WorkerOptions = {
1a81f8af
S
64 killBehavior: DEFAULT_KILL_BEHAVIOR,
65 maxInactiveTime: DEFAULT_MAX_INACTIVE_TIME
4c35177b 66 }
c97c7edb
S
67 ) {
68 super(type)
1a81f8af
S
69 this.killBehavior = this.opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR
70 this.maxInactiveTime =
71 this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME
c97c7edb
S
72 this.async = !!this.opts.async
73 this.lastTask = Date.now()
c510fea7 74 this.checkFunctionInput(fn)
838898f1 75 // Keep the worker active
c97c7edb
S
76 if (!isMain) {
77 this.interval = setInterval(
78 this.checkAlive.bind(this),
79 this.maxInactiveTime / 2
80 )
81 this.checkAlive.bind(this)()
82 }
838898f1
S
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
4dc27476 98 this.isKilled = true
838898f1
S
99 if (this.interval) clearInterval(this.interval)
100 this.emitDestroy()
101 }
102 })
c97c7edb
S
103 }
104
c510fea7
APA
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
729c563d
S
114 /**
115 * Returns the main worker.
838898f1
S
116 *
117 * @returns Reference to the main worker.
729c563d 118 */
838898f1
S
119 protected getMainWorker (): MainWorker {
120 if (!this.mainWorker) {
121 throw new Error('Main worker was not set')
122 }
123 return this.mainWorker
124 }
c97c7edb 125
729c563d
S
126 /**
127 * Send a message to the main worker.
128 *
129 * @param message The response message.
130 */
c97c7edb
S
131 protected abstract sendToMainWorker (message: MessageValue<Response>): void
132
729c563d
S
133 /**
134 * Check to see if the worker should be terminated, because its living too long.
135 */
c97c7edb 136 protected checkAlive (): void {
4dc27476 137 if (Date.now() - this.lastTask > this.maxInactiveTime && !this.isKilled) {
4c35177b 138 this.sendToMainWorker({ kill: this.killBehavior })
c97c7edb
S
139 }
140 }
141
729c563d
S
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.
50eceb07 146 * @returns Message of the error.
729c563d 147 */
c97c7edb
S
148 protected handleError (e: Error | string): string {
149 return (e as unknown) as string
150 }
151
729c563d
S
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 */
c97c7edb
S
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
729c563d
S
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 */
c97c7edb
S
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}