Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
import { ThreadWorker } from 'poolifier';
import Utils from '../utils/Utils';
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();
if (!isMainThread) {
// Add listener to start charging station from main thread
addListener();
const station = new ChargingStation(data.index , data.templateFile);
station.start();
}
const station = new ChargingStation(data.index , data.templateFile);
station.start();
}
-
-export default new ThreadWorker(startChargingStation, { maxInactiveTime: Constants.WORKER_POOL_MAX_INACTIVE_TIME, async: false });
static async start() {
try {
let numStationsTotal = 0;
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()) {
await workerImplementation.start();
// Start ChargingStation object in worker thread
if (Configuration.getStationTemplateURLs()) {
STATIC_POOL = 'staticPool'
}
STATIC_POOL = 'staticPool'
}
+export interface WorkerOptions {
+ poolMaxSize?: number;
+ poolMinSize?: number;
+ elementsPerWorker?: number;
+}
+
export interface WorkerData { }
export interface StationWorkerData extends WorkerData {
export interface WorkerData { }
export interface StationWorkerData extends WorkerData {
public constructor(objName: string) {
this.objId = objName;
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 {
}
public addMessage(command: RequestCommand | IncomingRequestCommand, messageType: MessageType): void {
-import Configuration from '../utils/Configuration';
+import { WorkerOptions, WorkerProcessType } from '../types/Worker';
+
+import Utils from '../utils/Utils';
import WorkerDynamicPool from './WorkerDynamicPool';
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 {
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:
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:
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:
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);