Skip to content

Commit 1b10653

Browse files
fix(core): keep the automation scheduler running after a storage failure (#68145)
Co-authored-by: opencodejr[bot] <348916617+opencodejr[bot]@users.noreply.github.com> Co-authored-by: neriousy <hejmowskifilip@gmail.com>
1 parent a0a2acd commit 1b10653

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎packages/core/src/automation.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,18 @@ const layer = Layer.effect(
10321032
if (wait > 0) yield* doorbell.await.pipe(Effect.timeoutOption(wait))
10331033
})
10341034

1035-
const run = Effect.forever(loop).pipe(Effect.ensuring(releaseLeader))
1035+
// A storage failure (a full disk, a lock held past busy_timeout) dies inside the loop's own queries. Log it and try
1036+
// again after MAX_SLEEP instead of ending the scheduler for the life of the process. A failure that races shutdown
1037+
// carries an interrupt too, and still stops the loop.
1038+
const run = Effect.forever(
1039+
loop.pipe(
1040+
Effect.catchCauseIf(
1041+
(cause) => !Cause.hasInterrupts(cause),
1042+
(cause) =>
1043+
Effect.logError("automation scheduler failed", { cause }).pipe(Effect.andThen(Effect.sleep(MAX_SLEEP))),
1044+
),
1045+
),
1046+
).pipe(Effect.ensuring(releaseLeader))
10361047

10371048
const resolveZone = (tz: string | undefined) => {
10381049
if (tz === undefined) return Effect.succeed(DateTime.zoneMakeLocal())

‎packages/core/test/automation.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,39 @@ describe("Automation", () => {
13851385
}),
13861386
)
13871387

1388+
it.effect("keeps scheduling after a storage failure in the loop's own queries", () =>
1389+
Effect.gen(function* () {
1390+
const automations = yield* setup()
1391+
const { db } = yield* Database.Service
1392+
// Fail the lease claim, as a full disk or a lock held past busy_timeout would.
1393+
yield* db
1394+
.run(
1395+
"CREATE TRIGGER fail_lease BEFORE INSERT ON kv WHEN NEW.key = 'automation:leader' BEGIN SELECT RAISE(ABORT, 'database or disk is full'); END",
1396+
)
1397+
.pipe(Effect.orDie)
1398+
const automation = yield* automations.create({
1399+
sessionID,
1400+
name: "after the failure",
1401+
trigger: { type: "at", at: "in 10 seconds" },
1402+
action: prompt("still scheduled"),
1403+
})
1404+
const runner = yield* Effect.forkChild(automations.run)
1405+
yield* Effect.repeat(Effect.yieldNow, { times: 5 })
1406+
expect(runner.pollUnsafe()).toBeUndefined()
1407+
1408+
// The database recovers, but the loop waits out its MAX_SLEEP backoff instead of retrying at once.
1409+
yield* db.run("DROP TRIGGER fail_lease").pipe(Effect.orDie)
1410+
yield* TestClock.adjust("10 seconds")
1411+
yield* Effect.repeat(Effect.yieldNow, { times: 5 })
1412+
expect(yield* automations.get(automation.id)).toMatchObject({ status: "active", runCount: 0 })
1413+
1414+
yield* TestClock.adjust("20 seconds")
1415+
yield* Effect.repeat(Effect.yieldNow, { times: 5 })
1416+
yield* Fiber.interrupt(runner)
1417+
expect(yield* automations.get(automation.id)).toMatchObject({ status: "done", runCount: 1 })
1418+
}),
1419+
)
1420+
13881421
describe("code actions", () => {
13891422
/** Approves every permission ask "once", as a person at the creating Session would, and records them. */
13901423
const answerAsks = Effect.gen(function* () {

0 commit comments

Comments
 (0)