mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
feat(core,server): enable schedules by default and run loop in opencode serve (#60478)
This commit is contained in:
committed by
GitHub
Unverified
parent
4e7d369021
commit
e76c67a250
@@ -1,7 +1,8 @@
|
||||
export * as Schedule from "./schedule.js"
|
||||
|
||||
import { and, asc, eq, gt, isNull, lte, min, or } from "drizzle-orm"
|
||||
import { Cause, Clock, Context, Cron, DateTime, Effect, Latch, Layer, Option, Result, Schema } from "effect"
|
||||
import { randomUUID } from "node:crypto"
|
||||
import { and, asc, eq, gt, isNull, lte, min, or, sql } from "drizzle-orm"
|
||||
import { Cause, Clock, Context, Cron, DateTime, Effect, Latch, Layer, Option, Result, Schedule as EffectSchedule, Schema } from "effect"
|
||||
import { Node, makeGlobalNode } from "@opencode/util/effect/app-node"
|
||||
import { LayerNode } from "@opencode/util/effect/layer-node"
|
||||
import {
|
||||
@@ -23,6 +24,7 @@ import { Config } from "./config.js"
|
||||
import { Database } from "./database/database.js"
|
||||
import { FileSystem } from "./filesystem.js"
|
||||
import { Instance } from "./instance/service.js"
|
||||
import { KVTable } from "./kv/sql.js"
|
||||
import type { Permission } from "./permission.js"
|
||||
import { Session } from "./session.js"
|
||||
import { SessionExecution } from "./session/execution.js"
|
||||
@@ -52,7 +54,7 @@ export class InvalidError extends Schema.TaggedError<InvalidError>()("Schedule.I
|
||||
|
||||
export class DisabledError extends Schema.TaggedError<DisabledError>()("Schedule.DisabledError", {}) {
|
||||
override get message() {
|
||||
return "Scheduled tasks are disabled; set experimental.schedules in config"
|
||||
return "Scheduled tasks are disabled by experimental.schedules: false in config"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,8 +72,9 @@ export interface Interface {
|
||||
/** Fires every schedule that is due now, at most once each. */
|
||||
readonly tick: Effect.Effect<void>
|
||||
/**
|
||||
* Runs `tick` whenever a schedule falls due, until interrupted. All state lives in the table, so exactly one
|
||||
* process may run this: the managed server.
|
||||
* Runs `tick` whenever a schedule falls due, until interrupted. Elects at most one active leader per SQLite
|
||||
* database via a lease row, so auxiliary processes (`opencode acp`, `--standalone`) sharing a data directory
|
||||
* never double-fire alongside a managed service.
|
||||
*/
|
||||
readonly run: Effect.Effect<never>
|
||||
}
|
||||
@@ -90,6 +93,8 @@ const DEFAULT_UNTIL_AFTER = 24 * HOUR
|
||||
const LATE_AFTER = 60_000
|
||||
/** The loop re-reads the table at least this often, so clock jumps and missed doorbells are caught. */
|
||||
const MAX_SLEEP = 30_000
|
||||
const LEADER_KEY = "schedule:leader"
|
||||
const LEADER_TTL = 2 * MAX_SLEEP
|
||||
/** How long a due schedule waits before its location's flag is read again, while schedules are off there. */
|
||||
const DISABLED_RETRY = HOUR
|
||||
/** How long a schedule whose prompt failed waits before trying again. */
|
||||
@@ -211,10 +216,10 @@ const layer = Layer.effect(
|
||||
const settings = (session: SessionSchema.Info) =>
|
||||
Effect.gen(function* () {
|
||||
const config = yield* Effect.serviceOption(Config.Service)
|
||||
if (Option.isNone(config)) return { enabled: false, timezone: undefined }
|
||||
if (Option.isNone(config)) return { enabled: true, timezone: undefined }
|
||||
const entries = yield* config.value.entries()
|
||||
return {
|
||||
enabled: Config.latest(entries, "experimental")?.schedules === true,
|
||||
enabled: Config.latest(entries, "experimental")?.schedules !== false,
|
||||
timezone: Config.latest(entries, "timezone"),
|
||||
}
|
||||
}).pipe(instances.provide(session))
|
||||
@@ -442,6 +447,7 @@ const layer = Layer.effect(
|
||||
const step = Effect.fnUntraced(function* (row: Row, now: number) {
|
||||
const due = row.next_due_at
|
||||
if (due === null || row.session_id === null) return
|
||||
if (!(yield* claimLeader(yield* Clock.currentTimeMillis))) return
|
||||
const session = yield* sessions.get(row.session_id)
|
||||
if (!(yield* settings(session)).enabled) {
|
||||
// Reading the flag boots the Session's location, so check again in an hour rather than every tick. The row
|
||||
@@ -538,18 +544,98 @@ const layer = Layer.effect(
|
||||
),
|
||||
)
|
||||
|
||||
const hasActiveRows = db
|
||||
.select({ id: ScheduleTable.id })
|
||||
.from(ScheduleTable)
|
||||
.where(eq(ScheduleTable.status, "active"))
|
||||
.limit(1)
|
||||
.get()
|
||||
.pipe(
|
||||
Effect.orDie,
|
||||
Effect.map((row) => row !== undefined),
|
||||
)
|
||||
|
||||
const ownerID = randomUUID()
|
||||
let leading = false
|
||||
let bootChecked = false
|
||||
|
||||
const claimLeader = Effect.fnUntraced(function* (now: number) {
|
||||
const value = { owner: ownerID, until: now + LEADER_TTL }
|
||||
const claimed = yield* db
|
||||
.insert(KVTable)
|
||||
.values({ key: LEADER_KEY, value, time_created: now, time_updated: now })
|
||||
.onConflictDoUpdate({
|
||||
target: KVTable.key,
|
||||
set: { value, time_updated: now },
|
||||
where: sql`json_extract(${KVTable.value}, '$.owner') = ${ownerID} or json_extract(${KVTable.value}, '$.until') <= ${now}`,
|
||||
})
|
||||
.returning({ key: KVTable.key })
|
||||
.get()
|
||||
.pipe(Effect.orDie)
|
||||
return claimed !== undefined
|
||||
})
|
||||
|
||||
const releaseLeader = Effect.sync(() => {
|
||||
leading = false
|
||||
}).pipe(
|
||||
Effect.andThen(
|
||||
db
|
||||
.delete(KVTable)
|
||||
.where(and(eq(KVTable.key, LEADER_KEY), sql`json_extract(${KVTable.value}, '$.owner') = ${ownerID}`))
|
||||
.run()
|
||||
.pipe(Effect.ignore),
|
||||
),
|
||||
)
|
||||
|
||||
const heartbeat = Clock.currentTimeMillis.pipe(
|
||||
Effect.flatMap(claimLeader),
|
||||
Effect.ignore,
|
||||
Effect.schedule(EffectSchedule.spaced(LEADER_TTL / 3)),
|
||||
)
|
||||
|
||||
const loop = Effect.gen(function* () {
|
||||
yield* doorbell.close
|
||||
const active = yield* hasActiveRows
|
||||
if (!active && bootChecked) {
|
||||
if (leading) yield* releaseLeader
|
||||
yield* doorbell.await.pipe(Effect.timeoutOption(MAX_SLEEP))
|
||||
return
|
||||
}
|
||||
const now = yield* Clock.currentTimeMillis
|
||||
yield* tickAt(now).pipe(Effect.catchCause((cause) => Effect.logError("schedule tick failed", { cause })))
|
||||
const isLeader = yield* claimLeader(now)
|
||||
if (!isLeader) {
|
||||
leading = false
|
||||
bootChecked = true
|
||||
yield* doorbell.await.pipe(Effect.timeoutOption(MAX_SLEEP))
|
||||
return
|
||||
}
|
||||
if (!leading) {
|
||||
leading = true
|
||||
bootChecked = true
|
||||
yield* rewake.pipe(Effect.catchCause((cause) => Effect.logError("schedule rewake failed", { cause })))
|
||||
}
|
||||
if (!active) {
|
||||
yield* releaseLeader
|
||||
yield* doorbell.await.pipe(Effect.timeoutOption(MAX_SLEEP))
|
||||
return
|
||||
}
|
||||
yield* Effect.scoped(
|
||||
Effect.forkScoped(heartbeat).pipe(
|
||||
Effect.andThen(
|
||||
tickAt(now).pipe(Effect.catchCause((cause) => Effect.logError("schedule tick failed", { cause }))),
|
||||
),
|
||||
),
|
||||
)
|
||||
if (!(yield* hasActiveRows)) {
|
||||
yield* releaseLeader
|
||||
yield* doorbell.await.pipe(Effect.timeoutOption(MAX_SLEEP))
|
||||
return
|
||||
}
|
||||
const wait = yield* nextWake(now)
|
||||
if (wait > 0) yield* doorbell.await.pipe(Effect.timeoutOption(wait))
|
||||
})
|
||||
|
||||
const run = rewake.pipe(
|
||||
Effect.catchCause((cause) => Effect.logError("schedule rewake failed", { cause })),
|
||||
Effect.andThen(Effect.forever(loop)),
|
||||
)
|
||||
const run = Effect.forever(loop).pipe(Effect.ensuring(releaseLeader))
|
||||
|
||||
const resolveZone = (tz: string | undefined) => {
|
||||
if (tz === undefined) return Effect.succeed(DateTime.zoneMakeLocal())
|
||||
|
||||
@@ -52,7 +52,7 @@ export const Plugin = {
|
||||
ctx.event,
|
||||
Effect.andThen(ctx.tool.reload(), ctx.command.reload()),
|
||||
)
|
||||
const enabled = () => Config.latest(loaded.entries, "experimental")?.schedules === true
|
||||
const enabled = () => Config.latest(loaded.entries, "experimental")?.schedules !== false
|
||||
|
||||
// A request keeps the tools it started with after config reloads, so each call rechecks the flag.
|
||||
const allowed = (sessionID: Session.ID) =>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect } from "bun:test"
|
||||
import { FileSystem } from "@opencode/core/filesystem"
|
||||
import { DateTime, Effect, Fiber, Latch, Layer, LayerMap, Queue, Schema, Sink, Stream } from "effect"
|
||||
import { Clock, DateTime, Effect, Fiber, Latch, Layer, LayerMap, Queue, Schema, Sink, Stream } from "effect"
|
||||
import { ChildProcessSpawner } from "effect/process"
|
||||
import { ExitCode, makeHandle, ProcessId } from "effect/process/ChildProcessSpawner"
|
||||
import { TestClock } from "effect/testing"
|
||||
@@ -76,11 +76,19 @@ const enabledEntries = [
|
||||
type: "document",
|
||||
info: new Info({
|
||||
timezone: "America/New_York",
|
||||
experimental: new ConfigExperimental.Info({ schedules: true }),
|
||||
}),
|
||||
}),
|
||||
]
|
||||
const disabledEntries = [
|
||||
new Document({
|
||||
type: "document",
|
||||
info: new Info({
|
||||
experimental: new ConfigExperimental.Info({ schedules: false }),
|
||||
}),
|
||||
}),
|
||||
]
|
||||
const enabled = Config.testLayer(enabledEntries)
|
||||
const disabled = Config.testLayer(disabledEntries)
|
||||
/** A location whose flag the test turns on, counting every config read. */
|
||||
const toggle = { on: false, reads: 0 }
|
||||
const toggled = Layer.succeed(
|
||||
@@ -89,7 +97,7 @@ const toggled = Layer.succeed(
|
||||
entries: () =>
|
||||
Effect.sync(() => {
|
||||
toggle.reads++
|
||||
return toggle.on ? enabledEntries : []
|
||||
return toggle.on ? enabledEntries : disabledEntries
|
||||
}),
|
||||
changes: () => Stream.empty,
|
||||
}),
|
||||
@@ -224,7 +232,7 @@ const locations = makeGlobalNode({
|
||||
? enabled
|
||||
: ref.directory === toggleDirectory
|
||||
? toggled
|
||||
: Layer.empty,
|
||||
: disabled,
|
||||
configureAgent.pipe(
|
||||
Layer.provideMerge(
|
||||
LayerNode.compile(
|
||||
@@ -1466,5 +1474,51 @@ describe("Schedule", () => {
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("picks up a schedule created by another process sharing the DB while idle", () =>
|
||||
Effect.gen(function* () {
|
||||
const primary = yield* setup()
|
||||
const { db } = yield* Database.Service
|
||||
const runner = yield* Effect.forkChild(primary.run)
|
||||
yield* Effect.repeat(Effect.yieldNow, { times: 5 })
|
||||
|
||||
// Insert directly into SQLite (bypassing primary.create so primary's in-memory doorbell is never opened)
|
||||
// to simulate a short-lived `opencode serve` / `--standalone` writing a schedule and exiting.
|
||||
const now = yield* Clock.currentTimeMillis
|
||||
const externalID = Schedule.ID.create()
|
||||
yield* db
|
||||
.insert(ScheduleTable)
|
||||
.values({
|
||||
id: externalID,
|
||||
session_id: sessionID,
|
||||
name: "external",
|
||||
check: null,
|
||||
state: null,
|
||||
checks: 0,
|
||||
action: { type: "prompt", prompt: "Cross-process wake." },
|
||||
when: { type: "in", in: "10 seconds" },
|
||||
tz: "UTC",
|
||||
status: "active",
|
||||
next_due_at: now + 10_000,
|
||||
ends_at: null,
|
||||
remaining_runs: 1,
|
||||
time_created: now,
|
||||
time_updated: now,
|
||||
})
|
||||
.run()
|
||||
.pipe(Effect.orDie)
|
||||
|
||||
// Bounded MAX_SLEEP (30s) wakes the idle loop, claims leadership, and fires the row.
|
||||
yield* TestClock.adjust("30 seconds")
|
||||
yield* Effect.repeat(Effect.yieldNow, { times: 5 })
|
||||
yield* Fiber.interrupt(runner)
|
||||
|
||||
expect(yield* primary.get(externalID)).toMatchObject({
|
||||
status: "done",
|
||||
runCount: 1,
|
||||
lastOutcome: "fired",
|
||||
})
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -141,15 +141,14 @@ const paths = (registry: Tool.Interface) =>
|
||||
)
|
||||
|
||||
describe("ScheduleTools", () => {
|
||||
it.effect("are absent unless experimental.schedules is enabled", () =>
|
||||
it.effect("are registered by default unless experimental.schedules is false", () =>
|
||||
Effect.gen(function* () {
|
||||
expect(yield* paths((yield* setup(undefined)).registry)).not.toContain(names[0])
|
||||
const off = yield* setup(new ConfigExperimental.Info({ schedules: false }))
|
||||
expect(yield* paths(off.registry)).not.toContain(names[0])
|
||||
expect(off.commands).toEqual([])
|
||||
const on = yield* setup(new ConfigExperimental.Info({ schedules: true }))
|
||||
expect(yield* paths(on.registry)).toEqual(expect.arrayContaining(names))
|
||||
expect(on.commands.map((command) => command.name)).toEqual(["schedule"])
|
||||
const byDefault = yield* setup(undefined)
|
||||
expect(yield* paths(byDefault.registry)).toEqual(expect.arrayContaining(names))
|
||||
expect(byDefault.commands.map((command) => command.name)).toEqual(["schedule"])
|
||||
}),
|
||||
)
|
||||
|
||||
|
||||
Generated
+6
-6
@@ -12722,7 +12722,7 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "Schedule a prompt that wakes the Session once after a delay, at a time, every interval, or on a cron. Experimental: needs experimental.schedules in the Session's config and may change without notice.",
|
||||
"description": "Schedule a prompt that wakes the Session once after a delay, at a time, every interval, or on a cron. Experimental: internal OpenCode schedule API; wire shape may change without notice (disable with experimental.schedules: false).",
|
||||
"summary": "Create schedule",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
@@ -12811,7 +12811,7 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "List a Session's schedules, oldest first. Experimental: needs experimental.schedules in the Session's config and may change without notice.",
|
||||
"description": "List a Session's schedules, oldest first. Experimental: internal OpenCode schedule API; wire shape may change without notice (disable with experimental.schedules: false).",
|
||||
"summary": "List schedules"
|
||||
}
|
||||
},
|
||||
@@ -12889,7 +12889,7 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "Read one schedule. Experimental: needs experimental.schedules in the Session's config and may change without notice.",
|
||||
"description": "Read one schedule. Experimental: internal OpenCode schedule API; wire shape may change without notice (disable with experimental.schedules: false).",
|
||||
"summary": "Get schedule"
|
||||
},
|
||||
"delete": {
|
||||
@@ -12951,7 +12951,7 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "Delete a schedule. Experimental: needs experimental.schedules in the Session's config and may change without notice.",
|
||||
"description": "Delete a schedule. Experimental: internal OpenCode schedule API; wire shape may change without notice (disable with experimental.schedules: false).",
|
||||
"summary": "Delete schedule"
|
||||
}
|
||||
},
|
||||
@@ -13029,7 +13029,7 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "Stop a schedule from firing until it is resumed. Experimental: needs experimental.schedules in the Session's config and may change without notice.",
|
||||
"description": "Stop a schedule from firing until it is resumed. Experimental: internal OpenCode schedule API; wire shape may change without notice (disable with experimental.schedules: false).",
|
||||
"summary": "Pause schedule"
|
||||
}
|
||||
},
|
||||
@@ -13107,7 +13107,7 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "Resume a paused schedule. A one-shot that fell due while paused fires once; a repeating schedule resumes at its next slot. Experimental: needs experimental.schedules in the Session's config and may change without notice.",
|
||||
"description": "Resume a paused schedule. A one-shot that fell due while paused fires once; a repeating schedule resumes at its next slot. Experimental: internal OpenCode schedule API; wire shape may change without notice (disable with experimental.schedules: false).",
|
||||
"summary": "Resume schedule"
|
||||
}
|
||||
},
|
||||
|
||||
@@ -4,7 +4,8 @@ import { Schema } from "effect"
|
||||
import { HttpApiEndpoint, HttpApiSchema, OpenApi } from "effect/http-api"
|
||||
import { ForbiddenError, InvalidRequestError, ScheduleNotFoundError, SessionNotFoundError } from "../errors.js"
|
||||
|
||||
const EXPERIMENTAL = "Experimental: needs experimental.schedules in the Session's config and may change without notice."
|
||||
const EXPERIMENTAL =
|
||||
"Experimental: internal OpenCode schedule API; wire shape may change without notice (disable with experimental.schedules: false)."
|
||||
const errors = [ScheduleNotFoundError, ForbiddenError] as const
|
||||
const params = { scheduleID: Schedule.ID }
|
||||
const annotations = (identifier: string, summary: string, description: string) =>
|
||||
|
||||
@@ -110,9 +110,9 @@ export const start = Effect.fn("ServerProcess.start")(function* <E, R>(
|
||||
yield* installRestartContinuity(Context.get(context, SessionRestart.Service)).pipe(
|
||||
Effect.provideService(Scope.Scope, applicationScope),
|
||||
)
|
||||
// Schedules fire from one process only; the table holds all their state.
|
||||
yield* Effect.forkIn(Context.get(context, Schedule.Service).run, applicationScope)
|
||||
}
|
||||
// Schedules fire from any server process (`service`, `opencode serve`, or `--standalone`); the table holds all their state.
|
||||
yield* Effect.forkIn(Context.get(context, Schedule.Service).run, applicationScope)
|
||||
yield* Ref.set(application, Option.some(Context.get(context, HttpRouter.HttpRouter).asHttpEffect()))
|
||||
yield* status.ready
|
||||
const bus = Context.get(context, Bus.Service)
|
||||
|
||||
@@ -6,6 +6,8 @@ import { Effect } from "effect"
|
||||
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
|
||||
import { it } from "../../core/test/lib/effect"
|
||||
import { ServerFetch } from "../src/fetch"
|
||||
import { ServerProcess } from "../src/process"
|
||||
import { startServer } from "./fixture/server"
|
||||
|
||||
const client = (handler: (request: Request) => Promise<Response>) =>
|
||||
OpenCode.make({
|
||||
@@ -15,7 +17,7 @@ const client = (handler: (request: Request) => Promise<Response>) =>
|
||||
}),
|
||||
})
|
||||
|
||||
it.live("manages schedules through the experimental API where the session's config enables them", () =>
|
||||
it.live("manages schedules by default through the experimental API and honors experimental.schedules: false", () =>
|
||||
Effect.gen(function* () {
|
||||
const dir = (yield* tmpdirScoped("opencode-schedule-api-")).path
|
||||
const global = path.join(dir, "global")
|
||||
@@ -23,7 +25,7 @@ it.live("manages schedules through the experimental API where the session's conf
|
||||
const disabled = path.join(dir, "disabled")
|
||||
yield* Effect.promise(async () => {
|
||||
await Promise.all([global, enabled, disabled].map((item) => fs.mkdir(item, { recursive: true })))
|
||||
await fs.writeFile(path.join(enabled, "opencode.json"), JSON.stringify({ experimental: { schedules: true } }))
|
||||
await fs.writeFile(path.join(disabled, "opencode.json"), JSON.stringify({ experimental: { schedules: false } }))
|
||||
})
|
||||
const handler = yield* ServerFetch.make({
|
||||
app: { version: "test" },
|
||||
@@ -142,3 +144,102 @@ it.live("manages schedules through the experimental API where the session's conf
|
||||
})
|
||||
}).pipe(Effect.scoped),
|
||||
)
|
||||
|
||||
it.live("ticks due schedules in standalone ServerProcess.start without lifecycle", () =>
|
||||
Effect.gen(function* () {
|
||||
const dir = (yield* tmpdirScoped("opencode-schedule-serve-")).path
|
||||
const server = yield* startServer(dir)
|
||||
const api = OpenCode.make({ baseUrl: server.base, headers: server.headers })
|
||||
|
||||
yield* Effect.promise(async () => {
|
||||
await fs.writeFile(path.join(dir, "opencode.json"), JSON.stringify({ permission: { shell: "allow" } }))
|
||||
const session = await api.session.create({ location: { directory: dir } })
|
||||
const created = await api.experimental.schedule.create({
|
||||
sessionID: session.id,
|
||||
when: { in: "150 millis" },
|
||||
if: { run: "false" },
|
||||
prompt: "wake from serve",
|
||||
})
|
||||
expect(created.status).toBe("active")
|
||||
|
||||
const deadline = Date.now() + 5_000
|
||||
let current = created
|
||||
while (current.status === "active" && Date.now() < deadline) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 50))
|
||||
current = await api.experimental.schedule.get({ scheduleID: created.id })
|
||||
}
|
||||
expect(current).toMatchObject({
|
||||
status: "done",
|
||||
lastOutcome: "checked",
|
||||
})
|
||||
})
|
||||
}).pipe(Effect.scoped),
|
||||
)
|
||||
|
||||
it.live("elects one scheduler leader when two server processes boot against a due schedule", () =>
|
||||
Effect.gen(function* () {
|
||||
const dir = (yield* tmpdirScoped("opencode-schedule-leader-")).path
|
||||
const dbPath = path.join(dir, "shared.db")
|
||||
const marker = path.join(dir, "runs.txt")
|
||||
yield* Effect.promise(() =>
|
||||
fs.writeFile(path.join(dir, "opencode.json"), JSON.stringify({ permission: { shell: "allow" } })),
|
||||
)
|
||||
|
||||
// Seed an active schedule in a short-lived scope so it falls due while no server is running.
|
||||
const createdID = yield* Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const handler = yield* ServerFetch.make({
|
||||
app: { version: "test" },
|
||||
database: { path: dbPath },
|
||||
config: { directory: dir, project: true, claude: false },
|
||||
models: { fetch: false },
|
||||
fs: { filewatcher: false },
|
||||
})
|
||||
const seededApi = client(handler)
|
||||
return yield* Effect.promise(async () => {
|
||||
const session = await seededApi.session.create({ location: { directory: dir } })
|
||||
const created = await seededApi.experimental.schedule.create({
|
||||
sessionID: session.id,
|
||||
when: { in: "80 millis" },
|
||||
if: { run: `printf 'x\\n' >> ${JSON.stringify(marker)} && false` },
|
||||
prompt: "single leader check",
|
||||
})
|
||||
return created.id
|
||||
})
|
||||
}),
|
||||
)
|
||||
yield* Effect.promise(() => new Promise((resolve) => setTimeout(resolve, 120)))
|
||||
|
||||
const boot = () =>
|
||||
ServerProcess.start<never, never>({
|
||||
hostname: "127.0.0.1",
|
||||
port: 0,
|
||||
password: "secret",
|
||||
app: { version: "test-version" },
|
||||
database: { path: dbPath },
|
||||
config: { directory: dir },
|
||||
fs: { filewatcher: false },
|
||||
models: { fetch: false },
|
||||
})
|
||||
// Both servers start against the same already-due row at t=0; without leader election both run the check.
|
||||
const [first] = yield* Effect.all([boot(), boot()], { concurrency: 2 })
|
||||
const base = `http://127.0.0.1:${"port" in first.address ? first.address.port : 0}`
|
||||
const api = OpenCode.make({
|
||||
baseUrl: base,
|
||||
headers: { authorization: `Basic ${btoa("opencode:secret")}` },
|
||||
})
|
||||
|
||||
yield* Effect.promise(async () => {
|
||||
const deadline = Date.now() + 5_000
|
||||
let current = await api.experimental.schedule.get({ scheduleID: createdID })
|
||||
while (current.status === "active" && Date.now() < deadline) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 50))
|
||||
current = await api.experimental.schedule.get({ scheduleID: createdID })
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 150))
|
||||
const lines = (await fs.readFile(marker, "utf8")).trim().split("\n").filter(Boolean)
|
||||
expect(current.status).toBe("done")
|
||||
expect(lines).toEqual(["x"])
|
||||
})
|
||||
}).pipe(Effect.scoped),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user