Skip to content

feat(tasks): read database time, and the emitted time of awaited events - #109

Open
psteinroe wants to merge 1 commit into
mainfrom
feat/db-time
Open

psteinroe wants to merge 1 commit into
mainfrom
feat/db-time

Conversation

@psteinroe

Copy link
Copy Markdown
Owner

Adds public APIs for database time, so consumers that enforce absolute deadlines no longer call the private pgconductor._private_current_time().

Reading the time

  • conductor.now(): Promise<Date> reads _private_current_time() exactly: database time, following the test clock / fake offset. Each call is one query.
  • ctx.now(): Date returns the worker clock the context already has. That is local time corrected by the sampled database offset, the same clock time windows, ctx.checkpoint() and cron use. It needs no round trip and follows createTestClock through the Conductor clock option.
  • Neither is memoized. The docs recommend ctx.step() for a value that is stable across replays.

emittedAt on awaited events

ctx.waitForEvent(), subscription wait() and ctx.waitForAny() event branches now return { name, payload, emittedAt: Date }. emittedAt is the dispatch execution's created_at, the timestamp dispatch already compares with the wait's expiry. Dispatch stores it in the wait's step result, so replays return the same value, and the context revives the stored string as a Date. The change is additive (ReceivedEvent<TDef> is exported). Steps stored before this change have no emittedAt. Under the in-place migration policy that is not handled.

Verification

  • testing.test.ts (public API): with the test clock set and then advanced, conductor.now() and ctx.now() follow it.
  • wait-for-event.test.ts: under fake and real time, emittedAt from waitForEvent and from a waitForAny event branch equals the event's stored created_at, and a retry replays the same value. A deadline test approves an event emitted before an absolute deadline and rejects one emitted after the clock advances past it.
  • Type tests cover emittedAt on all three APIs and the types of ctx.now() / conductor.now(). Existing exact-equality assertions on stored events and winners now include emittedAt.
  • just lint, just format, pnpm typecheck and pnpm test (71 files, 619 tests) pass.

Docs: api/task-context.md (ctx.now(), emittedAt), api/conductor.md (conductor.now()) and the events guide (crafting-tasks/triggers.md, new "Event Times" section with a deadline example). AGENTS.md is updated.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant