Commit | Line | Data |
---|---|---|
46eb543c | 1 | import { StationWorkerData, WorkerEvents } from '../types/Worker'; |
3d2ff9e4 | 2 | import { isMainThread, parentPort, workerData } from 'worker_threads'; |
3f40bc9c | 3 | |
6af9012e | 4 | import ChargingStation from './ChargingStation'; |
a4624c96 JB |
5 | import Constants from '../utils/Constants'; |
6 | import { ThreadWorker } from 'poolifier'; | |
6013bc53 | 7 | import Utils from '../utils/Utils'; |
7dde0b73 | 8 | |
b8da29bc JB |
9 | // Conditionally export ThreadWorker instance for pool usage |
10 | export let threadWorker; | |
11 | if (Utils.workerPoolInUse()) { | |
b0317ac7 | 12 | threadWorker = new ThreadWorker<StationWorkerData>(startChargingStation, { maxInactiveTime: Constants.WORKER_POOL_MAX_INACTIVE_TIME, async: false }); |
b8da29bc JB |
13 | } |
14 | ||
7dde0b73 | 15 | if (!isMainThread) { |
6013bc53 | 16 | // Add listener to start charging station from main thread |
3d2ff9e4 | 17 | addListener(); |
6013bc53 JB |
18 | if (!Utils.isUndefined(workerData)) { |
19 | startChargingStation({ index: workerData.index as number, templateFile: workerData.templateFile as string }); | |
20 | } | |
3d2ff9e4 J |
21 | } |
22 | ||
abbd09af | 23 | function addListener(): void { |
3e1416d8 JB |
24 | parentPort.on('message', (message) => { |
25 | if (message.id === WorkerEvents.START_WORKER_ELEMENT) { | |
26 | startChargingStation(message.workerData); | |
3d2ff9e4 J |
27 | } |
28 | }); | |
29 | } | |
30 | ||
abbd09af | 31 | function startChargingStation(data: StationWorkerData): void { |
a4624c96 | 32 | const station = new ChargingStation(data.index , data.templateFile); |
3d2ff9e4 | 33 | station.start(); |
7dde0b73 | 34 | } |