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'
8 * Base class containing some shared logic for all poolifier workers.
10 * @template MainWorker Type of main worker.
11 * @template Data Type of data this worker receives from pool's execution.
12 * @template Response Type of response the worker sends back to the main worker.
14 export abstract class AbstractWorker
<
15 MainWorker
extends Worker
| MessagePort
,
18 > extends AsyncResource
{
20 * The maximum time to keep this worker alive while idle. The pool automatically checks and terminates this worker when the time expires.
22 protected readonly maxInactiveTime
: number
24 * Whether the worker is working asynchronously or not.
26 protected readonly async: boolean
28 * Timestamp of the last task processed by this worker.
30 protected lastTask
: number
32 * Handler ID of the `interval` alive check.
34 protected readonly interval
?: NodeJS
.Timeout
37 * Constructs a new poolifier worker.
39 * @param type The type of async event.
40 * @param isMain Whether this is the main worker or not.
41 * @param fn Function processed by the worker when the pool's `execution` function is invoked.
42 * @param mainWorker Reference to main worker.
43 * @param opts Options for the worker.
48 fn
: (data
: Data
) => Response
,
49 protected mainWorker
?: MainWorker
| null,
50 public readonly opts
: WorkerOptions
= {}
54 this.maxInactiveTime
= this.opts
.maxInactiveTime
?? 1000 * 60
55 this.async = !!this.opts
.async
56 this.lastTask
= Date.now()
57 if (!fn
) throw new Error('fn parameter is mandatory')
58 // Keep the worker active
60 this.interval
= setInterval(
61 this.checkAlive
.bind(this),
62 this.maxInactiveTime
/ 2
64 this.checkAlive
.bind(this)()
67 this.mainWorker
?.on('message', (value
: MessageValue
<Data
, MainWorker
>) => {
68 if (value
?.data
&& value
.id
) {
69 // Here you will receive messages
71 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, value
)
73 this.runInAsyncScope(this.run
.bind(this), this, fn
, value
)
75 } else if (value
.parent) {
76 // Save a reference of the main worker to communicate with it
77 // This will be received once
78 this.mainWorker
= value
.parent
79 } else if (value
.kill
) {
80 // Here is time to kill this worker, just clearing the interval
81 if (this.interval
) clearInterval(this.interval
)
88 * Returns the main worker.
90 * @returns Reference to the main worker.
92 protected getMainWorker (): MainWorker
{
93 if (!this.mainWorker
) {
94 throw new Error('Main worker was not set')
96 return this.mainWorker
100 * Send a message to the main worker.
102 * @param message The response message.
104 protected abstract sendToMainWorker (message
: MessageValue
<Response
>): void
107 * Check to see if the worker should be terminated, because its living too long.
109 protected checkAlive (): void {
110 if (Date.now() - this.lastTask
> this.maxInactiveTime
) {
111 this.sendToMainWorker({ kill
: 1 })
116 * Handle an error and convert it to a string so it can be sent back to the main worker.
118 * @param e The error raised by the worker.
119 * @returns Message of the error.
121 protected handleError (e
: Error | string): string {
122 return (e
as unknown
) as string
126 * Run the given function synchronously.
128 * @param fn Function that will be executed.
129 * @param value Input data for the given function.
132 fn
: (data
?: Data
) => Response
,
133 value
: MessageValue
<Data
>
136 const res
= fn(value
.data
)
137 this.sendToMainWorker({ data
: res
, id
: value
.id
})
138 this.lastTask
= Date.now()
140 const err
= this.handleError(e
)
141 this.sendToMainWorker({ error
: err
, id
: value
.id
})
142 this.lastTask
= Date.now()
147 * Run the given function asynchronously.
149 * @param fn Function that will be executed.
150 * @param value Input data for the given function.
153 fn
: (data
?: Data
) => Promise
<Response
>,
154 value
: MessageValue
<Data
>
158 this.sendToMainWorker({ data
: res
, id
: value
.id
})
159 this.lastTask
= Date.now()
163 const err
= this.handleError(e
)
164 this.sendToMainWorker({ error
: err
, id
: value
.id
})
165 this.lastTask
= Date.now()