X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=6bf84b875622ebaece3d8b4b8dae3c637593ed68;hb=ee9f0ad39b47fe80bb08d159024dad0fae81f6c5;hp=512a822df295d17ba7578302274c4d87e615e9b9;hpb=283b4a1183daabc04d40a567c0c2ee8ba98dbe27;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 512a822d..6bf84b87 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -301,7 +301,9 @@ export abstract class AbstractWorker< if (this.isMain) { throw new Error('Cannot handle message to worker in main worker') } else if (message.workerId != null && message.workerId !== this.id) { - throw new Error('Message worker id does not match the worker id') + throw new Error( + `Message worker id ${message.workerId} does not match the worker id ${this.id}` + ) } else if (message.workerId === this.id) { if (message.statistics != null) { // Statistics message received @@ -326,8 +328,30 @@ export abstract class AbstractWorker< */ protected handleKillMessage (message: MessageValue): void { this.stopCheckActive() - this.opts.killHandler?.() - this.emitDestroy() + if (isAsyncFunction(this.opts.killHandler)) { + (this.opts.killHandler?.() as Promise) + .then(() => { + this.sendToMainWorker({ kill: 'success', workerId: this.id }) + return null + }) + .catch(() => { + this.sendToMainWorker({ kill: 'failure', workerId: this.id }) + }) + .finally(() => { + this.emitDestroy() + }) + .catch(EMPTY_FUNCTION) + } else { + try { + // eslint-disable-next-line @typescript-eslint/no-invalid-void-type + this.opts.killHandler?.() as void + this.sendToMainWorker({ kill: 'success', workerId: this.id }) + } catch { + this.sendToMainWorker({ kill: 'failure', workerId: this.id }) + } finally { + this.emitDestroy() + } + } } /**