@atomist/automation-client
Version:
Atomist API for software low-level client
100 lines • 4.17 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const exitHook = require("async-exit-hook");
const lodash_1 = require("lodash");
const logger_1 = require("../../util/logger");
/** Believe or not, this is the default grace period. */
exports.defaultGracePeriod = 10000;
/**
* Return whether graceful termination is enabled.
*/
function terminationGraceful(cfg) {
return lodash_1.get(cfg, "ws.termination.graceful", false);
}
exports.terminationGraceful = terminationGraceful;
/**
* Return graceful termination period in milliseconds.
*/
function terminationGracePeriod(cfg) {
return lodash_1.get(cfg, "ws.termination.gracePeriod", exports.defaultGracePeriod);
}
exports.terminationGracePeriod = terminationGracePeriod;
let shutdownHooks = [];
/**
* Add callback to run when shutdown is initiated prior to process
* exit. See [[ShutdownHook]] for description of parameters.
*/
function registerShutdownHook(cb, priority = 1000, desc) {
const description = desc || `Shutdown hook with priority ${priority}`;
shutdownHooks = [{ priority, hook: cb, description }, ...shutdownHooks].sort((h1, h2) => h1.priority - h2.priority);
}
exports.registerShutdownHook = registerShutdownHook;
/**
* Run each shutdown hook and collect its result.
*/
function executeShutdownHooks(cb) {
return __awaiter(this, void 0, void 0, function* () {
if (shutdownHooks.length === 0) {
// logger.info("Shutting down");
cb();
throw new Error(`async-exit-hook callback returned but should not have`);
}
logger_1.logger.info("Shutdown initiated, calling shutdown hooks");
let status = 0;
for (const hook of shutdownHooks) {
try {
logger_1.logger.debug(`Calling shutdown hook '${hook.description}'...`);
const result = yield hook.hook();
logger_1.logger.debug(`Shutdown hook '${hook.description}' completed with status '${result}'`);
status += result;
}
catch (e) {
logger_1.logger.warn(`Shutdown hook '${hook.description}' threw an error: ${e.message}`);
status += 10;
}
}
logger_1.logger.info(`Shutdown hooks completed with status '${status}', exiting`);
shutdownHooks = [];
cb();
throw new Error(`async-exit-hook callback returned but should not have`);
});
}
exports.executeShutdownHooks = executeShutdownHooks;
exitHook(executeShutdownHooks);
/**
* Set the absolute longest number of milliseconds shutdown should
* take.
*/
function setForceExitTimeout(ms) {
exitHook.forceExitTimeout(ms);
}
exports.setForceExitTimeout = setForceExitTimeout;
/**
* Register a final shutdown hook that calls `process.exit(code)` and
* then initiates shutdown. This allows you to exit with a specific
* exit code _and_ process all async shutdown hooks, something not
* possible when calling process.exit directly.
*
* For the fastest safe exit, set the automation client configuration
* ws.termination.graceful to false before calling this.
*
* @param code Exit code
*/
function safeExit(code) {
registerShutdownHook(() => __awaiter(this, void 0, void 0, function* () {
process.exit(code);
return 0; // make the compiler happy
}), Number.MAX_VALUE, `safeExit ${code}`);
process.kill(process.pid);
}
exports.safeExit = safeExit;
//# sourceMappingURL=shutdown.js.map