X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcharging-station%2FBootstrap.ts;h=4a6bd75c2d7a2ebbce2e6ebb24c3ceab9019dba6;hb=6bd808fd1dc554c8d55521b20a064447835ef04c;hp=3312224278521fe7e98fc128b58c2ad5bc887fb6;hpb=b63b4a73f7869a2ff31e5bd259d056faaa53a5de;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 33122242..4a6bd75c 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -17,10 +17,11 @@ import { type ChargingStationWorkerMessage, type ChargingStationWorkerMessageData, ChargingStationWorkerMessageEvents, + ProcedureName, type StationTemplateUrl, type Statistics, } from '../types'; -import { Configuration, ErrorUtils, Utils, logger } from '../utils'; +import { Configuration, Constants, ErrorUtils, Utils, logger } from '../utils'; import { type MessageHandler, type WorkerAbstract, WorkerFactory } from '../worker'; const moduleName = 'Bootstrap'; @@ -44,6 +45,11 @@ export class Bootstrap { private readonly workerScript: string; private constructor() { + for (const signal of ['SIGINT', 'SIGQUIT', 'SIGTERM']) { + process.on(signal, () => { + this.gracefulShutdown().catch(Constants.EMPTY_FUNCTION); + }); + } // Enable unconditionally for now ErrorUtils.handleUnhandledRejection(); ErrorUtils.handleUncaughtException(); @@ -123,6 +129,11 @@ export class Bootstrap { public async stop(): Promise { if (isMainThread && this.started === true) { + await this.uiServer?.sendBroadcastChannelRequest( + Utils.generateUUID(), + ProcedureName.STOP_CHARGING_STATION, + Constants.EMPTY_FREEZED_OBJECT + ); await this.workerImplementation?.stop(); this.workerImplementation = null; this.uiServer?.stop(); @@ -271,6 +282,16 @@ export class Bootstrap { }); } + private gracefulShutdown = async (): Promise => { + console.info(`${chalk.green('Graceful shutdown')}`); + try { + await this.stop(); + process.exit(0); + } catch (error) { + process.exit(1); + } + }; + private logPrefix = (): string => { return Utils.logPrefix(' Bootstrap |'); };