"use strict";
/**
 * Supervisord lifecycle helpers for Docker deployments (`ccs docker up`).
 *
 * In Docker, supervisord owns the CLIProxy process lifecycle. Direct
 * stop+start via session-tracker / service-manager creates orphaned
 * processes and causes supervisord to enter FATAL state (EADDRINUSE).
 * All restart operations must delegate to `supervisorctl` instead.
 */
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 __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.restartCliproxyViaSupervisord = exports.isRunningUnderSupervisord = void 0;
const fs = __importStar(require("fs"));
const child_process_1 = require("child_process");
const port_manager_1 = require("../cliproxy/config/port-manager");
const SUPERVISOR_SOCK = '/var/run/supervisor.sock';
const SUPERVISOR_CONF = '/etc/supervisord.conf';
/** True when running inside a supervisord-managed container. */
function isRunningUnderSupervisord() {
    return fs.existsSync(SUPERVISOR_SOCK);
}
exports.isRunningUnderSupervisord = isRunningUnderSupervisord;
/** Restart the cliproxy program via supervisorctl. Returns port on success. */
function restartCliproxyViaSupervisord() {
    try {
        (0, child_process_1.execSync)(`supervisorctl -c ${SUPERVISOR_CONF} restart cliproxy`, { timeout: 15000 });
        return { success: true, port: port_manager_1.CLIPROXY_DEFAULT_PORT };
    }
    catch (err) {
        const detail = err instanceof Error ? err.message : String(err);
        console.error(`[cliproxy] supervisorctl restart failed: ${detail}`);
        return { success: false, error: 'supervisorctl restart failed' };
    }
}
exports.restartCliproxyViaSupervisord = restartCliproxyViaSupervisord;
//# sourceMappingURL=supervisord-lifecycle.js.map