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 JB |
8 | |
9 | if (!isMainThread) { | |
6013bc53 | 10 | // Add listener to start charging station from main thread |
3d2ff9e4 | 11 | addListener(); |
6013bc53 JB |
12 | if (!Utils.isUndefined(workerData)) { |
13 | startChargingStation({ index: workerData.index as number, templateFile: workerData.templateFile as string }); | |
14 | } | |
3d2ff9e4 J |
15 | } |
16 | ||
17 | function addListener() { | |
3e1416d8 JB |
18 | parentPort.on('message', (message) => { |
19 | if (message.id === WorkerEvents.START_WORKER_ELEMENT) { | |
20 | startChargingStation(message.workerData); | |
3d2ff9e4 J |
21 | } |
22 | }); | |
23 | } | |
24 | ||
46eb543c | 25 | function startChargingStation(data: StationWorkerData) { |
a4624c96 | 26 | const station = new ChargingStation(data.index , data.templateFile); |
3d2ff9e4 | 27 | station.start(); |
7dde0b73 | 28 | } |
a4624c96 JB |
29 | |
30 | export default new ThreadWorker(startChargingStation, { maxInactiveTime: Constants.WORKER_POOL_MAX_INACTIVE_TIME, async: false }); |