1 import Configuration from
'../utils/Configuration';
2 import { StationWorkerData
} from
'../types/Worker';
3 import Utils from
'../utils/Utils';
4 import WorkerAbstract from
'../worker/WorkerAbstract';
5 import WorkerFactory from
'../worker/WorkerFactory';
6 import { isMainThread
} from
'worker_threads';
7 import path from
'path';
9 export default class Bootstrap
{
10 private static instance
: Bootstrap
;
11 private started
: boolean;
12 private workerScript
: string;
13 private workerImplementationInstance
: WorkerAbstract
;
15 private constructor() {
17 this.workerScript
= path
.join(path
.resolve(__dirname
, '../'), 'charging-station', 'StationWorker.js');
20 public static getInstance(): Bootstrap
{
21 if (!Bootstrap
.instance
) {
22 Bootstrap
.instance
= new Bootstrap();
24 return Bootstrap
.instance
;
27 public async start(): Promise
<void> {
28 if (isMainThread
&& !this.started
) {
30 let numStationsTotal
= 0;
31 await this.getWorkerImplementationInstance().start();
32 // Start ChargingStation object in worker thread
33 if (Configuration
.getStationTemplateURLs()) {
34 for (const stationURL
of Configuration
.getStationTemplateURLs()) {
36 const nbStations
= stationURL
.numberOfStations
? stationURL
.numberOfStations
: 0;
37 for (let index
= 1; index
<= nbStations
; index
++) {
38 const workerData
: StationWorkerData
= {
40 templateFile
: path
.join(path
.resolve(__dirname
, '../'), 'assets', 'station-templates', path
.basename(stationURL
.file
))
42 await this.getWorkerImplementationInstance().addElement(workerData
);
46 // eslint-disable-next-line no-console
47 console
.error('Charging station start with template file ' + stationURL
.file
+ ' error ', error
);
51 console
.log('No stationTemplateURLs defined in configuration, exiting');
53 if (numStationsTotal
=== 0) {
54 console
.log('No charging station template enabled in configuration, exiting');
56 console
.log(`Charging station simulator 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)` : ''}`);
60 // eslint-disable-next-line no-console
61 console.error('Bootstrap start error ', error);
66 public async stop(): Promise<void> {
67 if (isMainThread && this.started) {
68 if (this.getWorkerImplementationInstance()) {
69 await this.getWorkerImplementationInstance().stop();
70 // Nullify to force worker implementation instance creation
71 this.workerImplementationInstance = null;
77 public async restart(): Promise<void> {
82 private getWorkerImplementationInstance(): WorkerAbstract {
83 if (!this.workerImplementationInstance) {
84 this.workerImplementationInstance = WorkerFactory.getWorkerImplementation<StationWorkerData>(this.workerScript, Configuration.getWorkerProcess(), {
85 poolMaxSize: Configuration.getWorkerPoolMaxSize(),
86 poolMinSize: Configuration.getWorkerPoolMinSize(),
87 elementsPerWorker: Configuration.getChargingStationsPerWorker()
90 return this.workerImplementationInstance;