From: Jérôme Benoit Date: Sun, 6 Aug 2023 10:38:32 +0000 (+0200) Subject: fix: avoid worker-threads restart at error during startup X-Git-Tag: v1.2.20~13 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=156c5f4ee0466adeb90e1e131e98b3f271955787;p=e-mobility-charging-stations-simulator.git fix: avoid worker-threads restart at error during startup Signed-off-by: Jérôme Benoit --- diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index c1fecdce..12f29294 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -19,6 +19,7 @@ import { sleep } from './WorkerUtils'; export class WorkerSet extends WorkerAbstract { public readonly emitter!: EventEmitter; private readonly workerSet: Set; + private workerStartup: boolean; /** * Creates a new `WorkerSet`. @@ -48,6 +49,7 @@ export class WorkerSet extends WorkerAbstract { if (this.workerOptions.poolOptions?.enableEvents) { this.emitter = new EventEmitter(); } + this.workerStartup = false; } get info(): SetInfo { @@ -114,6 +116,7 @@ export class WorkerSet extends WorkerAbstract { * Adds a new `WorkerSetElement`. */ private addWorkerSetElement(): WorkerSetElement { + this.workerStartup = true; const worker = new Worker(this.workerScript, { env: SHARE_ENV, ...this.workerOptions.poolOptions?.workerOptions, @@ -129,7 +132,7 @@ export class WorkerSet extends WorkerAbstract { worker.on('error', this.workerOptions.poolOptions?.errorHandler ?? EMPTY_FUNCTION); worker.on('error', (error) => { this.emitter?.emit(WorkerSetEvents.error, error); - if (this.workerOptions.poolOptions?.restartWorkerOnError) { + if (this.workerOptions.poolOptions?.restartWorkerOnError && !this.workerStartup) { this.addWorkerSetElement(); } }); @@ -138,6 +141,7 @@ export class WorkerSet extends WorkerAbstract { worker.once('exit', () => this.removeWorkerSetElement(this.getWorkerSetElementByWorker(worker)!), ); + this.workerStartup = false; const workerSetElement: WorkerSetElement = { worker, numberOfWorkerElements: 0 }; this.workerSet.add(workerSetElement); return workerSetElement;