openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
84 lines (83 loc) • 3.64 kB
JavaScript
import { p as isCronJobActive } from "./active-jobs-DAhIn24S.mjs";
import { l as parseCronStreamSandboxTarget } from "./stream-schedule-BFX6oe0X.mjs";
import { a as reconcileCronSourceIdentity } from "./source-schedule-DlDKIQwQ.mjs";
import { y as cronSchedulingInputsEqual } from "./row-codec-D4aymQkb.mjs";
import { c as isJobEnabled, n as computeJobNextRunAtMs, s as hasScheduledNextRunAtMs } from "./jobs-scheduling-D3arlYs4.mjs";
import { i as resolveForcePreservedOneShotAtMs } from "./one-shot-schedule-Ff0uCKas.mjs";
import { isDeepStrictEqual } from "node:util";
//#region src/cron/service/jobs-mutation.ts
/** Completes a canonical job edit before authority rebinding and persistence. */
/** Keep the harness-owned immutable envelope intact when copying mutable job fields. */
function cloneCronJobForMutation(e) {
const { runtimeAuthority: t, ...n } = e;
return {
...structuredClone(n),
...t ? { runtimeAuthority: t } : {}
};
}
/** Only private caller options can install a sandbox target; unrelated edits retain it. */
function applyCronStreamSandboxTarget(e, t) {
if (e.schedule.kind !== "stream") {
if (t !== void 0) throw new TypeError("Sandbox stream targets require a stream schedule");
delete e.streamSandbox;
return;
}
if (t !== void 0) {
const n = parseCronStreamSandboxTarget(t);
if (!n) throw new TypeError("Invalid sandbox stream target");
e.streamSandbox = n;
}
}
function finalizeUpdatedJob(e) {
const { job: t, nextJob: n, now: r } = e;
if (e.scheduleChanged && n.schedule.kind === "every") {
const e = n.schedule.anchorMs;
if (typeof e !== "number" || !Number.isFinite(e)) {
const e = (t.schedule.kind === "every" && t.schedule.everyMs === n.schedule.everyMs && typeof t.schedule.anchorMs === "number" && Number.isFinite(t.schedule.anchorMs) ? t.schedule.anchorMs : void 0) ?? r;
n.schedule = {
...n.schedule,
anchorMs: Math.max(0, Math.floor(e))
};
}
}
reconcileCronSourceIdentity(t, n, e.streamRestartExhaustedRequested);
const i = t.payload.kind === "script" ? t.payload.script : void 0;
const a = n.payload.kind === "script" ? n.payload.script : void 0;
if (!isDeepStrictEqual(t.trigger, n.trigger) || i !== a) for (const t of [
"triggerState",
"triggerEvalCount",
"lastTriggerEvalAtMs",
"lastTriggerFireAtMs"
]) if (e.explicitTriggerState && Object.hasOwn(e.explicitTriggerState, t)) Object.assign(n.state, { [t]: e.explicitTriggerState[t] });
else delete n.state[t];
const o = e.schedulingInputsRequested && !cronSchedulingInputsEqual(t, n);
if (e.scheduleChanged && n.schedule.kind === "cron" && !isJobEnabled(n)) computeJobNextRunAtMs({
...n,
enabled: true
}, r);
n.updatedAtMs = r;
if (o) {
n.state.scheduleActivatedAtMs = r;
n.state.startupCatchupAtMs = void 0;
n.state.pacedNextRunAtMs = void 0;
n.state.forcePreservedNextRunAtMs = cronSchedulingInputsEqual({
...t,
enabled: n.enabled
}, n) ? resolveForcePreservedOneShotAtMs(t) : void 0;
if (isJobEnabled(n)) n.state.nextRunAtMs = computeJobNextRunAtMs(n, r);
else {
n.state.nextRunAtMs = void 0;
n.state.queuedAtMs = void 0;
if (!(e.isJobActive ?? isCronJobActive)(n.id)) {
Object.assign(n.state, {
runningAtMs: void 0,
runningReceiptId: void 0
});
delete n.state.runningScheduleChangeId;
}
}
} else if (isJobEnabled(n) && !hasScheduledNextRunAtMs(n.state.nextRunAtMs)) n.state.nextRunAtMs = computeJobNextRunAtMs(n, r);
if (n.state.runningAtMs !== t.state.runningAtMs) delete n.state.runningReceiptId;
}
//#endregion
export { cloneCronJobForMutation as n, finalizeUpdatedJob as r, applyCronStreamSandboxTarget as t };