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';
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();
public async stop(): Promise<void> {
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();
});
}
+ private gracefulShutdown = async (): Promise<void> => {
+ 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 |');
};
this.chargingStations.clear();
}
+ public async sendBroadcastChannelRequest(
+ id: string,
+ procedureName: ProcedureName,
+ requestPayload: RequestPayload
+ ): Promise<void> {
+ for (const uiService of this.uiServices.values()) {
+ await uiService.requestHandler(this.buildProtocolRequest(id, procedureName, requestPayload));
+ }
+ }
+
protected startHttpServer(): void {
if (this.httpServer.listening === false) {
this.httpServer.listen(this.uiServerConfiguration.options);
responsePayload = await this.requestHandlers.get(command)(messageId, command, requestPayload);
} catch (error) {
// Log
- logger.error(`${this.logPrefix(moduleName, 'messageHandler')} Handle request error:`, error);
+ logger.error(`${this.logPrefix(moduleName, 'requestHandler')} Handle request error:`, error);
responsePayload = {
hashIds: requestPayload?.hashIds,
status: ResponseStatus.FAILURE,
}
public static defaultExitHandler = (code: number): void => {
- if (code !== 0) {
- console.error(chalk.red(`Worker exited with error exit code: ${code.toString()}`));
+ if (code === 0) {
+ console.info(chalk.green('Worker exited successfully'));
+ } else if (code === 1) {
+ console.info(chalk.green('Worker terminated successfully'));
+ } else if (code > 1) {
+ console.error(chalk.red(`Worker exited with exit code: ${code.toString()}`));
}
};