"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const error_tracking_1 = require("./error-tracking");
const util_1 = require("./util");
const shutdownHandlers = [];
function registerShutdownHandler() {
    process.on('SIGTERM', shutdown);
    process.on('SIGINT', shutdown);
}
exports.registerShutdownHandler = registerShutdownHandler;
function addShutdownHandler(handler) {
    shutdownHandlers.push(handler);
}
exports.addShutdownHandler = addShutdownHandler;
async function shutdown() {
    console.log('Shutting down...');
    const shutdownPromises = Promise.all(shutdownHandlers.map(async (handler) => {
        try {
            await handler();
        }
        catch (e) {
            error_tracking_1.reportError(e);
        }
    }));
    await Promise.race([
        shutdownPromises,
        util_1.delay(2000) // After 2 seconds, we just close anyway, we're done.
    ]);
    process.exit(0);
}
//# sourceMappingURL=shutdown.js.map