From: Jérôme Benoit Date: Sat, 10 Jun 2023 23:37:59 +0000 (+0200) Subject: fix: make worker set respect all relevant worker options X-Git-Tag: v1.2.17~33 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=29bb4dee107ec14629baaf6ffc66d934c4c2dacf;p=e-mobility-charging-stations-simulator.git fix: make worker set respect all relevant worker options Signed-off-by: Jérôme Benoit --- diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index da5684b0..1938ae58 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -27,8 +27,17 @@ export class WorkerSet extends WorkerAbstract { */ constructor(workerScript: string, workerOptions?: WorkerOptions) { super(workerScript, workerOptions); + this.workerOptions.poolOptions = { + ...{ + enableEvents: true, + restartWorkerOnError: true, + }, + ...this.workerOptions.poolOptions, + }; this.workerSet = new Set(); - this.emitter = new EventEmitterAsyncResource(); + if (this.workerOptions?.poolOptions?.enableEvents) { + this.emitter = new EventEmitterAsyncResource(); + } } get info(): SetInfo { @@ -99,8 +108,12 @@ export class WorkerSet extends WorkerAbstract { this.workerOptions?.poolOptions?.errorHandler ?? WorkerConstants.EMPTY_FUNCTION ); worker.on('error', (error) => { - this.emitter.emit(WorkerSetEvents.error, error); - this.addWorkerSetElement(); + if (this.emitter !== undefined) { + this.emitter.emit(WorkerSetEvents.error, error); + } + if (this.workerOptions?.poolOptions?.restartWorkerOnError) { + this.addWorkerSetElement(); + } }); worker.on( 'online', @@ -110,7 +123,7 @@ export class WorkerSet extends WorkerAbstract { 'exit', this.workerOptions?.poolOptions?.exitHandler ?? WorkerConstants.EMPTY_FUNCTION ); - worker.on('exit', () => this.workerSet.delete(this.getWorkerSetElementByWorker(worker))); + worker.once('exit', () => this.workerSet.delete(this.getWorkerSetElementByWorker(worker))); const workerSetElement: WorkerSetElement = { worker, numberOfWorkerElements: 0 }; this.workerSet.add(workerSetElement); return workerSetElement;