fix: avoid concurrent ATG startup
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 4 Jun 2023 18:42:30 +0000 (20:42 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 4 Jun 2023 18:42:30 +0000 (20:42 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/ChargingStationWorker.ts

index 477a8d9c824f2922cb178631d5156ee840a96878..0389526fd6a9400a90404903a360d8d607448fd6 100644 (file)
@@ -31,11 +31,15 @@ export class AutomaticTransactionGenerator extends AsyncResource {
 
   public readonly connectorsStatus: Map<number, Status>;
   public started: boolean;
+  private starting: boolean;
+  private stopping: boolean;
   private readonly chargingStation: ChargingStation;
 
   private constructor(chargingStation: ChargingStation) {
     super(moduleName);
     this.started = false;
+    this.starting = false;
+    this.stopping = false;
     this.chargingStation = chargingStation;
     this.connectorsStatus = new Map<number, Status>();
     this.initializeConnectorsStatus();
@@ -63,8 +67,14 @@ export class AutomaticTransactionGenerator extends AsyncResource {
       logger.warn(`${this.logPrefix()} is already started`);
       return;
     }
+    if (this.starting === true) {
+      logger.warn(`${this.logPrefix()} is already starting`);
+      return;
+    }
+    this.starting = true;
     this.startConnectors();
     this.started = true;
+    this.starting = false;
   }
 
   public stop(): void {
@@ -72,8 +82,14 @@ export class AutomaticTransactionGenerator extends AsyncResource {
       logger.warn(`${this.logPrefix()} is already stopped`);
       return;
     }
+    if (this.stopping === true) {
+      logger.warn(`${this.logPrefix()} is already stopping`);
+      return;
+    }
+    this.stopping = true;
     this.stopConnectors();
     this.started = false;
+    this.stopping = false;
   }
 
   public startConnector(connectorId: number): void {
index de0b7a9fd95be118b6f6e154e9bc5a25504cf2be..741449e3b63e9b8126e4440c9c143116b61156b7 100644 (file)
@@ -10,6 +10,8 @@ import type { ChargingStationWorkerData } from '../types';
 import { Configuration } from '../utils';
 import { WorkerConstants, type WorkerMessage, WorkerMessageEvents } from '../worker';
 
+const moduleName = 'ChargingStationWorker';
+
 /**
  * Create and start a charging station instance
  *
@@ -21,7 +23,7 @@ const startChargingStation = (data: ChargingStationWorkerData): void => {
 
 class ChargingStationWorker extends AsyncResource {
   constructor() {
-    super('ChargingStationWorker');
+    super(moduleName);
     // Add message listener to create and start charging station from the main thread
     parentPort?.on('message', (message: WorkerMessage<ChargingStationWorkerData>) => {
       if (message.id === WorkerMessageEvents.startWorkerElement) {