UNPKG

@atomist/automation-client

Version:
31 lines 1.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const exitHook = require("async-exit-hook"); const logger_1 = require("./logger"); let shutdownHooks = []; function registerShutdownHook(cb, priority = Number.MAX_VALUE) { shutdownHooks = [{ priority, hook: cb }, ...shutdownHooks].sort((h1, h2) => h1.priority - h2.priority); } exports.registerShutdownHook = registerShutdownHook; exitHook.forceExitTimeout(60000 * 2); exitHook(callback => { // Exit early if no shutdown hooks got registered if (shutdownHooks.length === 0) { process.exit(0); } logger_1.logger.info("Shutdown initiated. Calling shutdown hooks"); shutdownHooks .map(h => h.hook) .reduce((p, c, i, result) => p.then(c), Promise.resolve(0)) .then(result => { logger_1.logger.info("Shutdown hooks completed. Exiting..."); callback(); process.exit(result); }) .catch(() => { logger_1.logger.info("Shutdown hooks failed. Exiting..."); callback(); process.exit(1); }); }); //# sourceMappingURL=shutdown.js.map