From: Jérôme Benoit Date: Wed, 5 Jul 2023 16:41:30 +0000 (+0200) Subject: fix: wait for workers exit at worket set stop X-Git-Tag: v1.2.18~74 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=dbc29904c4cdd813b9ff179734175b6bb6cc06e2;p=e-mobility-charging-stations-simulator.git fix: wait for workers exit at worket set stop Signed-off-by: Jérôme Benoit --- diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index a2e6faf0..d761a12a 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -72,7 +72,13 @@ export class WorkerSet extends WorkerAbstract { /** @inheritDoc */ public async stop(): Promise { for (const workerSetElement of this.workerSet) { + const workerExitPromise = new Promise((resolve) => { + workerSetElement.worker.on('exit', () => { + resolve(); + }); + }); await workerSetElement.worker.terminate(); + await workerExitPromise; } this.workerSet.clear(); } @@ -126,12 +132,18 @@ export class WorkerSet extends WorkerAbstract { 'exit', this.workerOptions?.poolOptions?.exitHandler ?? WorkerConstants.EMPTY_FUNCTION ); - worker.once('exit', () => this.workerSet.delete(this.getWorkerSetElementByWorker(worker))); + worker.once('exit', () => + this.removeWorkerSetElement(this.getWorkerSetElementByWorker(worker)) + ); const workerSetElement: WorkerSetElement = { worker, numberOfWorkerElements: 0 }; this.workerSet.add(workerSetElement); return workerSetElement; } + private removeWorkerSetElement(workerSetElement: WorkerSetElement): void { + this.workerSet.delete(workerSetElement); + } + private async getWorkerSetElement(): Promise { let chosenWorkerSetElement: WorkerSetElement; for (const workerSetElement of this.workerSet) {