UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

87 lines (86 loc) • 2.98 kB
import { n as executeExistingOpenClawStateRead } from "./openclaw-state-db-readonly-B6YHb1oF.mjs"; import { n as captureOpenClawStateReadWorkerContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs"; import { l as resolveCronStoreSelectionAsync } from "./paths-CY4cvrR6.mjs"; import { toUSVString } from "node:util"; //#region src/cron/store/job-name.ts const preparedNames = /* @__PURE__ */ new Set(); const releasedNames = new FinalizationRegistry((e) => { preparedNames.delete(e); }); function* retainedNames(e) { for (const t of preparedNames) { const n = t.deref(); if (!n) preparedNames.delete(t); else if (e === void 0 || n.storeKey === void 0 || n.storeKey === e) yield n; } } /** All cron commit notifications invalidate names, including uncertain outcomes and repairs. */ function invalidateCronJobNames(e) { for (const t of retainedNames(e)) { t.revision++; t.names = void 0; } } /** Publish only acknowledged postimages belonging to a resolver's original physical generation. */ function publishCronJobNames(e, t, n) { for (const r of retainedNames(e)) { if (r.storeKey !== e) continue; try { t.admission.assertCurrent(); r.context.admission.assertCurrent(); } catch { continue; } const i = t.admission.identity; const a = r.context.admission.identity; if (i.key !== a.key || i.birthtime !== a.birthtime) continue; r.revision++; r.names = new Map(r.jobIds.map((e) => [e, n.get(e)])); } } async function prepareCronJobNameResolver(e, t) { const n = [...new Set(e.map(toUSVString))]; if (n.length === 0) return () => { throw new Error("Cron job names were not prepared; await prepareCronJobNameResolver"); }; const r = { context: captureOpenClawStateReadWorkerContext(), jobIds: n, revision: 0 }; const i = new WeakRef(r); preparedNames.add(i); releasedNames.register(r, i); const resolveName = (e) => { r.context.admission.assertCurrent(); const t = toUSVString(e); if (!r.names?.has(t)) throw new Error("Cron job names need refresh; await prepareCronJobNameResolver"); return r.names.get(t); }; try { const e = await resolveCronStoreSelectionAsync(t, process.env, r.context); if (e.status === "unresolved") { r.names = new Map(n.map((e) => [e, void 0])); return resolveName; } r.storeKey = e.storeKey; const i = await executeExistingOpenClawStateRead({ path: r.context.admission.databasePath }, { type: "cron.jobNames", jobIds: n, storePath: r.storeKey }, { context: r.context, current: true }); if (i && (!i.ok || i.type !== "cron.jobNames")) throw new Error("Unexpected cron job names result"); r.context.admission.assertCurrent(); r.storeKey = i?.storeKey ?? r.storeKey; if (r.revision === 0) r.names = new Map(n.map((e) => [e, i?.names.get(e)])); return resolveName; } catch (e) { preparedNames.delete(i); throw e; } } //#endregion export { prepareCronJobNameResolver as n, publishCronJobNames as r, invalidateCronJobNames as t };