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'
8 const DEFAULT_MAX_INACTIVE_TIME
= 1000 * 60
9 const DEFAULT_KILL_BEHAVIOR
: KillBehavior
= KillBehaviors
.SOFT
12 * Base class containing some shared logic for all poolifier workers.
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.
18 export abstract class AbstractWorker
<
19 MainWorker
extends Worker
| MessagePort
,
22 > extends AsyncResource
{
24 * The maximum time to keep this worker alive while idle. The pool automatically checks and terminates this worker when the time expires.
26 protected readonly maxInactiveTime
: number
28 * The kill behavior set as option on the Worker constructor or a default value.
30 protected readonly killBehavior
: KillBehavior
32 * Whether the worker is working asynchronously or not.
34 protected readonly async: boolean
36 * Timestamp of the last task processed by this worker.
38 protected lastTask
: number
40 * Handler ID of the `interval` alive check.
42 protected readonly interval
?: NodeJS
.Timeout
45 * This value is immediately set to true when the kill from the main worker is received.
47 private isKilled
: boolean = false
50 * Constructs a new poolifier worker.
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.
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
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
77 this.interval
= setInterval(
78 this.checkAlive
.bind(this),
79 this.maxInactiveTime
/ 2
81 this.checkAlive
.bind(this)()
84 this.mainWorker
?.on('message', (value
: MessageValue
<Data
, MainWorker
>) => {
85 if (value
?.data
&& value
.id
) {
86 // Here you will receive messages
88 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, value
)
90 this.runInAsyncScope(this.run
.bind(this), this, fn
, value
)
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
99 if (this.interval
) clearInterval(this.interval
)
106 * Check if the `fn` parameter is passed to the constructor.
108 * @param fn The function that should be defined.
110 private checkFunctionInput (fn
: (data
: Data
) => Response
) {
111 if (!fn
) throw new Error('fn parameter is mandatory')
115 * Returns the main worker.
117 * @returns Reference to the main worker.
119 protected getMainWorker (): MainWorker
{
120 if (!this.mainWorker
) {
121 throw new Error('Main worker was not set')
123 return this.mainWorker
127 * Send a message to the main worker.
129 * @param message The response message.
131 protected abstract sendToMainWorker (message
: MessageValue
<Response
>): void
134 * Check to see if the worker should be terminated, because its living too long.
136 protected checkAlive (): void {
137 if (Date.now() - this.lastTask
> this.maxInactiveTime
&& !this.isKilled
) {
138 this.sendToMainWorker({ kill
: this.killBehavior
})
143 * Handle an error and convert it to a string so it can be sent back to the main worker.
145 * @param e The error raised by the worker.
146 * @returns Message of the error.
148 protected handleError (e
: Error | string): string {
149 return (e
as unknown
) as string
153 * Run the given function synchronously.
155 * @param fn Function that will be executed.
156 * @param value Input data for the given function.
159 fn
: (data
?: Data
) => Response
,
160 value
: MessageValue
<Data
>
163 const res
= fn(value
.data
)
164 this.sendToMainWorker({ data
: res
, id
: value
.id
})
165 this.lastTask
= Date.now()
167 const err
= this.handleError(e
)
168 this.sendToMainWorker({ error
: err
, id
: value
.id
})
169 this.lastTask
= Date.now()
174 * Run the given function asynchronously.
176 * @param fn Function that will be executed.
177 * @param value Input data for the given function.
180 fn
: (data
?: Data
) => Promise
<Response
>,
181 value
: MessageValue
<Data
>
185 this.sendToMainWorker({ data
: res
, id
: value
.id
})
186 this.lastTask
= Date.now()
190 const err
= this.handleError(e
)
191 this.sendToMainWorker({ error
: err
, id
: value
.id
})
192 this.lastTask
= Date.now()