"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.installWorkerComponents = installWorkerComponents;
const tslib_1 = require("tslib");
const app_1 = require("@deepkit/app");
const event_1 = require("@deepkit/event");
const framework_1 = require("@deepkit/framework");
const ioredis_1 = require("ioredis");
const resolver_1 = require("../../app/resolver");
const const_1 = require("../../app/const");
const state_1 = require("../../app/state");
const cli_1 = require("./cli");
const entity_1 = require("./entity");
const queue_1 = require("./queue");
const runner_1 = require("./runner");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function installWorkerComponents(app) {
    state_1.globalState.enableWorker = true;
    state_1.globalState.additionalEntities.push(entity_1.JobEntity);
    app.appModule.addProvider(runner_1.WorkerRunnerService);
    app.appModule.addController(cli_1.WorkerStartCommand);
    app.appModule.addController(cli_1.WorkerQueueJobCommand);
    class WorkerListener {
        constructor(runner) {
            this.runner = runner;
        }
        async shutdownRunner() {
            await this.runner.shutdown();
            // prevent new Redis connections from being created after shutdown
            // some issue in dev keeping processes alive forever
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            ioredis_1.Redis.prototype.connect = () => { };
        }
        // we want this to run very late in the process, after the worker has shut down
        async closeQueues() {
            await queue_1.WorkerQueueRegistry.closeQueues();
        }
    }
    WorkerListener.__type = [() => runner_1.WorkerRunnerService, 'runner', 'constructor', 'shutdownRunner', 'closeQueues', 'WorkerListener', 'PP7!2";"0#P"0$P"0%5w&'];
    tslib_1.__decorate([
        event_1.eventDispatcher.listen(framework_1.onServerShutdown),
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", []),
        tslib_1.__metadata("design:returntype", Promise)
    ], WorkerListener.prototype, "shutdownRunner", null);
    tslib_1.__decorate([
        event_1.eventDispatcher.listen(framework_1.onServerShutdown, 1000),
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", []),
        tslib_1.__metadata("design:returntype", Promise)
    ], WorkerListener.prototype, "closeQueues", null);
    app.appModule.addListener(WorkerListener);
    app.listen(framework_1.onServerMainBootstrapDone, () => {
        if (!state_1.globalState.isCliService) {
            setTimeout(() => {
                const config = (0, resolver_1.getAppConfig)();
                if (config.ENABLE_JOB_RUNNER ?? const_1.isDevelopment)
                    app.get(runner_1.WorkerRunnerService).start();
            }, 1000);
        }
    });
}
installWorkerComponents.__type = [() => app_1.App, 'app', 'installWorkerComponents', 'PP"7!2""/#'];
//# sourceMappingURL=bootstrap.js.map