Commit | Line | Data |
---|---|---|
edd13439 | 1 | // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
c8eeb62b | 2 | |
66a7748d | 3 | import { parentPort } from 'node:worker_threads' |
8114d10e | 4 | |
66a7748d | 5 | import { ThreadWorker } from 'poolifier' |
8114d10e | 6 | |
66a7748d JB |
7 | import { ChargingStation } from './ChargingStation.js' |
8 | import { BaseError } from '../exception/index.js' | |
9 | import type { ChargingStationWorkerData } from '../types/index.js' | |
10 | import { Configuration } from '../utils/index.js' | |
11 | import { type WorkerMessage, WorkerMessageEvents } from '../worker/index.js' | |
11353865 | 12 | |
e8a92d57 | 13 | /** |
361c98f5 | 14 | * Creates and starts a charging station instance |
e8a92d57 | 15 | * |
d769e04a | 16 | * @param data - data sent to worker |
e8a92d57 | 17 | */ |
a37fc6dc | 18 | const startChargingStation = (data?: ChargingStationWorkerData): void => { |
66a7748d JB |
19 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
20 | new ChargingStation(data!.index, data!.templateFile).start() | |
21 | } | |
e8a92d57 | 22 | |
66a7748d | 23 | // eslint-disable-next-line @typescript-eslint/no-extraneous-class |
ca0da0da | 24 | class ChargingStationWorker<Data extends ChargingStationWorkerData> { |
66a7748d | 25 | constructor () { |
6b57eb9a | 26 | // Add message listener to create and start charging station from the main thread |
d769e04a | 27 | parentPort?.on('message', (message: WorkerMessage<Data>) => { |
f93dda6a JB |
28 | switch (message.event) { |
29 | case WorkerMessageEvents.startWorkerElement: | |
30 | try { | |
66a7748d | 31 | startChargingStation(message.data) |
f93dda6a | 32 | parentPort?.postMessage({ |
66a7748d JB |
33 | event: WorkerMessageEvents.startedWorkerElement |
34 | }) | |
f93dda6a JB |
35 | } catch (error) { |
36 | parentPort?.postMessage({ | |
37 | event: WorkerMessageEvents.startWorkerElementError, | |
38 | data: { | |
7cd4006c | 39 | name: (error as Error).name, |
f93dda6a | 40 | message: (error as Error).message, |
66a7748d JB |
41 | stack: (error as Error).stack |
42 | } | |
43 | }) | |
f93dda6a | 44 | } |
66a7748d | 45 | break |
f93dda6a | 46 | default: |
a6080904 | 47 | throw new BaseError( |
f93dda6a JB |
48 | `Unknown worker event: '${message.event}' received with data: '${JSON.stringify( |
49 | message.data, | |
4ed03b6e | 50 | undefined, |
66a7748d JB |
51 | 2 |
52 | )}'` | |
53 | ) | |
6b57eb9a | 54 | } |
66a7748d | 55 | }) |
6b57eb9a | 56 | } |
6b57eb9a JB |
57 | } |
58 | ||
d769e04a | 59 | export let chargingStationWorker: |
66a7748d JB |
60 | | ChargingStationWorker<ChargingStationWorkerData> |
61 | | ThreadWorker<ChargingStationWorkerData> | |
aa7d6d95 | 62 | if (Configuration.workerPoolInUse()) { |
66a7748d | 63 | chargingStationWorker = new ThreadWorker<ChargingStationWorkerData>(startChargingStation) |
74bbc59b | 64 | } else { |
66a7748d | 65 | chargingStationWorker = new ChargingStationWorker<ChargingStationWorkerData>() |
3d2ff9e4 | 66 | } |