fix: avoid worker-threads restart at error during startup
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 6 Aug 2023 10:38:32 +0000 (12:38 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 6 Aug 2023 10:38:32 +0000 (12:38 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/worker/WorkerSet.ts

index c1fecdcec2f7f6b03adfe412a2bfe18c8d997935..12f29294ea54b98ad82b1cca9013662725f3c223 100644 (file)
@@ -19,6 +19,7 @@ import { sleep } from './WorkerUtils';
 export class WorkerSet extends WorkerAbstract<WorkerData> {
   public readonly emitter!: EventEmitter;
   private readonly workerSet: Set<WorkerSetElement>;
+  private workerStartup: boolean;
 
   /**
    * Creates a new `WorkerSet`.
@@ -48,6 +49,7 @@ export class WorkerSet extends WorkerAbstract<WorkerData> {
     if (this.workerOptions.poolOptions?.enableEvents) {
       this.emitter = new EventEmitter();
     }
+    this.workerStartup = false;
   }
 
   get info(): SetInfo {
@@ -114,6 +116,7 @@ export class WorkerSet extends WorkerAbstract<WorkerData> {
    * 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<WorkerData> {
     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<WorkerData> {
     worker.once('exit', () =>
       this.removeWorkerSetElement(this.getWorkerSetElementByWorker(worker)!),
     );
+    this.workerStartup = false;
     const workerSetElement: WorkerSetElement = { worker, numberOfWorkerElements: 0 };
     this.workerSet.add(workerSetElement);
     return workerSetElement;