protected readonly opts: PoolOptions<Worker>
) {
if (!this.isMain()) {
- throw new Error('Cannot start a pool from a worker!')
+ throw new Error(
+ 'Cannot start a pool from a worker with the same type as the pool'
+ )
}
this.checkNumberOfWorkers(this.numberOfWorkers)
this.checkFilePath(this.filePath)
)
}
+ protected async sendKillMessageToWorker (
+ workerNodeKey: number,
+ workerId: number
+ ): Promise<void> {
+ const waitForKillResponse = new Promise<void>((resolve, reject) => {
+ this.registerWorkerMessageListener(workerNodeKey, (message) => {
+ if (message.kill === 'success') {
+ resolve()
+ } else if (message.kill === 'failure') {
+ reject(new Error(`Worker ${workerId} kill message handling failed`))
+ }
+ })
+ })
+ this.sendToWorker(workerNodeKey, { kill: true, workerId })
+ await waitForKillResponse
+ }
+
/**
* Terminates the worker node given its worker node key.
*
// Kill message received from worker
if (
isKillBehavior(KillBehaviors.HARD, message.kill) ||
- (message.kill != null &&
+ (isKillBehavior(KillBehaviors.SOFT, message.kill) &&
((this.opts.enableTasksQueue === false &&
workerUsage.tasks.executing === 0) ||
(this.opts.enableTasksQueue === true &&
workerUsage.tasks.executing === 0 &&
this.tasksQueueSize(localWorkerNodeKey) === 0)))
) {
- this.destroyWorkerNode(localWorkerNodeKey).catch(EMPTY_FUNCTION)
+ this.destroyWorkerNode(localWorkerNodeKey).catch((error) => {
+ this.emitter?.emit(PoolEvents.error, error)
+ })
}
})
const workerInfo = this.getWorkerInfo(workerNodeKey)