Skip to content

Commit f0676a7

Browse files
fix(cron): delete successful on-exit deleteAfterRun jobs (#104550)
* fix(cron): delete successful on-exit deleteAfterRun jobs deleteAfterRun finalization was restricted to at schedules, so a successful on-exit job with deleteAfterRun=true was retained as a disabled job instead of being removed (the gateway exit watcher durably disables on-exit jobs BEFORE firing as replay protection, and no later transition deleted them). Successful delete-after-run now covers every one-shot schedule kind; the pre-fire disable ordering is unchanged. Regression covers deletion on ok, retention without the flag, and retention on failure through the same force path the exit watcher fires. Fixes #104518 * test(cron): prove durable on-exit finalization --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 7778663 commit f0676a7

2 files changed

Lines changed: 75 additions & 3 deletions

File tree

‎src/cron/service/ops.regression.test.ts‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
tryBeginGatewayRootWorkAdmission,
2323
} from "../../process/gateway-work-admission.js";
2424
import { CommandLane } from "../../process/lanes.js";
25-
import { saveCronStore } from "../store.js";
25+
import { loadCronStore, saveCronStore } from "../store.js";
2626
import { enqueueRun, remove, run, start } from "./ops.js";
2727
import type { CronEvent } from "./state.js";
2828
import { createCronServiceState } from "./state.js";
@@ -795,4 +795,74 @@ describe("cron service ops regressions", () => {
795795

796796
clearCommandLane(CommandLane.Cron);
797797
});
798+
it.each([
799+
{
800+
id: "onexit-delete-ok",
801+
deleteAfterRun: true,
802+
runStatus: "ok" as const,
803+
expectedJob: undefined,
804+
expectedActions: ["started", "finished", "removed"],
805+
},
806+
{
807+
id: "onexit-keep-ok",
808+
deleteAfterRun: false,
809+
runStatus: "ok" as const,
810+
expectedJob: { enabled: false, lastStatus: "ok" },
811+
expectedActions: ["started", "finished"],
812+
},
813+
{
814+
id: "onexit-delete-error",
815+
deleteAfterRun: true,
816+
runStatus: "error" as const,
817+
expectedJob: { enabled: false, lastStatus: "error" },
818+
expectedActions: ["started", "finished"],
819+
},
820+
])("#104518 finalizes watcher-fired on-exit job: $id", async (params) => {
821+
const store = opsRegressionFixtures.makeStorePath();
822+
const nowMs = Date.now();
823+
const job = createIsolatedRegressionJob({
824+
id: params.id,
825+
name: params.id,
826+
scheduledAt: nowMs,
827+
schedule: { kind: "on-exit", command: 'sh -c "exit 0"' },
828+
payload: { kind: "agentTurn", message: "post-exit payload" },
829+
state: {},
830+
});
831+
job.deleteAfterRun = params.deleteAfterRun;
832+
// The gateway watcher persists this disable before force-running the payload.
833+
job.enabled = false;
834+
await saveCronStore(store.storePath, { version: 1, jobs: [job] });
835+
836+
const events: CronEvent[] = [];
837+
const state = createCronServiceState({
838+
cronEnabled: false,
839+
storePath: store.storePath,
840+
log: noopLogger,
841+
enqueueSystemEvent: vi.fn(),
842+
requestHeartbeat: vi.fn(),
843+
runIsolatedAgentJob:
844+
params.runStatus === "ok"
845+
? vi.fn().mockResolvedValue({ status: "ok", summary: "ok" })
846+
: vi.fn().mockResolvedValue({ status: "error", error: "boom" }),
847+
onEvent: (event) => events.push(event),
848+
});
849+
await expect(run(state, params.id, "force")).resolves.toEqual({ ok: true, ran: true });
850+
851+
const memoryJob = state.store?.jobs.find((entry) => entry.id === params.id);
852+
const durableJob = (await loadCronStore(store.storePath)).jobs.find(
853+
(entry) => entry.id === params.id,
854+
);
855+
if (params.expectedJob) {
856+
for (const persistedJob of [memoryJob, durableJob]) {
857+
expect(persistedJob).toMatchObject({
858+
enabled: params.expectedJob.enabled,
859+
state: { lastStatus: params.expectedJob.lastStatus },
860+
});
861+
}
862+
} else {
863+
expect(memoryJob).toBeUndefined();
864+
expect(durableJob).toBeUndefined();
865+
}
866+
expect(events.map((event) => event.action)).toEqual(params.expectedActions);
867+
});
798868
});

‎src/cron/service/timer.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,10 @@ export function applyJobResult(
803803
job.state.lastFailureAlertAtMs = undefined;
804804
}
805805

806-
const shouldDelete =
807-
job.schedule.kind === "at" && job.deleteAfterRun === true && result.status === "ok";
806+
// The gateway watcher disables on-exit jobs before firing; successful removal here
807+
// completes the same deleteAfterRun contract as a one-shot at schedule.
808+
const isOneShotSchedule = job.schedule.kind === "at" || job.schedule.kind === "on-exit";
809+
const shouldDelete = isOneShotSchedule && job.deleteAfterRun === true && result.status === "ok";
808810
const retryDisabledHeartbeatOneShot = shouldRetryDisabledHeartbeatOneShot(job, result);
809811

810812
if (!shouldDelete) {

0 commit comments

Comments
 (0)