"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Start = void 0;
const constants_1 = require("@n8n/constants");
const db_1 = require("@n8n/db");
const decorators_1 = require("@n8n/decorators");
const di_1 = require("@n8n/di");
const core_1 = require("@n8n/n8n-nodes-langchain/mcp/core");
const fast_glob_1 = __importDefault(require("fast-glob"));
const fs_1 = require("fs");
const promises_1 = require("fs/promises");
const n8n_workflow_1 = require("n8n-workflow");
const path_1 = __importDefault(require("path"));
const replacestream_1 = __importDefault(require("replacestream"));
const promises_2 = require("stream/promises");
const zod_1 = require("zod");
const active_executions_1 = require("../active-executions");
const active_workflow_manager_1 = require("../active-workflow-manager");
const config_1 = __importDefault(require("../config"));
const constants_2 = require("../constants");
const feature_not_licensed_error_1 = require("../errors/feature-not-licensed.error");
const message_event_bus_1 = require("../eventbus/message-event-bus/message-event-bus");
const event_service_1 = require("../events/event.service");
const execution_service_1 = require("../executions/execution.service");
const multi_main_setup_ee_1 = require("../scaling/multi-main-setup.ee");
const publisher_service_1 = require("../scaling/pubsub/publisher.service");
const pubsub_registry_1 = require("../scaling/pubsub/pubsub.registry");
const subscriber_service_1 = require("../scaling/pubsub/subscriber.service");
const server_1 = require("../server");
const ownership_service_1 = require("../services/ownership.service");
const executions_pruning_service_1 = require("../services/pruning/executions-pruning.service");
const url_service_1 = require("../services/url.service");
const wait_tracker_1 = require("../wait-tracker");
const workflow_runner_1 = require("../workflow-runner");
const base_command_1 = require("./base-command");
const credentials_overwrites_1 = require("../credentials-overwrites");
const deprecation_service_1 = require("../deprecation/deprecation.service");
const load_nodes_and_credentials_1 = require("../load-nodes-and-credentials");
const workflow_history_compaction_service_1 = require("../services/pruning/workflow-history-compaction.service");
const open = require('open');
const flagsSchema = zod_1.z.object({
    open: zod_1.z.boolean().alias('o').describe('opens the UI automatically in browser').optional(),
});
let Start = class Start extends base_command_1.BaseCommand {
    constructor() {
        super(...arguments);
        this.server = di_1.Container.get(server_1.Server);
        this.needsCommunityPackages = true;
        this.needsTaskRunner = true;
        this.getEditorUrl = () => di_1.Container.get(url_service_1.UrlService).getInstanceBaseUrl();
    }
    openBrowser() {
        const editorUrl = this.getEditorUrl();
        open(editorUrl, { wait: true }).catch(() => {
            this.logger.info(`\nWas not able to open URL in browser. Please open manually by visiting:\n${editorUrl}\n`);
        });
    }
    async stopProcess() {
        this.logger.info('\nStopping n8n...');
        try {
            this.activeWorkflowManager.removeAllQueuedWorkflowActivations();
            di_1.Container.get(wait_tracker_1.WaitTracker).stopTracking();
            await this.externalHooks?.run('n8n.stop');
            await this.activeWorkflowManager.removeAllTriggerAndPollerBasedWorkflows();
            if (this.instanceSettings.isMultiMain) {
                await di_1.Container.get(multi_main_setup_ee_1.MultiMainSetup).shutdown();
            }
            if (this.globalConfig.executions.mode === 'queue') {
                di_1.Container.get(publisher_service_1.Publisher).shutdown();
                di_1.Container.get(subscriber_service_1.Subscriber).shutdown();
            }
            di_1.Container.get(event_service_1.EventService).emit('instance-stopped');
            await di_1.Container.get(active_executions_1.ActiveExecutions).shutdown();
            await di_1.Container.get(message_event_bus_1.MessageEventBus).close();
        }
        catch (error) {
            await this.exitWithCrash('There was an error shutting down n8n.', error);
        }
        await this.exitSuccessFully();
    }
    generateConfigTags() {
        const frontendSentryConfig = JSON.stringify({
            dsn: this.globalConfig.sentry.frontendDsn,
            environment: process.env.ENVIRONMENT || 'development',
            serverName: process.env.DEPLOYMENT_NAME,
            release: `n8n@${constants_2.N8N_VERSION}`,
        });
        const b64Encode = (value) => Buffer.from(value).toString('base64');
        const restEndpointEncoded = b64Encode(this.globalConfig.endpoints.rest);
        const sentryConfigEncoded = b64Encode(frontendSentryConfig);
        const configMetaTags = [
            `<meta name="n8n:config:rest-endpoint" content="${restEndpointEncoded}">`,
            `<meta name="n8n:config:sentry" content="${sentryConfigEncoded}">`,
        ].join('');
        return configMetaTags;
    }
    async generateStaticAssets() {
        const n8nPath = this.globalConfig.path;
        const hooksUrls = this.globalConfig.externalFrontendHooksUrls;
        let scriptsString = '';
        if (hooksUrls) {
            scriptsString = hooksUrls.split(';').reduce((acc, curr) => {
                return `${acc}<script src="${curr}"></script>`;
            }, '');
        }
        const closingTitleTag = '</title>';
        const { staticCacheDir } = this.instanceSettings;
        const compileFile = async (fileName) => {
            const filePath = path_1.default.join(constants_2.EDITOR_UI_DIST_DIR, fileName);
            if (/(index\.html)|.*\.(js|css)/.test(filePath) && (0, fs_1.existsSync)(filePath)) {
                const destFile = path_1.default.join(staticCacheDir, fileName);
                await (0, promises_1.mkdir)(path_1.default.dirname(destFile), { recursive: true });
                const streams = [
                    (0, fs_1.createReadStream)(filePath, 'utf-8'),
                    (0, replacestream_1.default)('%CONFIG_TAGS%', this.generateConfigTags(), { ignoreCase: false }),
                    (0, replacestream_1.default)('/{{BASE_PATH}}/', n8nPath, { ignoreCase: false }),
                    (0, replacestream_1.default)('/%7B%7BBASE_PATH%7D%7D/', n8nPath, { ignoreCase: false }),
                    (0, replacestream_1.default)('/%257B%257BBASE_PATH%257D%257D/', n8nPath, { ignoreCase: false }),
                ];
                if (filePath.endsWith('index.html')) {
                    streams.push((0, replacestream_1.default)('{{REST_ENDPOINT}}', this.globalConfig.endpoints.rest, {
                        ignoreCase: false,
                    }), (0, replacestream_1.default)(closingTitleTag, closingTitleTag + scriptsString, {
                        ignoreCase: false,
                    }));
                }
                streams.push((0, fs_1.createWriteStream)(destFile, 'utf-8'));
                return await (0, promises_2.pipeline)(streams);
            }
        };
        const files = await (0, fast_glob_1.default)('**/*.{css,js}', { cwd: constants_2.EDITOR_UI_DIST_DIR });
        await Promise.all([compileFile('index.html'), ...files.map(compileFile)]);
    }
    async init() {
        await this.initCrashJournal();
        this.logger.info('Initializing n8n process');
        if (this.globalConfig.executions.mode === 'queue') {
            const scopedLogger = this.logger.scoped('scaling');
            scopedLogger.debug('Starting main instance in scaling mode');
            scopedLogger.debug(`Host ID: ${this.instanceSettings.hostId}`);
            if (process.env.OFFLOAD_MANUAL_EXECUTIONS_TO_WORKERS === 'true') {
                this.needsTaskRunner = false;
            }
        }
        await super.init();
        di_1.Container.get(deprecation_service_1.DeprecationService).warn();
        this.activeWorkflowManager = di_1.Container.get(active_workflow_manager_1.ActiveWorkflowManager);
        const isMultiMainEnabled = this.globalConfig.executions.mode === 'queue' && this.globalConfig.multiMainSetup.enabled;
        this.instanceSettings.setMultiMainEnabled(isMultiMainEnabled);
        if (isMultiMainEnabled)
            this.instanceSettings.setMultiMainLicensed(true);
        if (this.globalConfig.executions.mode === 'regular') {
            this.instanceSettings.markAsLeader();
        }
        else {
            await this.initOrchestration();
        }
        await this.initLicense();
        if (isMultiMainEnabled) {
            await this.ensureMultiMainLicensed();
        }
        await this.initCommunityPackages();
        if (this.instanceSettings.instanceType === 'main') {
            await di_1.Container.get(db_1.AuthRolesService).init();
            this.logger.debug('Auth roles service init complete');
            await this.initInstanceSettingsLoader();
            this.logger.debug('Instance settings loader init complete');
        }
        di_1.Container.get(wait_tracker_1.WaitTracker).init();
        this.logger.debug('Wait tracker init complete');
        await di_1.Container.get(credentials_overwrites_1.CredentialsOverwrites).init();
        this.logger.debug('Credentials overwrites init complete');
        await this.initBinaryDataService();
        this.logger.debug('Binary data service init complete');
        await this.initDataDeduplicationService();
        this.logger.debug('Data deduplication service init complete');
        await this.initExternalHooks();
        this.logger.debug('External hooks init complete');
        this.initWorkflowHistory();
        this.logger.debug('Workflow history init complete');
        if (!isMultiMainEnabled) {
            await this.cleanupTestRunner();
            this.logger.debug('Test runner cleanup complete');
        }
        if (!this.globalConfig.endpoints.disableUi) {
            await this.generateStaticAssets();
        }
        await this.moduleRegistry.initModules(this.instanceSettings.instanceType);
        const { AuthHandlerRegistry } = await Promise.resolve().then(() => __importStar(require('../auth/auth-handler.registry')));
        await di_1.Container.get(AuthHandlerRegistry).init();
        if (this.instanceSettings.isMultiMain) {
            if (this.globalConfig.endpoints.metrics.enable) {
                const { PrometheusMetricsService } = await Promise.resolve().then(() => __importStar(require('../metrics/prometheus-metrics.service')));
                di_1.Container.get(PrometheusMetricsService);
            }
            di_1.Container.get(multi_main_setup_ee_1.MultiMainSetup).registerEventHandlers();
        }
        await this.executionContextHookRegistry.init();
        await di_1.Container.get(load_nodes_and_credentials_1.LoadNodesAndCredentials).postProcessLoaders();
    }
    async initInstanceSettingsLoader() {
        const { InstanceSettingsLoaderService } = await Promise.resolve().then(() => __importStar(require('../instance-settings-loader/instance-settings-loader.service')));
        await di_1.Container.get(InstanceSettingsLoaderService).init();
    }
    async initOrchestration() {
        di_1.Container.get(publisher_service_1.Publisher);
        di_1.Container.get(pubsub_registry_1.PubSubRegistry).init();
        const subscriber = di_1.Container.get(subscriber_service_1.Subscriber);
        await subscriber.subscribe(subscriber.getCommandChannel());
        await subscriber.subscribe(subscriber.getWorkerResponseChannel());
        await subscriber.subscribe(subscriber.getMcpRelayChannel());
        subscriber.setMcpRelayHandler((msg) => {
            try {
                const mcpServer = core_1.McpServer.instance(this.logger);
                mcpServer.handleWorkerResponse(msg.sessionId, msg.messageId, msg.response);
            }
            catch (error) {
                this.logger.error('Failed to handle MCP relay message', {
                    sessionId: msg.sessionId,
                    messageId: msg.messageId,
                    error: error instanceof Error ? error.message : String(error),
                });
            }
        });
        if (this.instanceSettings.isMultiMain) {
            await di_1.Container.get(multi_main_setup_ee_1.MultiMainSetup).init();
        }
        else {
            this.instanceSettings.markAsLeader();
        }
    }
    async ensureMultiMainLicensed() {
        if (this.license.isMultiMainLicensed())
            return;
        if (!this.instanceSettings.isLeader) {
            const maxRetries = 5;
            for (let attempt = 1; attempt <= maxRetries; attempt++) {
                const delayMs = 2 ** attempt * 1000;
                this.logger.warn(`Instance not licensed for multi-main — retrying license check in ${delayMs / 1000}s (attempt ${attempt}/${maxRetries})`);
                await (0, n8n_workflow_1.sleep)(delayMs);
                await this.license.reload();
                if (this.license.isMultiMainLicensed())
                    return;
            }
        }
        throw new feature_not_licensed_error_1.FeatureNotLicensedError(constants_1.LICENSE_FEATURES.MULTIPLE_MAIN_INSTANCES);
    }
    async run() {
        const { flags } = this;
        const databaseSettings = await di_1.Container.get(db_1.SettingsRepository).findBy({
            loadOnStartup: true,
        });
        databaseSettings.forEach((setting) => {
            config_1.default.set(setting.key, (0, n8n_workflow_1.jsonParse)(setting.value, { fallbackValue: setting.value }));
        });
        const { type: dbType } = this.globalConfig.database;
        if (dbType === 'sqlite') {
            const shouldRunVacuum = this.globalConfig.database.sqlite.executeVacuumOnStartup;
            if (shouldRunVacuum) {
                await di_1.Container.get(db_1.ExecutionRepository).query('VACUUM;');
            }
        }
        await this.server.start();
        di_1.Container.get(executions_pruning_service_1.ExecutionsPruningService).init();
        di_1.Container.get(workflow_history_compaction_service_1.WorkflowHistoryCompactionService).init();
        if (this.globalConfig.executions.mode === 'regular') {
            await this.runEnqueuedExecutions();
        }
        await this.activeWorkflowManager.init();
        di_1.Container.get(load_nodes_and_credentials_1.LoadNodesAndCredentials).releaseTypes();
        const editorUrl = this.getEditorUrl();
        this.log(`\nEditor is now accessible via:\n${editorUrl}`);
        if (Boolean(process.stdout.isTTY) && process.stdin.setRawMode) {
            process.stdin.setRawMode(true);
            process.stdin.resume();
            process.stdin.setEncoding('utf8');
            if (flags.open) {
                this.openBrowser();
            }
            this.log('\nPress "o" to open in Browser.');
            process.stdin.on('data', (key) => {
                if (key === 'o') {
                    this.openBrowser();
                }
                else if (key.charCodeAt(0) === 3) {
                    void this.onTerminationSignal('SIGINT')();
                }
                else {
                    if (key.charCodeAt(0) === 13) {
                        process.stdout.write('\n');
                    }
                    else {
                        process.stdout.write(key);
                    }
                }
            });
        }
    }
    async catch(error) {
        if (error.stack)
            this.logger.error(error.stack);
        await this.exitWithCrash('Exiting due to an error.', error);
    }
    async runEnqueuedExecutions() {
        const executions = await di_1.Container.get(execution_service_1.ExecutionService).findAllEnqueuedExecutions();
        if (executions.length === 0)
            return;
        this.logger.debug('[Startup] Found enqueued executions to run', {
            executionIds: executions.map((e) => e.id),
        });
        const ownershipService = di_1.Container.get(ownership_service_1.OwnershipService);
        const workflowRunner = di_1.Container.get(workflow_runner_1.WorkflowRunner);
        for (const execution of executions) {
            const project = await ownershipService.getWorkflowProjectCached(execution.workflowId);
            const data = {
                executionMode: execution.mode,
                executionData: execution.data,
                workflowData: execution.workflowData,
                projectId: project.id,
            };
            di_1.Container.get(event_service_1.EventService).emit('execution-started-during-bootup', {
                executionId: execution.id,
            });
            void workflowRunner.run(data, undefined, false, execution.id);
        }
    }
};
exports.Start = Start;
exports.Start = Start = __decorate([
    (0, decorators_1.Command)({
        name: 'start',
        description: 'Starts n8n. Makes Web-UI available and starts active workflows',
        examples: ['', '-o'],
        flagsSchema,
    })
], Start);
//# sourceMappingURL=start.js.map