Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/serve-schedules-restart.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vercel/board': patch
---

`vb serve` now sends a schedule that was pending when it stopped: on its first request after a restart it arms every pending schedule again, so one that came due while it was down sends once and a repeating one keeps going. Before, they stayed `next due` forever.
10 changes: 9 additions & 1 deletion packages/board/src/scheduler/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const MAX_DELAY = 2 ** 31 - 1
/** The local scheduler, which `stop()` stops: its timers cleared, nothing pending. */
export type LocalScheduler = A2Scheduler & { stop(): void }

/** The local schedulers made in this process, whose timers a restart drops. */
const local = new WeakSet<A2Scheduler>()

/** Whether `scheduler`'s timers live in this process, so whoever opens the log next arms them again. */
export const inProcess = (scheduler: A2Scheduler | undefined): boolean => scheduler !== undefined && local.has(scheduler)

export function localScheduler(): LocalScheduler {
let servers: readonly DrainableServer[] = []
// The same task asked for twice runs once, as a queue's deduplication would.
Expand All @@ -33,7 +39,7 @@ export function localScheduler(): LocalScheduler {
else await deliverSchedulerClear(server, task)
}

return {
const scheduler: LocalScheduler = {
async schedule(task) {
// Once stopped, what's asked for stays in the log: whoever opens it next catches up.
if (stopped) return
Expand Down Expand Up @@ -68,4 +74,6 @@ export function localScheduler(): LocalScheduler {
return async () => new Response(null, { status: 204 })
},
}
local.add(scheduler)
return scheduler
}
16 changes: 15 additions & 1 deletion packages/board/src/server/schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { indexOf, ref, scheduleRef, viaOfRecord } from './feed.ts'
import { waitUntil } from './wait-until.ts'
import { storedPrincipal } from './registry.ts'
import { ownedActor } from './actors.ts'
import { inProcess } from '../scheduler/local.ts'

/** Vercel Queues delays at most 6 days: a send further out than this goes in hops. */
export const MAX_HOP_MS = 5 * 86_400_000
Expand Down Expand Up @@ -333,7 +334,7 @@ export function createSchedules(options: { store?: A2Store | undefined; schedule
* after the takeover, or missed by a takeover that failed partway, still gets a timer here.
*/
async function takeOver(): Promise<void> {
if (deployment === null) return
if (deployment === null) return inProcess(options.scheduler) ? rearm() : undefined
const read = await session.state(schedulesReducer)
let { state } = read
if (state.takeover?.deployment !== deployment) {
Expand All @@ -350,6 +351,19 @@ export function createSchedules(options: { store?: A2Store | undefined; schedule
if (schedule.next !== null) await arm(`takeover-${since}`, schedule.id, schedule.sent, schedule.next)
}

/**
* A scheduler in this process (`vb serve`'s) drops its timers on a restart: arm every
* pending schedule's next send again, under a name of this start's own, since a name an earlier
* start used may already have sent its timer's event. A send already made stays made: `due`
* skips a timer for a send that isn't next.
*/
const started = Date.now()
async function rearm(): Promise<void> {
const { state } = await session.state(schedulesReducer)
for (const schedule of Object.values(state.byId))
if (schedule.next !== null) await arm(`restart-${started}`, schedule.id, schedule.sent, schedule.next)
}

/**
* The first call on an instance starts the arming catching up, and doesn't wait: on the first
* deploy with it, every schedule from before gets its next send armed here. The first call that
Expand Down
26 changes: 26 additions & 0 deletions packages/board/test/local.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,32 @@ describe('local()', () => {
}
})

it('sends a schedule after a restart, though the timer its creation armed went with the process', async () => {
const dir = await scratch()
const first = local({ dir })
const before = board({ backend: first })
const root = (await before.post('dev', 'deploy')).id
await before.schedule({ re: root }, { text: 'did it land?', in: '1s' })
await first.close()

// Restarted twice, past its time: it sends once.
for (let start = 0; start < 2; start++) {
const again = local({ dir })
const team = board({ backend: again })
try {
await vi.waitFor(
async () => {
expect((await team.thread(root)).replies.map((m) => m.text)).toEqual(['did it land?'])
expect(await team.schedules()).toEqual([])
},
{ timeout: 4000 },
)
} finally {
await again.close()
}
}
})

it("keeps one board per name whatever its team: there's one team here", async () => {
const dir = await scratch()
const acme = local({ dir })
Expand Down