openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.39 kB
JavaScript
import{n as GATEWAY_SERVICE_STOP_TIMEOUT_MS}from"./gateway-shutdown-budget-Utx5eFL-.mjs";import{t as sleep}from"./sleep-B4CgCyZu.mjs";import"./gateway-state-owner-D_klysuh.mjs";import{t as GatewayStateOwnerContentionError}from"./gateway-state-owner-errors-Cke3Bshm.mjs";import{a as hasCommandProcessCleanupError}from"./exec-result-TD4w1Z1i.mjs";import{r as DEFAULT_UPDATE_STEP_TIMEOUT_MS}from"./update-run-timeouts-MQiDlYE0.mjs";import{t as acquireWithWait}from"./acquire-with-wait-C5jtmZa7.mjs";import{a as TICK_INTERVAL_MS}from"./server-constants-BmIepgnr.mjs";import{t as resolveGatewayRestartDeferralTimeoutMs}from"./restart-budget-HbMaGsmG.mjs";import{n as GatewayLockError,r as acquireGatewayLock}from"./gateway-lock-CQoO1usb.mjs";import{n as resolveDoctorRepairMode,t as isDoctorUpdateRepairMode}from"./doctor-repair-mode-Dilz8sGs.mjs";
//!src/commands/doctor-maintenance-foreground.ts
const DOCTOR_WORK_RESERVE_FLOOR_MS=6e5;const DOCTOR_MAINTENANCE_WAIT_PROGRESS_INTERVAL_MS=3e4;async function resolveDoctorMaintenanceWaitDeadlineMs(e){let t=0;try{const{findActiveUpdateRun:n}=await import(`./update-run-reader-tNF5l9at.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)`}`}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}}export{resolveDoctorMaintenanceWaitDeadlineMs as i,acquireDoctorGatewayMaintenanceOwner as n,formatDoctorWaitSeconds as r,DOCTOR_MAINTENANCE_WAIT_PROGRESS_INTERVAL_MS as t};