private version: string = version;
private started: boolean;
private workerScript: string;
- private workerImplementationInstance: WorkerAbstract | null = null;
+ private workerImplementation: WorkerAbstract | null = null;
private constructor() {
this.started = false;
this.workerScript = path.join(path.resolve(__dirname, '../'), 'charging-station', 'StationWorker.js');
+ this.initWorkerImplementation();
Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart());
}
if (isMainThread && !this.started) {
try {
let numStationsTotal = 0;
- await this.getWorkerImplementationInstance()?.start();
+ await this.workerImplementation.start();
// Start ChargingStation object in worker thread
if (Configuration.getStationTemplateURLs()) {
for (const stationURL of Configuration.getStationTemplateURLs()) {
index,
templateFile: path.join(path.resolve(__dirname, '../'), 'assets', 'station-templates', path.basename(stationURL.file))
};
- await this.getWorkerImplementationInstance()?.addElement(workerData);
+ await this.workerImplementation.addElement(workerData);
numStationsTotal++;
}
} catch (error) {
if (numStationsTotal === 0) {
console.log('No charging station template enabled in configuration, exiting');
} else {
- console.log(`Charging station simulator ${this.version} started with ${numStationsTotal.toString()} charging station(s) and ${Utils.workerDynamicPoolInUse() ? `${Configuration.getWorkerPoolMinSize().toString()}/` : ''}${this.getWorkerImplementationInstance().size}${Utils.workerPoolInUse() ? `/${Configuration.getWorkerPoolMaxSize().toString()}` : ''} worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode${this.getWorkerImplementationInstance().maxElementsPerWorker ? ` (${this.getWorkerImplementationInstance().maxElementsPerWorker} charging station(s) per worker)` : ''}`);
+ console.log(`Charging station simulator ${this.version} started with ${numStationsTotal.toString()} charging station(s) and ${Utils.workerDynamicPoolInUse() ? `${Configuration.getWorkerPoolMinSize().toString()}/` : ''}${this.workerImplementation.size}${Utils.workerPoolInUse() ? `/${Configuration.getWorkerPoolMaxSize().toString()}` : ''} worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode${this.workerImplementation.maxElementsPerWorker ? ` (${this.workerImplementation.maxElementsPerWorker} charging station(s) per worker)` : ''}`);
}
this.started = true;
} catch (error) {
public async stop(): Promise<void> {
if (isMainThread && this.started) {
- await this.getWorkerImplementationInstance()?.stop();
- // Nullify to force worker implementation instance creation
- this.workerImplementationInstance = null;
+ await this.workerImplementation.stop();
}
this.started = false;
}
public async restart(): Promise<void> {
await this.stop();
+ this.initWorkerImplementation(true);
await this.start();
}
- private getWorkerImplementationInstance(): WorkerAbstract | null {
- if (!this.workerImplementationInstance) {
- this.workerImplementationInstance = WorkerFactory.getWorkerImplementation<StationWorkerData>(this.workerScript, Configuration.getWorkerProcess(),
- {
- startDelay: Configuration.getWorkerStartDelay(),
- poolMaxSize: Configuration.getWorkerPoolMaxSize(),
- poolMinSize: Configuration.getWorkerPoolMinSize(),
- elementsPerWorker: Configuration.getChargingStationsPerWorker(),
- poolOptions: {
- workerChoiceStrategy: Configuration.getWorkerPoolStrategy()
- }
- });
+ private initWorkerImplementation(forceInstantiation = false) {
+ this.workerImplementation = WorkerFactory.getWorkerImplementation<StationWorkerData>(this.workerScript, Configuration.getWorkerProcess(),
+ {
+ startDelay: Configuration.getWorkerStartDelay(),
+ poolMaxSize: Configuration.getWorkerPoolMaxSize(),
+ poolMinSize: Configuration.getWorkerPoolMinSize(),
+ elementsPerWorker: Configuration.getChargingStationsPerWorker(),
+ poolOptions: {
+ workerChoiceStrategy: Configuration.getWorkerPoolStrategy()
+ }
+ }, forceInstantiation);
+ if (!this.workerImplementation) {
+ throw new Error('Worker implementation not found');
}
- return this.workerImplementationInstance;
}
}
import { WorkerUtils } from './WorkerUtils';
export default class WorkerDynamicPool<T> extends WorkerAbstract {
- private pool: DynamicPool;
+ private pool: DynamicThreadPool<WorkerData>;
/**
* Create a new `WorkerDynamicPool`.
*/
constructor(workerScript: string, min: number, max: number, workerStartDelay?: number, opts?: PoolOptions<Worker>) {
super(workerScript, workerStartDelay);
- this.pool = DynamicPool.getInstance(min, max, this.workerScript, opts);
+ opts.exitHandler = opts?.exitHandler ?? WorkerUtils.defaultExitHandler;
+ this.pool = new DynamicThreadPool<WorkerData>(min, max, this.workerScript, opts);
}
get size(): number {
* @public
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
- public async start(): Promise<void> { }
+ public async start(): Promise<void> {}
/**
*
await Utils.sleep(this.workerStartDelay);
}
}
-
-class DynamicPool extends DynamicThreadPool<WorkerData> {
- private static instance: DynamicPool;
-
- private constructor(min: number, max: number, workerScript: string, opts?: PoolOptions<Worker>) {
- super(min, max, workerScript, opts);
- }
-
- public static getInstance(min: number, max: number, workerScript: string, opts?: PoolOptions<Worker>): DynamicPool {
- if (!DynamicPool.instance) {
- opts.exitHandler = opts?.exitHandler ?? WorkerUtils.defaultExitHandler;
- DynamicPool.instance = new DynamicPool(min, max, workerScript, opts);
- }
- return DynamicPool.instance;
- }
-}
import { isMainThread } from 'worker_threads';
export default class WorkerFactory {
- public static getWorkerImplementation<T>(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions): WorkerAbstract | null {
+ private static workerImplementation: WorkerAbstract | null;
+
+ private constructor() {}
+
+ public static getWorkerImplementation<T>(workerScript: string, workerProcessType: WorkerProcessType, options?: WorkerOptions, forceInstantiation = false): WorkerAbstract | null {
if (!isMainThread) {
throw new Error('Trying to get a worker implementation outside the main thread');
}
- options = options ?? {} as WorkerOptions;
- options.startDelay = options.startDelay ?? Constants.WORKER_START_DELAY;
- switch (workerProcessType) {
- case WorkerProcessType.WORKER_SET:
- options.elementsPerWorker = options.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER;
- return new WorkerSet<T>(workerScript, options.elementsPerWorker, options.startDelay);
- case WorkerProcessType.STATIC_POOL:
- options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
- return new WorkerStaticPool<T>(workerScript, options.poolMaxSize, options.startDelay, options.poolOptions);
- case WorkerProcessType.DYNAMIC_POOL:
- options.poolMinSize = options.poolMinSize ?? Constants.DEFAULT_WORKER_POOL_MIN_SIZE;
- options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
- return new WorkerDynamicPool<T>(workerScript, options.poolMinSize, options.poolMaxSize, options.startDelay, options.poolOptions);
- default:
- return null;
+ if (!WorkerFactory.workerImplementation || forceInstantiation) {
+ options = options ?? {} as WorkerOptions;
+ options.startDelay = options.startDelay ?? Constants.WORKER_START_DELAY;
+ WorkerFactory.workerImplementation = null;
+ switch (workerProcessType) {
+ case WorkerProcessType.WORKER_SET:
+ options.elementsPerWorker = options.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER;
+ WorkerFactory.workerImplementation = new WorkerSet<T>(workerScript, options.elementsPerWorker, options.startDelay);
+ break;
+ case WorkerProcessType.STATIC_POOL:
+ options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
+ WorkerFactory.workerImplementation = new WorkerStaticPool<T>(workerScript, options.poolMaxSize, options.startDelay, options.poolOptions);
+ break;
+ case WorkerProcessType.DYNAMIC_POOL:
+ options.poolMinSize = options.poolMinSize ?? Constants.DEFAULT_WORKER_POOL_MIN_SIZE;
+ options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
+ WorkerFactory.workerImplementation = new WorkerDynamicPool<T>(workerScript, options.poolMinSize, options.poolMaxSize, options.startDelay, options.poolOptions);
+ break;
+ }
}
+ return WorkerFactory.workerImplementation;
}
}
import { WorkerUtils } from './WorkerUtils';
export default class WorkerStaticPool<T> extends WorkerAbstract {
- private pool: StaticPool;
+ private pool: FixedThreadPool<WorkerData>;
/**
* Create a new `WorkerStaticPool`.
*/
constructor(workerScript: string, numberOfThreads: number, startWorkerDelay?: number, opts?: PoolOptions<Worker>) {
super(workerScript, startWorkerDelay);
- this.pool = StaticPool.getInstance(numberOfThreads, this.workerScript, opts);
+ opts.exitHandler = opts?.exitHandler ?? WorkerUtils.defaultExitHandler;
+ this.pool = new FixedThreadPool(numberOfThreads, this.workerScript, opts);
}
get size(): number {
* @public
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
- public async start(): Promise<void> { }
+ public async start(): Promise<void> {}
/**
*
await Utils.sleep(this.workerStartDelay);
}
}
-
-class StaticPool extends FixedThreadPool<WorkerData> {
- private static instance: StaticPool;
-
- private constructor(numberOfThreads: number, workerScript: string, opts?: PoolOptions<Worker>) {
- super(numberOfThreads, workerScript, opts);
- }
-
- public static getInstance(numberOfThreads: number, workerScript: string, opts?: PoolOptions<Worker>): StaticPool {
- if (!StaticPool.instance) {
- opts.exitHandler = opts?.exitHandler ?? WorkerUtils.defaultExitHandler;
- StaticPool.instance = new StaticPool(numberOfThreads, workerScript, opts);
- }
- return StaticPool.instance;
- }
-}