Skip to content

Commit 0dd0a0c

Browse files
t3dotggclaude
andcommitted
fix(scheduler): keep the stored next run across equivalent schedule edits, and read the start time before reserving a run
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d1e90de commit 0dd0a0c

2 files changed

Lines changed: 45 additions & 11 deletions

File tree

‎apps/server/src/scheduledTasks/ScheduledTaskService.interval.test.ts‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,33 @@ it.effect("runs a one-time schedule once and rejects past times", () =>
181181
),
182182
);
183183

184+
const ONCE = { type: "once", runAt: "2026-11-01T08:00:00Z" } as const;
185+
184186
const onceInput = {
185187
...input,
186188
commandId: "once-race",
187189
schedule: { type: "once", runAt: "2026-11-01T08:00:00Z" },
188190
};
189191

190192
it.effect.each([
191-
{ change: "a run completes", pause: false, expected: null },
192-
{ change: "another client pauses the task", pause: true, expected: "2026-11-01T08:00:00.000Z" },
193+
{ change: "a run completes", saved: ONCE, edited: ONCE, pause: false, expected: null },
194+
{
195+
change: "a run completes during a padded-time edit",
196+
saved: { type: "fixed_time", timeOfDay: "9:00" },
197+
edited: { type: "fixed_time", timeOfDay: "09:00" },
198+
pause: false,
199+
expected: null,
200+
},
201+
{
202+
change: "another client pauses the task",
203+
saved: ONCE,
204+
edited: ONCE,
205+
pause: true,
206+
expected: "2026-11-01T08:00:00.000Z",
207+
},
193208
] as const)(
194209
"a title save keeps the stored next run only while nothing else changed: $change",
195-
({ pause, expected }) =>
210+
({ saved, edited, pause, expected }) =>
196211
Effect.gen(function* () {
197212
yield* TestClock.setTime(Date.parse("2026-11-01T07:00:00.000Z"));
198213
// The project check runs between the save's read and write, so it stands in for
@@ -217,10 +232,19 @@ it.effect.each([
217232
const service = yield* ScheduledTaskService.ScheduledTaskService;
218233
const worktree = { type: "worktree", baseRef: "main" };
219234
yield* service.upsert(
220-
yield* decodeInput({ ...onceInput, workspaceStrategy: { type: "root" } }),
235+
yield* decodeInput({
236+
...onceInput,
237+
schedule: saved,
238+
workspaceStrategy: { type: "root" },
239+
}),
221240
);
222241
const renamed = yield* service.upsert(
223-
yield* decodeInput({ ...onceInput, workspaceStrategy: worktree, title: "Renamed" }),
242+
yield* decodeInput({
243+
...onceInput,
244+
schedule: edited,
245+
workspaceStrategy: worktree,
246+
title: "Renamed",
247+
}),
224248
);
225249
expect(renamed.task.enabled).toBe(true);
226250
expect(renamed.task.nextRunAt).toBe(expected);

‎apps/server/src/scheduledTasks/ScheduledTaskService.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,8 @@ export const layer = Layer.effect(
571571
const saveTask = (
572572
task: ScheduledTask,
573573
requireExisting: boolean,
574-
keepNextRun: boolean,
574+
/** The schedule this save read, when it left the schedule unchanged. */
575+
keptSchedule: ScheduledTask["schedule"] | null,
575576
webhook: {
576577
readonly token: string | null;
577578
readonly secret: string | null;
@@ -645,8 +646,11 @@ export const layer = Layer.effect(
645646
-- Keep a next_run_at that a finishing run moved during this save, unless
646647
-- another edit changed the schedule or enabled state meanwhile.
647648
next_run_at = CASE
648-
WHEN ${keepNextRun ? 1 : 0} = 1
649-
AND scheduled_tasks.schedule_json = excluded.schedule_json
649+
WHEN ${keptSchedule === null ? 0 : 1} = 1
650+
AND scheduled_tasks.schedule_json IN (
651+
excluded.schedule_json,
652+
${keptSchedule === null ? null : JSON.stringify(keptSchedule)}
653+
)
650654
AND scheduled_tasks.enabled = excluded.enabled
651655
THEN scheduled_tasks.next_run_at
652656
ELSE excluded.next_run_at
@@ -777,6 +781,9 @@ export const layer = Layer.effect(
777781
readonly dispatched: Ref.Ref<boolean>;
778782
},
779783
) {
784+
// Read before reserving: nothing between the reservation and its release may fail.
785+
const startedAt = yield* localNow;
786+
const startedAtIso = iso(startedAt);
780787
const reserved = yield* Ref.modify(activeRuns, (active) => {
781788
if (active.has(task.id)) return [false, active] as const;
782789
const next = new Set(active);
@@ -790,8 +797,6 @@ export const layer = Layer.effect(
790797
return { task, threadId: null };
791798
}
792799

793-
const startedAt = yield* localNow;
794-
const startedAtIso = iso(startedAt);
795800
return yield* Effect.gen(function* () {
796801
// Re-read stale poll snapshots so deleted, paused or postponed tasks cannot fire.
797802
const active = yield* findTask(task.id);
@@ -1272,7 +1277,12 @@ export const layer = Layer.effect(
12721277
lastRunError: existingTask?.lastRunError ?? null,
12731278
runCount: existingTask?.runCount ?? 0,
12741279
};
1275-
yield* saveTask(task, input.requireExisting === true, scheduleUnchanged, webhook);
1280+
yield* saveTask(
1281+
task,
1282+
input.requireExisting === true,
1283+
scheduleUnchanged ? existingTask.schedule : null,
1284+
webhook,
1285+
);
12761286
yield* notifyChanged;
12771287
return { task: (yield* findTask(id)) ?? task };
12781288
});

0 commit comments

Comments
 (0)