| 1 | // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
| 2 | |
| 3 | import { AsyncResource } from 'node:async_hooks'; |
| 4 | import { parentPort } from 'node:worker_threads'; |
| 5 | |
| 6 | import { ThreadWorker } from 'poolifier'; |
| 7 | |
| 8 | import { ChargingStation } from './ChargingStation'; |
| 9 | import { BaseError } from '../exception'; |
| 10 | import type { ChargingStationWorkerData } from '../types'; |
| 11 | import { Configuration } from '../utils'; |
| 12 | import { type WorkerMessage, WorkerMessageEvents } from '../worker'; |
| 13 | |
| 14 | const moduleName = 'ChargingStationWorker'; |
| 15 | |
| 16 | /** |
| 17 | * Creates and starts a charging station instance |
| 18 | * |
| 19 | * @param data - workerData |
| 20 | */ |
| 21 | const startChargingStation = (data?: ChargingStationWorkerData): void => { |
| 22 | new ChargingStation(data!.index, data!.templateFile).start(); |
| 23 | }; |
| 24 | |
| 25 | class ChargingStationWorker extends AsyncResource { |
| 26 | constructor() { |
| 27 | super(moduleName); |
| 28 | // Add message listener to create and start charging station from the main thread |
| 29 | parentPort?.on('message', (message: WorkerMessage<ChargingStationWorkerData>) => { |
| 30 | switch (message.event) { |
| 31 | case WorkerMessageEvents.startWorkerElement: |
| 32 | try { |
| 33 | this.runInAsyncScope( |
| 34 | startChargingStation.bind(this) as (data?: ChargingStationWorkerData) => void, |
| 35 | this, |
| 36 | message.data, |
| 37 | ); |
| 38 | parentPort?.postMessage({ |
| 39 | event: WorkerMessageEvents.startedWorkerElement, |
| 40 | }); |
| 41 | } catch (error) { |
| 42 | parentPort?.postMessage({ |
| 43 | event: WorkerMessageEvents.startWorkerElementError, |
| 44 | data: { |
| 45 | message: (error as Error).message, |
| 46 | stack: (error as Error).stack, |
| 47 | }, |
| 48 | }); |
| 49 | } |
| 50 | break; |
| 51 | default: |
| 52 | throw new BaseError( |
| 53 | `Unknown worker event: '${message.event}' received with data: '${JSON.stringify( |
| 54 | message.data, |
| 55 | undefined, |
| 56 | 2, |
| 57 | )}'`, |
| 58 | ); |
| 59 | } |
| 60 | }); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | export let chargingStationWorker: ChargingStationWorker | ThreadWorker<ChargingStationWorkerData>; |
| 65 | if (Configuration.workerPoolInUse()) { |
| 66 | chargingStationWorker = new ThreadWorker<ChargingStationWorkerData>(startChargingStation); |
| 67 | } else { |
| 68 | chargingStationWorker = new ChargingStationWorker(); |
| 69 | } |