Skip to content

Commit 505801d

Browse files
committed
fix(core): renew scheduler leader lease during long ticks and tighten leader test
1 parent 5aecb52 commit 505801d

2 files changed

Lines changed: 49 additions & 15 deletions

File tree

‎packages/core/src/schedule.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export * as Schedule from "./schedule.js"
22

33
import { randomUUID } from "node:crypto"
44
import { and, asc, eq, gt, isNull, lte, min, or, sql } from "drizzle-orm"
5-
import { Cause, Clock, Context, Cron, DateTime, Effect, Latch, Layer, Option, Result, Schema } from "effect"
5+
import { Cause, Clock, Context, Cron, DateTime, Effect, Latch, Layer, Option, Result, Schedule as EffectSchedule, Schema } from "effect"
66
import { Node, makeGlobalNode } from "@opencode/util/effect/app-node"
77
import { LayerNode } from "@opencode/util/effect/layer-node"
88
import {
@@ -447,6 +447,7 @@ const layer = Layer.effect(
447447
const step = Effect.fnUntraced(function* (row: Row, now: number) {
448448
const due = row.next_due_at
449449
if (due === null || row.session_id === null) return
450+
if (!(yield* claimLeader(yield* Clock.currentTimeMillis))) return
450451
const session = yield* sessions.get(row.session_id)
451452
if (!(yield* settings(session)).enabled) {
452453
// Reading the flag boots the Session's location, so check again in an hour rather than every tick. The row
@@ -574,6 +575,12 @@ const layer = Layer.effect(
574575
),
575576
)
576577

578+
const heartbeat = Clock.currentTimeMillis.pipe(
579+
Effect.flatMap(claimLeader),
580+
Effect.ignore,
581+
Effect.schedule(EffectSchedule.spaced(LEADER_TTL / 3)),
582+
)
583+
577584
const loop = Effect.gen(function* () {
578585
yield* doorbell.close
579586
const now = yield* Clock.currentTimeMillis
@@ -587,7 +594,13 @@ const layer = Layer.effect(
587594
leading = true
588595
yield* rewake.pipe(Effect.catchCause((cause) => Effect.logError("schedule rewake failed", { cause })))
589596
}
590-
yield* tickAt(now).pipe(Effect.catchCause((cause) => Effect.logError("schedule tick failed", { cause })))
597+
yield* Effect.scoped(
598+
Effect.forkScoped(heartbeat).pipe(
599+
Effect.andThen(
600+
tickAt(now).pipe(Effect.catchCause((cause) => Effect.logError("schedule tick failed", { cause }))),
601+
),
602+
),
603+
)
591604
const wait = yield* nextWake(now)
592605
if (wait > 0) yield* doorbell.await.pipe(Effect.timeoutOption(wait))
593606
})

‎packages/server/test/schedule.test.ts‎

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,40 @@ it.live("ticks due schedules in standalone ServerProcess.start without lifecycle
176176
}).pipe(Effect.scoped),
177177
)
178178

179-
it.live("elects one scheduler leader across two server processes sharing a database", () =>
179+
it.live("elects one scheduler leader when two server processes boot against a due schedule", () =>
180180
Effect.gen(function* () {
181181
const dir = (yield* tmpdirScoped("opencode-schedule-leader-")).path
182182
const dbPath = path.join(dir, "shared.db")
183183
const marker = path.join(dir, "runs.txt")
184+
yield* Effect.promise(() =>
185+
fs.writeFile(path.join(dir, "opencode.json"), JSON.stringify({ permission: { shell: "allow" } })),
186+
)
187+
188+
// Seed an active schedule in a short-lived scope so it falls due while no server is running.
189+
const createdID = yield* Effect.scoped(
190+
Effect.gen(function* () {
191+
const handler = yield* ServerFetch.make({
192+
app: { version: "test" },
193+
database: { path: dbPath },
194+
config: { directory: dir, project: true, claude: false },
195+
models: { fetch: false },
196+
fs: { filewatcher: false },
197+
})
198+
const seededApi = client(handler)
199+
return yield* Effect.promise(async () => {
200+
const session = await seededApi.session.create({ location: { directory: dir } })
201+
const created = await seededApi.experimental.schedule.create({
202+
sessionID: session.id,
203+
when: { in: "80 millis" },
204+
if: { run: `printf 'x\\n' >> ${JSON.stringify(marker)} && false` },
205+
prompt: "single leader check",
206+
})
207+
return created.id
208+
})
209+
}),
210+
)
211+
yield* Effect.promise(() => new Promise((resolve) => setTimeout(resolve, 120)))
212+
184213
const boot = () =>
185214
ServerProcess.start<never, never>({
186215
hostname: "127.0.0.1",
@@ -192,28 +221,20 @@ it.live("elects one scheduler leader across two server processes sharing a datab
192221
fs: { filewatcher: false },
193222
models: { fetch: false },
194223
})
195-
const first = yield* boot()
196-
yield* boot()
224+
// Both servers start against the same already-due row at t=0; without leader election both run the check.
225+
const [first] = yield* Effect.all([boot(), boot()], { concurrency: 2 })
197226
const base = `http://127.0.0.1:${"port" in first.address ? first.address.port : 0}`
198227
const api = OpenCode.make({
199228
baseUrl: base,
200229
headers: { authorization: `Basic ${btoa("opencode:secret")}` },
201230
})
202231

203232
yield* Effect.promise(async () => {
204-
await fs.writeFile(path.join(dir, "opencode.json"), JSON.stringify({ permission: { shell: "allow" } }))
205-
const session = await api.session.create({ location: { directory: dir } })
206-
const created = await api.experimental.schedule.create({
207-
sessionID: session.id,
208-
when: { in: "200 millis" },
209-
if: { run: `printf 'x\\n' >> ${JSON.stringify(marker)} && false` },
210-
prompt: "single leader check",
211-
})
212233
const deadline = Date.now() + 5_000
213-
let current = created
234+
let current = await api.experimental.schedule.get({ scheduleID: createdID })
214235
while (current.status === "active" && Date.now() < deadline) {
215236
await new Promise((resolve) => setTimeout(resolve, 50))
216-
current = await api.experimental.schedule.get({ scheduleID: created.id })
237+
current = await api.experimental.schedule.get({ scheduleID: createdID })
217238
}
218239
await new Promise((resolve) => setTimeout(resolve, 150))
219240
const lines = (await fs.readFile(marker, "utf8")).trim().split("\n").filter(Boolean)

0 commit comments

Comments
 (0)