From: Jérôme Benoit Date: Tue, 26 Jan 2021 18:36:54 +0000 (+0100) Subject: Fix workerSet process mode. X-Git-Tag: v1.0.1-0~105^2~16 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=b8da29bc984201049370a2bc497b7ae4143db32a;p=e-mobility-charging-stations-simulator.git Fix workerSet process mode. Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/StationWorker.ts b/src/charging-station/StationWorker.ts index 1b529190..65e2fb88 100644 --- a/src/charging-station/StationWorker.ts +++ b/src/charging-station/StationWorker.ts @@ -6,6 +6,12 @@ import Constants from '../utils/Constants'; import { ThreadWorker } from 'poolifier'; import Utils from '../utils/Utils'; +// Conditionally export ThreadWorker instance for pool usage +export let threadWorker; +if (Utils.workerPoolInUse()) { + threadWorker = new ThreadWorker(startChargingStation, { maxInactiveTime: Constants.WORKER_POOL_MAX_INACTIVE_TIME, async: false }); +} + if (!isMainThread) { // Add listener to start charging station from main thread addListener(); @@ -26,5 +32,3 @@ function startChargingStation(data: StationWorkerData) { const station = new ChargingStation(data.index , data.templateFile); station.start(); } - -export default new ThreadWorker(startChargingStation, { maxInactiveTime: Constants.WORKER_POOL_MAX_INACTIVE_TIME, async: false }); diff --git a/src/start.ts b/src/start.ts index 03bd4b8b..04dd2814 100644 --- a/src/start.ts +++ b/src/start.ts @@ -8,7 +8,11 @@ class Bootstrap { static async start() { try { let numStationsTotal = 0; - const workerImplementation: Wrk = WorkerFactory.getWorkerImpl('./dist/charging-station/StationWorker.js'); + const workerImplementation: Wrk = WorkerFactory.getWorkerImpl('./dist/charging-station/StationWorker.js', Configuration.getWorkerProcess(), { + poolMaxSize: Configuration.getWorkerPoolMaxSize(), + poolMinSize: Configuration.getWorkerPoolMinSize(), + elementsPerWorker: Configuration.getChargingStationsPerWorker() + }); await workerImplementation.start(); // Start ChargingStation object in worker thread if (Configuration.getStationTemplateURLs()) { diff --git a/src/types/Worker.ts b/src/types/Worker.ts index 6ec9e999..bc297268 100644 --- a/src/types/Worker.ts +++ b/src/types/Worker.ts @@ -6,6 +6,12 @@ export enum WorkerProcessType { STATIC_POOL = 'staticPool' } +export interface WorkerOptions { + poolMaxSize?: number; + poolMinSize?: number; + elementsPerWorker?: number; +} + export interface WorkerData { } export interface StationWorkerData extends WorkerData { diff --git a/src/utils/Statistics.ts b/src/utils/Statistics.ts index 966f1b57..05a22551 100644 --- a/src/utils/Statistics.ts +++ b/src/utils/Statistics.ts @@ -14,7 +14,7 @@ export default class Statistics { public constructor(objName: string) { this.objId = objName; - this.commandsStatistics = { id: this.objId ? this.objId : ' Object id not specified', commandsStatisticsData: {} } as CommandStatistics; + this.commandsStatistics = { id: this.objId ? this.objId : ' Object id not specified', commandsStatisticsData: {} }; } public addMessage(command: RequestCommand | IncomingRequestCommand, messageType: MessageType): void { diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 2d923daa..24145627 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,19 +1,35 @@ -import Configuration from '../utils/Configuration'; +import { WorkerOptions, WorkerProcessType } from '../types/Worker'; + +import Utils from '../utils/Utils'; import WorkerDynamicPool from './WorkerDynamicPool'; -import { WorkerProcessType } from '../types/Worker'; import WorkerSet from './WorkerSet'; import WorkerStaticPool from './WorkerStaticPool'; import Wrk from './Wrk'; export default class WorkerFactory { - public static getWorkerImpl(workerScript: string): Wrk { - switch (Configuration.getWorkerProcess()) { + public static getWorkerImpl(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): Wrk { + if (Utils.isUndefined(options)) { + options = {} as WorkerOptions; + } + switch (workerProcessType) { case WorkerProcessType.WORKER_SET: - return new WorkerSet(workerScript, Configuration.getChargingStationsPerWorker()); + if (Utils.isUndefined(options.elementsPerWorker)) { + options.elementsPerWorker = 1; + } + return new WorkerSet(workerScript, options.elementsPerWorker); case WorkerProcessType.STATIC_POOL: - return new WorkerStaticPool(workerScript, Configuration.getWorkerPoolMaxSize()); + if (Utils.isUndefined(options.poolMaxSize)) { + options.elementsPerWorker = 16; + } + return new WorkerStaticPool(workerScript, options.poolMaxSize); case WorkerProcessType.DYNAMIC_POOL: - return new WorkerDynamicPool(workerScript, Configuration.getWorkerPoolMinSize(), Configuration.getWorkerPoolMaxSize()); + if (Utils.isUndefined(options.poolMinSize)) { + options.elementsPerWorker = 4; + } + if (Utils.isUndefined(options.poolMaxSize)) { + options.elementsPerWorker = 16; + } + return new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize); default: return null; }