From 1135386576395b90768d5e03a16fb44d340361e0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 4 Jun 2023 20:42:30 +0200 Subject: [PATCH] fix: avoid concurrent ATG startup MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 16 ++++++++++++++++ src/charging-station/ChargingStationWorker.ts | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 477a8d9c..0389526f 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -31,11 +31,15 @@ export class AutomaticTransactionGenerator extends AsyncResource { public readonly connectorsStatus: Map; 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(); 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 { diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index de0b7a9f..741449e3 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -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) => { if (message.id === WorkerMessageEvents.startWorkerElement) { -- 2.34.1