openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
153 lines (152 loc) • 6.19 kB
JavaScript
import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs";
import { a as executeSqliteQueryTakeFirstSync, i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs";
import { l as runOpenClawStateWriteTransaction } from "./openclaw-state-db-ARhwdPJg.mjs";
import { o as writeConfigMachineStateInDatabase } from "./config-machine-state-write-DanSUxEP.mjs";
import { t as GATEWAY_CRASH_STATE_KEY } from "./gateway-crash-D22VnCOp.mjs";
import { a as inspectGatewayCrashLoopBreakerInDatabase, i as buildGatewayCrashLoopBreakerDecision, n as GATEWAY_CRASH_LOOP_RECOVERED_REASON, r as GATEWAY_SIGNAL_REPEAT_WINDOW_MS, s as readGatewayCrashStateInDatabase, t as GATEWAY_CRASH_LOOP_BREAKER_REASON } from "./gateway-boot-lifecycle.kernel-BYOJzAob.mjs";
import { randomUUID } from "node:crypto";
//#region src/infra/gateway-boot-lifecycle.worker.ts
const GATEWAY_BOOT_LIFECYCLE_RETENTION_MS = 864e5;
const gatewayLifecycleLog = createSubsystemLogger("gateway/lifecycle");
const gatewayBootOperations = {
"gatewayBoot.begin": (e, t) => {
const { nowMs: n, metadata: r, previousOwner: i } = e;
let a = buildGatewayCrashLoopBreakerDecision({ uncleanBoots: 0 });
try {
a = inspectGatewayCrashLoopBreakerInDatabase(t.open().db, n);
} catch (e) {
gatewayLifecycleLog.warn(`crash-loop breaker state unavailable; fail-open: ${String(e)}`);
}
const o = a.tripped ? a.shouldWriteStabilityBundle ? GATEWAY_CRASH_LOOP_BREAKER_REASON : void 0 : a.recovered ? GATEWAY_CRASH_LOOP_RECOVERED_REASON : void 0;
const s = randomUUID();
try {
const e = runOpenClawStateWriteTransaction(({ db: e }) => {
const t = getNodeSqliteKysely(e);
const a = t.selectFrom("gateway_boot_lifecycle").select([
"boot_id",
"pid",
"started_at_ms",
"outcome",
"startup_reason",
"reason"
]);
const c = executeSqliteQueryTakeFirstSync(e, a.orderBy("started_at_ms", "desc").limit(1));
let l;
if (c && (c.outcome === null || c.outcome === "forced_stop")) {
let t = c;
while (t.startup_reason === GATEWAY_CRASH_LOOP_RECOVERED_REASON) {
const n = executeSqliteQueryTakeFirstSync(e, a.where("pid", "=", t.pid).where("outcome", "=", "safe_mode_stable").where("completed_at_ms", "=", t.started_at_ms).where("started_at_ms", "<", t.started_at_ms).orderBy("started_at_ms", "desc").limit(1));
if (!n) break;
t = n;
}
l = {
startedAt: t.started_at_ms,
endedAt: n
};
}
const u = readGatewayCrashStateInDatabase(e);
let d;
if (c && (l || c.outcome === "startup_failed")) {
const e = u.active?.bootId === c.boot_id ? u.active : void 0;
const t = i?.pid === c.pid ? i.heartbeatAt : void 0;
d = {
...e,
bootId: c.boot_id,
pid: c.pid,
startedAtMs: l?.startedAt ?? c.started_at_ms,
detectedAtMs: n,
...c.reason ? { reason: c.reason } : {},
lastHeartbeatAtMs: t !== void 0 && t >= (l?.startedAt ?? c.started_at_ms) && t <= n ? t : void 0
};
u.recent = [d, ...u.recent].slice(0, 10);
}
u.active = {
...r,
bootId: s
};
writeConfigMachineStateInDatabase(e, GATEWAY_CRASH_STATE_KEY, u);
executeSqliteQuerySync(e, t.deleteFrom("gateway_boot_lifecycle").where("started_at_ms", "<", n - GATEWAY_BOOT_LIFECYCLE_RETENTION_MS));
executeSqliteQuerySync(e, t.insertInto("gateway_boot_lifecycle").values({
boot_id: s,
pid: process.pid,
started_at_ms: n,
completed_at_ms: null,
outcome: null,
startup_reason: o ?? null,
reason: null
}));
return {
previousCrash: d,
crashes: u.recent
};
}, t.stateOptions());
return {
decision: a,
id: s,
...e
};
} catch (e) {
gatewayLifecycleLog.warn(`failed to persist gateway boot start; fail-open: ${String(e)}`);
return {
decision: a,
id: void 0,
crashes: void 0,
previousCrash: void 0
};
}
},
"gatewayBoot.enrichCrash": (e, t) => runOpenClawStateWriteTransaction(({ db: t }) => {
const n = readGatewayCrashStateInDatabase(t);
const r = n.recent.find((t) => t.bootId === e.bootId);
if (r) {
Object.assign(r, e.evidence);
writeConfigMachineStateInDatabase(t, GATEWAY_CRASH_STATE_KEY, n);
}
return {
crash: r,
crashes: n.recent
};
}, t.stateOptions()),
"gatewayBoot.complete": (e, t) => runOpenClawStateWriteTransaction(({ db: t }) => {
const { bootId: n, completion: r, nowMs: i } = e;
const a = getNodeSqliteKysely(t);
executeSqliteQuerySync(t, a.updateTable("gateway_boot_lifecycle").set({
completed_at_ms: i,
outcome: r.outcome,
...r.startupReason ? { startup_reason: r.startupReason } : {},
reason: r.reason ?? null
}).where("boot_id", "=", n));
return e.countSignalStops ? executeSqliteQueryTakeFirstSync(t, a.selectFrom("gateway_boot_lifecycle").select((e) => e.fn.countAll().as("count")).where("outcome", "=", "clean_stop").where("reason", "=", r.reason ?? null).where("completed_at_ms", ">=", i - GATEWAY_SIGNAL_REPEAT_WINDOW_MS))?.count : void 0;
}, t.stateOptions()),
"gatewayBoot.recover": (e, t) => runOpenClawStateWriteTransaction(({ db: t }) => {
const n = e.nowMs ?? Date.now();
const r = inspectGatewayCrashLoopBreakerInDatabase(t, n);
if (!r.recovered || r.uncleanBoots !== 0) return;
const i = getNodeSqliteKysely(t);
if (e.bootId) {
if (executeSqliteQuerySync(t, i.updateTable("gateway_boot_lifecycle").set({
completed_at_ms: n,
outcome: "safe_mode_stable",
reason: null
}).where("boot_id", "=", e.bootId).where("completed_at_ms", "is", null)).numAffectedRows !== 1n) return;
}
const a = randomUUID();
const o = readGatewayCrashStateInDatabase(t);
if (o.active && o.active.bootId === e.bootId) {
o.active.bootId = a;
writeConfigMachineStateInDatabase(t, GATEWAY_CRASH_STATE_KEY, o);
}
executeSqliteQuerySync(t, i.insertInto("gateway_boot_lifecycle").values({
boot_id: a,
pid: process.pid,
started_at_ms: n,
completed_at_ms: null,
outcome: null,
startup_reason: GATEWAY_CRASH_LOOP_RECOVERED_REASON,
reason: null
}));
return a;
}, t.stateOptions())
};
//#endregion
export { gatewayBootOperations };