diff --git a/packages/opencode/src/scheduler/index.ts b/packages/opencode/src/scheduler/index.ts index e1fea455da..d876c50426 100644 --- a/packages/opencode/src/scheduler/index.ts +++ b/packages/opencode/src/scheduler/index.ts @@ -4,20 +4,30 @@ import { Log } from "../util/log" export namespace Scheduler { const log = Log.create({ service: "scheduler" }) + export type Scope = "instance" | "global" export type Task = { id: string interval: number run: () => Promise + scope?: Scope } type Timer = ReturnType + type Entry = { + tasks: Map + timers: Map + } + + const create = (): Entry => { + const tasks = new Map() + const timers = new Map() + return { tasks, timers } + } + + const shared = create() const state = Instance.state( - () => { - const tasks = new Map() - const timers = new Map() - return { tasks, timers } - }, + () => create(), async (entry) => { for (const timer of entry.timers.values()) { clearInterval(timer) @@ -28,8 +38,10 @@ export namespace Scheduler { ) export function register(task: Task) { - const entry = state() + const scope = task.scope ?? "instance" + const entry = scope === "global" ? shared : state() const current = entry.timers.get(task.id) + if (current && scope === "global") return if (current) clearInterval(current) entry.tasks.set(task.id, task) diff --git a/packages/opencode/src/snapshot/index.ts b/packages/opencode/src/snapshot/index.ts index bac807d29c..46c97cf8df 100644 --- a/packages/opencode/src/snapshot/index.ts +++ b/packages/opencode/src/snapshot/index.ts @@ -18,6 +18,7 @@ export namespace Snapshot { id: "snapshot.cleanup", interval: hour, run: cleanup, + scope: "instance", }) } diff --git a/packages/opencode/src/tool/truncation.ts b/packages/opencode/src/tool/truncation.ts index 16aea14e7c..84e799c131 100644 --- a/packages/opencode/src/tool/truncation.ts +++ b/packages/opencode/src/tool/truncation.ts @@ -27,6 +27,7 @@ export namespace Truncate { id: "tool.truncation.cleanup", interval: HOUR_MS, run: cleanup, + scope: "global", }) }