1 import { AsyncResource
} from
'async_hooks'
2 import type { MessageValue
} from
'../utility-types'
3 import type { WorkerOptions
} from
'./worker-options'
6 * Base class containing some shared logic for all poolifier workers.
8 * @template MainWorker Type of main worker.
9 * @template Data Type of data this worker receives from pool's execution.
10 * @template Response Type of response the worker sends back to the main worker.
12 export abstract class AbstractWorker
<
16 > extends AsyncResource
{
18 * The maximum time to keep this worker alive while idle. The pool automatically checks and terminates this worker when the time expires.
20 protected readonly maxInactiveTime
: number
22 * Whether the worker is working asynchronously or not.
24 protected readonly async: boolean
26 * Timestamp of the last task processed by this worker.
28 protected lastTask
: number
30 * Handler ID of the `interval` alive check.
32 protected readonly interval
?: NodeJS
.Timeout
35 * Constructs a new poolifier worker.
37 * @param type The type of async event.
38 * @param isMain Whether this is the main worker or not.
39 * @param fn Function processed by the worker when the pool's `execution` function is invoked.
40 * @param opts Options for the worker.
45 fn
: (data
: Data
) => Response
,
46 public readonly opts
: WorkerOptions
= {}
50 this.maxInactiveTime
= this.opts
.maxInactiveTime
?? 1000 * 60
51 this.async = !!this.opts
.async
52 this.lastTask
= Date.now()
53 if (!fn
) throw new Error('fn parameter is mandatory')
54 // keep the worker active
56 this.interval
= setInterval(
57 this.checkAlive
.bind(this),
58 this.maxInactiveTime
/ 2
60 this.checkAlive
.bind(this)()
65 * Returns the main worker.
67 protected abstract getMainWorker (): MainWorker
70 * Send a message to the main worker.
72 * @param message The response message.
74 protected abstract sendToMainWorker (message
: MessageValue
<Response
>): void
77 * Check to see if the worker should be terminated, because its living too long.
79 protected checkAlive (): void {
80 if (Date.now() - this.lastTask
> this.maxInactiveTime
) {
81 this.sendToMainWorker({ kill
: 1 })
86 * Handle an error and convert it to a string so it can be sent back to the main worker.
88 * @param e The error raised by the worker.
89 * @returns Message of the error.
91 protected handleError (e
: Error | string): string {
92 return (e
as unknown
) as string
96 * Run the given function synchronously.
98 * @param fn Function that will be executed.
99 * @param value Input data for the given function.
102 fn
: (data
?: Data
) => Response
,
103 value
: MessageValue
<Data
>
106 const res
= fn(value
.data
)
107 this.sendToMainWorker({ data
: res
, id
: value
.id
})
108 this.lastTask
= Date.now()
110 const err
= this.handleError(e
)
111 this.sendToMainWorker({ error
: err
, id
: value
.id
})
112 this.lastTask
= Date.now()
117 * Run the given function asynchronously.
119 * @param fn Function that will be executed.
120 * @param value Input data for the given function.
123 fn
: (data
?: Data
) => Promise
<Response
>,
124 value
: MessageValue
<Data
>
128 this.sendToMainWorker({ data
: res
, id
: value
.id
})
129 this.lastTask
= Date.now()
133 const err
= this.handleError(e
)
134 this.sendToMainWorker({ error
: err
, id
: value
.id
})
135 this.lastTask
= Date.now()