X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStationWorker.ts;h=042d3d5bfc2b3bc913484c5a0f5d0982b982148f;hb=3b09e788c6dab8e5a8b3314e8e69ede16ff82fcc;hp=2fd83728d9ff3cb0e8fb1d0bcdff24069946e29a;hpb=8114d10e3893e96bb725ce2fca9744429ee4b75b;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index 2fd83728..042d3d5b 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -1,51 +1,70 @@ -// Partial Copyright Jerome Benoit. 2021. All Rights Reserved. +// Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved. -import { parentPort, workerData } from 'worker_threads'; +import { parentPort } from 'node:worker_threads' -import { ThreadWorker } from 'poolifier'; +import { ThreadWorker } from 'poolifier' -import { +import { BaseError } from '../exception/index.js' +import type { + ChargingStationInfo, ChargingStationWorkerData, - ChargingStationWorkerMessage, - ChargingStationWorkerMessageEvents, -} from '../types/ChargingStationWorker'; -import Utils from '../utils/Utils'; -import WorkerConstants from '../worker/WorkerConstants'; -import ChargingStation from './ChargingStation'; -import { ChargingStationUtils } from './ChargingStationUtils'; + ChargingStationWorkerEventError, + ChargingStationWorkerMessage +} from '../types/index.js' +import { Configuration } from '../utils/index.js' +import { type WorkerMessage, WorkerMessageEvents } from '../worker/index.js' +import { ChargingStation } from './ChargingStation.js' -// Conditionally export ThreadWorker instance for pool usage -export let threadWorker: ThreadWorker; -if (ChargingStationUtils.workerPoolInUse()) { - threadWorker = new ThreadWorker(startChargingStation, { - maxInactiveTime: WorkerConstants.POOL_MAX_INACTIVE_TIME, - async: false, - }); +export let chargingStationWorker: object +if (Configuration.workerPoolInUse()) { + chargingStationWorker = new ThreadWorker< + ChargingStationWorkerData, + ChargingStationInfo | undefined + >((data?: ChargingStationWorkerData): ChargingStationInfo | undefined => { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, no-new + return new ChargingStation(data!.index, data!.templateFile, data!.options).stationInfo + }) } else { - // Add message listener to start charging station from main thread - addMessageListener(); - if (!Utils.isUndefined(workerData)) { - startChargingStation(workerData as ChargingStationWorkerData); - } -} - -/** - * Listen messages send by the main thread - */ -function addMessageListener(): void { - parentPort?.on('message', (message: ChargingStationWorkerMessage) => { - if (message.id === ChargingStationWorkerMessageEvents.START_WORKER_ELEMENT) { - startChargingStation(message.data); + // eslint-disable-next-line @typescript-eslint/no-extraneous-class + class ChargingStationWorker { + constructor () { + parentPort?.on('message', (message: WorkerMessage) => { + switch (message.event) { + case WorkerMessageEvents.addWorkerElement: + try { + const chargingStation = new ChargingStation( + message.data.index, + message.data.templateFile, + message.data.options + ) + parentPort?.postMessage({ + event: WorkerMessageEvents.addedWorkerElement, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + data: chargingStation.stationInfo! + } satisfies ChargingStationWorkerMessage) + } catch (error) { + parentPort?.postMessage({ + event: WorkerMessageEvents.workerElementError, + data: { + event: message.event, + name: (error as Error).name, + message: (error as Error).message, + stack: (error as Error).stack + } + } satisfies ChargingStationWorkerMessage) + } + break + default: + throw new BaseError( + `Unknown worker event: '${message.event}' received with data: '${JSON.stringify( + message.data, + undefined, + 2 + )}'` + ) + } + }) } - }); -} - -/** - * Create and start a charging station instance - * - * @param data workerData - */ -function startChargingStation(data: ChargingStationWorkerData): void { - const station = new ChargingStation(data.index, data.templateFile); - station.start(); + } + chargingStationWorker = new ChargingStationWorker() }