- private initializeWorkerImplementation(): void {
- this.workerImplementation === null &&
- (this.workerImplementation = WorkerFactory.getWorkerImplementation<ChargingStationWorkerData>(
- this.workerScript,
- Configuration.getWorker().processType,
- {
- workerStartDelay: Configuration.getWorker().startDelay,
- elementStartDelay: Configuration.getWorker().elementStartDelay,
- poolMaxSize: Configuration.getWorker().poolMaxSize,
- poolMinSize: Configuration.getWorker().poolMinSize,
- elementsPerWorker: Configuration.getWorker().elementsPerWorker,
- poolOptions: {
- workerChoiceStrategy: Configuration.getWorker().poolStrategy,
- },
- messageHandler: this.messageHandler.bind(this) as (
- msg: ChargingStationWorkerMessage<ChargingStationWorkerMessageData>
- ) => void,
+ private async waitChargingStationsStopped (): Promise<string> {
+ return await new Promise<string>((resolve, reject) => {
+ const waitTimeout = setTimeout(() => {
+ const message = `Timeout ${formatDurationMilliSeconds(
+ Constants.STOP_CHARGING_STATIONS_TIMEOUT
+ )} reached at stopping charging stations`
+ console.warn(chalk.yellow(message))
+ reject(new Error(message))
+ }, Constants.STOP_CHARGING_STATIONS_TIMEOUT)
+ waitChargingStationEvents(
+ this,
+ ChargingStationWorkerMessageEvents.stopped,
+ this.numberOfChargingStations
+ )
+ .then(() => {
+ resolve('Charging stations stopped')
+ })
+ .catch(reject)
+ .finally(() => {
+ clearTimeout(waitTimeout)
+ })
+ })
+ }
+
+ private initializeWorkerImplementation (workerConfiguration: WorkerConfiguration): void {
+ let elementsPerWorker: number | undefined
+ switch (workerConfiguration?.elementsPerWorker) {
+ case 'auto':
+ elementsPerWorker =
+ this.numberOfChargingStations > availableParallelism()
+ ? Math.round(this.numberOfChargingStations / (availableParallelism() * 1.5))
+ : 1
+ break
+ case 'all':
+ elementsPerWorker = this.numberOfChargingStations
+ break
+ }
+ this.workerImplementation = WorkerFactory.getWorkerImplementation<ChargingStationWorkerData>(
+ join(
+ dirname(fileURLToPath(import.meta.url)),
+ `ChargingStationWorker${extname(fileURLToPath(import.meta.url))}`
+ ),
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ workerConfiguration.processType!,
+ {
+ workerStartDelay: workerConfiguration.startDelay,
+ elementStartDelay: workerConfiguration.elementStartDelay,
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ poolMaxSize: workerConfiguration.poolMaxSize!,
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ poolMinSize: workerConfiguration.poolMinSize!,
+ elementsPerWorker: elementsPerWorker ?? (workerConfiguration.elementsPerWorker as number),
+ poolOptions: {
+ messageHandler: this.messageHandler.bind(this) as (message: unknown) => void,
+ workerOptions: { resourceLimits: workerConfiguration.resourceLimits }