UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

127 lines (126 loc) • 5.83 kB
import { n as GATEWAY_SERVICE_STOP_TIMEOUT_MS } from "./gateway-shutdown-budget-cDAYhBVI.mjs"; import { t as sleep } from "./sleep-CkI3nkiJ.mjs"; import "./gateway-state-owner-Bnv5owvG.mjs"; import { t as GatewayStateOwnerContentionError } from "./gateway-state-owner-errors-DI3mhN4z.mjs"; import { i as hasCommandProcessCleanupError } from "./exec-result-DJMhztYN.mjs"; import { r as DEFAULT_UPDATE_STEP_TIMEOUT_MS } from "./update-run-timeouts-Byb-PlTk.mjs"; import { t as acquireWithWait } from "./acquire-with-wait--M6IyZwQ.mjs"; import { o as TICK_INTERVAL_MS } from "./server-constants-CYohpfLc.mjs"; import { t as resolveGatewayRestartDeferralTimeoutMs } from "./restart-budget-Cn-1gpwB.mjs"; import { n as GatewayLockError, r as acquireGatewayLock } from "./gateway-lock-IYOwJQai.mjs"; import { n as resolveDoctorRepairMode, t as isDoctorUpdateRepairMode } from "./doctor-repair-mode-D2HKy2yZ.mjs"; //#region src/commands/doctor-maintenance-foreground.ts const DOCTOR_WORK_RESERVE_FLOOR_MS = 6e5; const DOCTOR_MAINTENANCE_WAIT_PROGRESS_INTERVAL_MS = 3e4; /** * The time, on this process's `performance.now()` clock, when update Doctor stops * waiting for other OpenClaw processes. Published drivers bound activation Doctor * only by an operator `--timeout`, counted from its spawn and not passed on, so * Doctor stays within the default step allowance from its own start. It keeps * twice this update's rehearsal Doctor time, and at least the floor, for its own * work. Every wait in one Doctor process ends at this deadline, so earlier waits * shorten later ones. */ async function resolveDoctorMaintenanceWaitDeadlineMs(e) { let t = 0; try { const { findActiveUpdateRun: n } = await import("./update-run-reader-g1w6betd.mjs"); const r = n({ env: e })?.steps.findLast((e) => e.step === "candidate-doctor"); if (r?.startedAtMs !== void 0 && r.endedAtMs !== void 0) t = Math.max(0, r.endedAtMs - r.startedAtMs); } catch (e) { if (hasCommandProcessCleanupError(e)) throw e; } return DEFAULT_UPDATE_STEP_TIMEOUT_MS - Math.max(DOCTOR_WORK_RESERVE_FLOOR_MS, 2 * t); } function formatDoctorWaitSeconds(e) { return `${Math.max(0, Math.round(e / 1e3))} s`; } function describeHolder(e) { const { holder: t } = e; return `the OpenClaw ${t?.role ? `${t.role} ` : ""}process ${t?.pid ?? "(unknown PID)"}`; } /** * Another process with a recorded non-Gateway role finishes on its own. This * process's own owner cannot release while Doctor waits on it, and an unknown or * Gateway holder keeps the existing serving-owner decisions. */ function isTransientHolder(e) { const t = e instanceof GatewayStateOwnerContentionError ? e.holder : void 0; return t?.role !== void 0 && t.role !== "gateway" && t.pid !== void 0 && t.pid !== process.pid; } async function acquireDoctorGatewayMaintenanceOwner(e, t, n) { const r = isDoctorUpdateRepairMode(resolveDoctorRepairMode(n.options, t)) || n.waitForMaintenance === true; const i = n.assertCurrent || n.assertAdmission ? () => { n.assertCurrent?.(); n.assertAdmission?.(); } : void 0; const a = performance.now(); const o = Math.min(n.deadlineMs ?? Infinity, a + TICK_INTERVAL_MS + resolveGatewayRestartDeferralTimeoutMs() + GATEWAY_SERVICE_STOP_TIMEOUT_MS); let s; let c; let l; let u = 0; let d = 100; const pollUntil = (e, t) => { const n = Math.min(d, e - t); d = Math.min(d * 2, 1e3); return { delayMs: n }; }; const acquire = async () => { try { const r = await acquireGatewayLock({ env: t, role: "sqlite-maintenance", allowInTests: true, lifecycleDeadlineMs: n.deadlineMs, assertCurrent: i, onWait: n.runtime.log }); if (!r) throw new Error(`Doctor could not acquire maintenance ownership for ${e}`); return r; } catch (e) { if (e instanceof GatewayLockError && e.cause instanceof GatewayStateOwnerContentionError) { if (r && isTransientHolder(e.cause)) s ??= await resolveDoctorMaintenanceWaitDeadlineMs(t); throw e.cause; } throw e; } }; try { const e = await acquireWithWait({ acquire, shouldRetry: (e) => { if (!r || !(e instanceof GatewayStateOwnerContentionError)) return false; n.signal?.throwIfAborted(); i?.(); const t = performance.now(); if (s !== void 0 && isTransientHolder(e)) { l = e; if (t >= s) return false; if (c !== "transient" || t >= u) { n.runtime.log(c === "transient" ? `Still waiting for ${describeHolder(e)} to release ${e.databasePath} (${formatDoctorWaitSeconds(t - a)} of ${formatDoctorWaitSeconds(s - a)}).` : `Waiting up to ${formatDoctorWaitSeconds(s - t)} for ${describeHolder(e)} to release ${e.databasePath} before Doctor maintenance. Let it finish, or stop it to continue sooner.`); u = t + DOCTOR_MAINTENANCE_WAIT_PROGRESS_INTERVAL_MS; } c = "transient"; return pollUntil(s, t); } if (!n.assertCurrent || t >= o) return false; if (c !== "gateway") { c = "gateway"; n.runtime.log("Waiting for the previous foreground Gateway to release state."); } return pollUntil(o, t); }, deadlineMs: Infinity, pollIntervalMs: 100, sleep: (e) => sleep(e, n.signal) }); if (c === "transient") n.runtime.log(`State ownership released after ${formatDoctorWaitSeconds(performance.now() - a)}; continuing Doctor maintenance.`); return e; } catch (e) { if (e === l && e instanceof GatewayStateOwnerContentionError) throw new GatewayStateOwnerContentionError(e.databasePath, e, `Doctor waited ${formatDoctorWaitSeconds(performance.now() - a)} for ${describeHolder(e)} to finish; ${e.holderDetail ?? "it is still running."}`, e.holder); throw e; } } //#endregion export { resolveDoctorMaintenanceWaitDeadlineMs as i, acquireDoctorGatewayMaintenanceOwner as n, formatDoctorWaitSeconds as r, DOCTOR_MAINTENANCE_WAIT_PROGRESS_INTERVAL_MS as t };