"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PruningWorker = void 0;
const cluster_1 = __importDefault(require("cluster"));
require("source-map-support/register");
const logger_1 = __importDefault(require("../logger"));
const event_1 = require("../services/event");
const pruning_1 = require("../services/pruning");
const storage_1 = require("../services/storage");
require("../utils/polyfills");
const services = [];
const PruningWorker = async () => {
    process.on('unhandledRejection', (error) => {
        console.error('Unhandled Rejection at:', error.stack || error);
        stop();
    });
    process.on('SIGTERM', stop);
    process.on('SIGINT', stop);
    services.push(storage_1.Storage, event_1.Event, pruning_1.Pruning);
    for (const service of services) {
        await service.start();
    }
};
exports.PruningWorker = PruningWorker;
let stopping = false;
const stop = async () => {
    if (stopping) {
        logger_1.default.error('Force stopping Pruning Worker');
        process.exit(1);
    }
    stopping = true;
    setTimeout(() => {
        logger_1.default.error('Pruning Worker did not shut down gracefully after 30 seconds, exiting');
        process.exit(1);
    }, 30 * 1000).unref();
    logger_1.default.info(`Shutting down pruning ${process.pid}`);
    for (const service of services.reverse()) {
        await service.stop();
    }
    if (!cluster_1.default.isPrimary) {
        process.removeAllListeners();
    }
};
if (require.main === module) {
    (0, exports.PruningWorker)();
}
//# sourceMappingURL=pruning.js.map