From: Jérôme Benoit Date: Thu, 21 Jan 2021 12:51:58 +0000 (+0100) Subject: Fix worker startup and cleanup some attributes in Wrk class. X-Git-Tag: v1.0.1-0~134 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=4faad557cd49253297c7d0230db2eecfd850b4f4;p=e-mobility-charging-stations-simulator.git Fix worker startup and cleanup some attributes in Wrk class. Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index d6a825ed..31d4d852 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -1,4 +1,4 @@ -import { AuthorizationStatus, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../types/ocpp/1.6/Transaction'; +import { AuthorizationStatus, AuthorizeResponse, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../types/ocpp/1.6/Transaction'; import { PerformanceObserver, performance } from 'perf_hooks'; import ChargingStation from './ChargingStation'; @@ -89,7 +89,7 @@ export default class AutomaticTransactionGenerator { if (start < this._chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) { skip = 0; // Start transaction - let startResponse: StartTransactionResponse; + let startResponse: StartTransactionResponse | AuthorizeResponse; if (this._chargingStation.getEnableStatistics()) { const startTransaction = performance.timerify(this.startTransaction); this._performanceObserver.observe({ entryTypes: ['function'] }); @@ -127,7 +127,7 @@ export default class AutomaticTransactionGenerator { } // eslint-disable-next-line consistent-this - private async startTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise { + private async startTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise { if (self._chargingStation.hasAuthorizedTags()) { const tagId = self._chargingStation.getRandomTagId(); if (self._chargingStation.stationInfo.AutomaticTransactionGenerator.requireAuthorize) { @@ -138,7 +138,7 @@ export default class AutomaticTransactionGenerator { // Start transaction return await self._chargingStation.sendStartTransaction(connectorId, tagId); } - return authorizeResponse as StartTransactionResponse; + return authorizeResponse; } logger.info(self._logPrefix(connectorId) + ' start transaction for tagID ' + tagId); // Start transaction diff --git a/src/charging-station/Worker.ts b/src/charging-station/Worker.ts index 41374297..efa327fd 100644 --- a/src/charging-station/Worker.ts +++ b/src/charging-station/Worker.ts @@ -8,8 +8,6 @@ import WorkerData from '../types/WorkerData'; export default class Wrk { private _workerScript: string; private _workerData: WorkerData; - private _index: number; - private _maxWorkerElements: number; private _worker: Worker; /** @@ -17,24 +15,13 @@ export default class Wrk { * * @param {string} workerScript * @param {WorkerData} workerData - * @param {number} maxWorkerElements */ - constructor(workerScript: string, workerData: WorkerData, maxWorkerElements = 1) { + constructor(workerScript: string, workerData: WorkerData) { this._workerData = workerData; - this._index = workerData.index; this._workerScript = workerScript; if (Configuration.useWorkerPool()) { WorkerPool.maxConcurrentWorkers = Configuration.getWorkerPoolSize(); } - this._maxWorkerElements = maxWorkerElements; - } - - /** - * @return {number} - * @public - */ - public get maxWorkerElements(): number { - return this._maxWorkerElements; } /** @@ -62,7 +49,6 @@ export default class Wrk { return; } this._workerData = workerData; - this._index = workerData.index; this._worker.postMessage({ id : Constants.START_WORKER_ELEMENT, workerData: workerData }); } @@ -96,7 +82,7 @@ export default class Wrk { worker.on('error', reject); worker.on('exit', (code) => { if (code !== 0) { - reject(new Error(`Worker id ${this._index} stopped with exit code ${code}`)); + reject(new Error(`Worker stopped with exit code ${code}`)); } }); this._worker = worker; diff --git a/src/start.ts b/src/start.ts index 3b9fc75b..2bcfbae5 100644 --- a/src/start.ts +++ b/src/start.ts @@ -9,10 +9,12 @@ class Bootstrap { try { let numStationsTotal = 0; let numConcurrentWorkers = 0; + const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker(); + let chargingStationsPerWorkerCounter = 0; let worker: Wrk; // Start each ChargingStation object in a worker thread if (Configuration.getStationTemplateURLs()) { - for await (const stationURL of Configuration.getStationTemplateURLs()) { + for (const stationURL of Configuration.getStationTemplateURLs()) { try { const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0; numStationsTotal += nbStations; @@ -25,22 +27,18 @@ class Bootstrap { worker = new Wrk('./dist/charging-station/StationWorker.js', workerData); worker.start().catch(() => { }); numConcurrentWorkers = Configuration.getWorkerPoolSize(); - } else { - const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker(); - let chargingStationsPerWorkerCounter = 0; - if (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter === chargingStationsPerWorker) { - // Start new Wrk with one charging station - worker = new Wrk('./dist/charging-station/StationWorker.js', workerData, chargingStationsPerWorker); - worker.start().catch(() => { }); - numConcurrentWorkers++; - chargingStationsPerWorkerCounter = 1; - // Start Wrk sequentially to optimize memory at start time - await Utils.sleep(Constants.START_WORKER_DELAY); - } else { - // Add charging station to existing Wrk - worker.addWorkerElement(workerData); - chargingStationsPerWorkerCounter++; - } + } else if (!Configuration.useWorkerPool() && (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter === chargingStationsPerWorker)) { + // Start new Wrk with one charging station + worker = new Wrk('./dist/charging-station/StationWorker.js', workerData); + worker.start().catch(() => { }); + numConcurrentWorkers++; + chargingStationsPerWorkerCounter = 1; + // Start Wrk sequentially to optimize memory at start time + await Utils.sleep(Constants.START_WORKER_DELAY); + } else if (!Configuration.useWorkerPool()) { + // Add charging station to existing Wrk + worker.addWorkerElement(workerData); + chargingStationsPerWorkerCounter++; } } } catch (error) { @@ -63,4 +61,8 @@ class Bootstrap { } } -Bootstrap.start(); +Bootstrap.start().catch( + (error) => { + console.error(error); + } +);