UNPKG

@piggly/fastify-chassis

Version:

An ESM/CommonJS toolkit to help you to do common operations in your back-end applications with Fastify and NodeJS.

49 lines 1.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.processStop = void 0; const tslib_1 = require("tslib"); /* eslint-disable no-console */ const ddd_toolkit_1 = require("@piggly/ddd-toolkit"); const event_bus_1 = tslib_1.__importDefault(require("@piggly/event-bus")); /** * Process stop error. * * It will wait for: * - EventBus cleanup; * - LoggerService flush and cleanup. * * You may not need to cleanup classes above when using this. * * @param {Environment} env * @param {() => Promise<number>} beforeExit * @param {number} exitCode Used when beforeExit is not provided. Default is 0. * @returns The callback function. * @since 5.1.0 * @author Caique Araujo <caique@piggly.com.br> */ const processStop = (env, beforeExit, exitCode = 1) => async () => { try { console.log('⚠️ Command is stopping... Please wait a moment.'); await event_bus_1.default.instance.cleanup(); const logger = ddd_toolkit_1.LoggerService.softResolve(); logger.flush(); await logger.cleanup(); if (beforeExit) { return process.exit(await beforeExit()); } process.exit(0); } catch (err) { if (env.debug === true) { console.error('❌ Command has failed to stop.'); console.error(err); } const logger = ddd_toolkit_1.LoggerService.softResolve(); logger.error('UNCAUGHT_EXCEPTION_ERROR', err); logger.flush(); await logger.cleanup(); process.exit(exitCode); } }; exports.processStop = processStop; //# sourceMappingURL=processStop.js.map