Skip to content

Commit 4767782

Browse files
t3dotggclaude
andcommitted
fix(scheduler): a run reports what it stored, and reads relay settings before taking the write lock
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a2435e9 commit 4767782

2 files changed

Lines changed: 48 additions & 9 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,35 @@ it.effect("keeps a one-time run after a restart interrupts an earlier manual run
280280
Effect.provide(SqlitePersistence.layerMemory),
281281
),
282282
);
283+
284+
it.effect("reports the stored task when the row was replaced during the run", () =>
285+
Effect.gen(function* () {
286+
yield* TestClock.setTime(Date.parse("2026-11-01T07:00:00.000Z"));
287+
// The launch stands in for a delete and same-id recreate while the run is out.
288+
const replaceRowDuringLaunch = Layer.unwrap(
289+
Effect.gen(function* () {
290+
const sql = yield* SqlClient.SqlClient;
291+
return Layer.mock(ThreadLaunchService.ThreadLaunchService)({
292+
launch: () =>
293+
sql`UPDATE scheduled_tasks SET last_run_status = 'never', last_run_at = NULL, run_count = 0`.pipe(
294+
Effect.orDie,
295+
Effect.andThen(Effect.die("synthetic dispatch failure")),
296+
),
297+
});
298+
}),
299+
);
300+
const layer = ScheduledTaskService.layer.pipe(
301+
Layer.provide(Layer.merge(dependencies, replaceRowDuringLaunch)),
302+
);
303+
yield* Effect.gen(function* () {
304+
const service = yield* ScheduledTaskService.ScheduledTaskService;
305+
const created = yield* service.upsert(yield* decodeInput(onceInput));
306+
const ran = yield* service.runNow({ id: created.task.id });
307+
expect(ran.task.runCount).toBe(0);
308+
expect(ran.task.lastRunStatus).toBe("never");
309+
}).pipe(Effect.provide(layer));
310+
}).pipe(
311+
Effect.provideService(ServerActivation, Effect.never),
312+
Effect.provide(SqlitePersistence.layerMemory),
313+
),
314+
);

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,9 @@ export const layer = Layer.effect(
727727
WHERE task_id = ${input.id}
728728
AND last_run_status = 'running'
729729
AND last_run_at = ${input.startedAtIso}
730+
RETURNING task_id
730731
`.pipe(
732+
Effect.map((rows) => rows.length > 0),
731733
Effect.mapError((cause) =>
732734
taskError("Could not record schedule task run.", { taskId: input.id, cause }),
733735
),
@@ -943,9 +945,12 @@ export const layer = Layer.effect(
943945
const lastRunError = runSucceeded ? null : errorMessage(result.cause);
944946
const threadId = runSucceeded ? result.value : null;
945947
// One transaction, so an edit cannot land between reading the schedule and
946-
// writing the next run computed from it.
948+
// writing the next run computed from it. The origin is read first: it reads
949+
// files, and the transaction holds SQLite's writer lock.
950+
const origin = yield* readWebhookOrigin;
947951
const record = Effect.gen(function* () {
948-
const current = yield* findTask(task.id);
952+
const [row] = yield* getRows(task.id);
953+
const current = row === undefined ? null : yield* decodeRow(row, origin);
949954
const scheduleSource = current ?? task;
950955
const completed: ScheduledTask = {
951956
...scheduleSource,
@@ -960,7 +965,7 @@ export const layer = Layer.effect(
960965
lastRunError,
961966
runCount: scheduleSource.runCount + 1,
962967
};
963-
if (current === null) return { completed, written: false };
968+
if (current === null) return { recorded: completed, written: false };
964969
// startedAtIso in the guard ensures this writes only to the row this
965970
// run marked as running — a task deleted mid-run and recreated with
966971
// the same id (idempotent commandId replay) must not be stamped.
@@ -972,21 +977,23 @@ export const layer = Layer.effect(
972977
error: lastRunError,
973978
startedAtIso,
974979
});
975-
if (webhook === undefined) yield* completion;
980+
let written = false;
981+
if (webhook === undefined) written = yield* completion;
976982
else {
977983
const pending =
978984
yield* sql`SELECT 1 FROM scheduled_task_webhook_pending WHERE delivery_id = ${webhook.deliveryId}`;
979-
if (pending.length === 0) yield* completion;
985+
if (pending.length === 0) written = yield* completion;
980986
else {
981987
const counted =
982988
yield* sql`UPDATE scheduled_task_webhook_pending SET state = ${runSucceeded ? "completed" : "failed"}
983989
WHERE delivery_id = ${webhook.deliveryId} AND state NOT IN ('completed', 'failed') RETURNING delivery_id`;
984-
if (counted.length > 0) yield* completion;
990+
if (counted.length > 0) written = yield* completion;
985991
}
986992
}
987-
return { completed, written: true };
993+
// A row recreated mid-run is not this run's, so report what is stored.
994+
return { recorded: written ? completed : current, written };
988995
});
989-
const { completed, written } = yield* sql
996+
const { recorded, written } = yield* sql
990997
.withTransaction(record)
991998
.pipe(
992999
Effect.mapError((cause) =>
@@ -1001,7 +1008,7 @@ export const layer = Layer.effect(
10011008
),
10021009
);
10031010
if (written) yield* notifyChanged;
1004-
return { task: completed, threadId };
1011+
return { task: recorded, threadId };
10051012
}).pipe(
10061013
Effect.onError((cause) =>
10071014
releaseStuckRun(task, startedAtIso, errorMessage(cause), webhook?.deliveryId),

0 commit comments

Comments
 (0)