Skip to content

Event processing

The reactor is the Runtime infrastructure that processes committed Events. Application authors register Listeners; the reactor claims Events, matches them to subscribers and runs the subscriber that invokes the Listeners. The PostgreSQL reactor, the SQLite reactor and the test reactor each do this differently; the immediate path runs Listeners without any of them.

A subscriber is an internal reactor consumer registered with addSubscriber. It names itself and registers either a contract ({ prn }) or a guard function that decides which Events it receives. Every reactor shares the registry in src/alpyne/reactor/protected/subscriber.ts; an Event is delivered to every matching subscriber.

The Alpyne subscriber (src/alpyne/subscriber/alpyne) is the subscriber that runs Listeners. From the Definition it builds two maps: Event PRN to Listeners, and parent Template PRN plus Event PRN to parent Listeners. It registers a guard that matches any Event PRN with at least one entry in either map. For each Event it receives, it opens one write execution under a system auth context in the Event’s Workspace, with idempotencyScope set to its package name, and inside it:

  1. Loads the Item the Event was published against.
  2. Runs every Listener registered on the Event PRN, in registration order.
  3. Walks up the Item’s parent chain, stopping below the Workspace Item, and runs every parent Listener registered for that parent’s Template and the Event PRN.

An idempotency rejection from that execution is logged as a warning and treated as already applied; any other error propagates to the reactor.

The PostgreSQL reactor (src/alpyne/reactor/postgresql) processes one Event per tick inside one database transaction. It selects the oldest unclaimed event row and locks it with FOR UPDATE SKIP LOCKED. A row qualifies when it was created at or before the tick time and within the previous 1.01 days, and its Workspace is not paused. A row locked by another reactor is skipped, so two reactors never process the same Event.

With the candidate locked, the reactor:

  • Counts the subscribers matching the Event PRN. When none match, one update marks the candidate and every other unclaimed, unlocked Event with the same PRN and Workspace as claimed and completed, and the reactor counts them as no-ops.
  • Validates the Event’s lineage. An Event whose lineage exceeds the depth bound is marked claimed and completed and logged as invalid.
  • Runs each matching subscriber in turn. A subscriber error is logged and counted, and the remaining subscribers still run.
  • Sets claimed_at and completed_at on the row after processing, then commits.

Because the subscriber’s own write execution commits in its own transaction, the reactor’s mark can fail after the subscriber succeeded. Processing is therefore at least once at the reactor level; the Alpyne subscriber’s idempotency scope makes Listener execution for one Event at most once.

A kill switch read from dynamic configuration every ten seconds pauses processing for every reactor. The local reactor wrapper (src/alpyne/reactor/local) runs the PostgreSQL reactor on a tick loop. The loop backs off to at most 250 milliseconds between empty ticks and returns to one millisecond as soon as an Event is processed. The worker package (src/alpyne/reactor/worker) coordinates reactors in worker threads through a shared atomic claim lock, so only one thread claims at a time.

The SQLite reactor (src/alpyne/reactor/sqlite) follows the PostgreSQL loop with a lease in place of a row lock, because SQLite has no FOR UPDATE SKIP LOCKED. A claim writes a claimToken and the claiming process id onto the row and renews claimedAt on a one-minute heartbeat. A claim becomes reclaimable when its heartbeat is five minutes stale and the owning process is no longer alive, or unconditionally after sixty minutes. The 1.01-day window and the paused-Workspace exclusion are computed in application code rather than SQL.

The test reactor (src/alpyne/reactor/test) claims up to 50 Events per tick from the platform service’s in-memory Event stream. It skips paused Workspaces, Events more than a day old, future-dated Events and Events that already carry completedAt. It splits the claimed Events into batches of the same Event PRN and Workspace, delivers each batch to every matching subscriber through a system auth context, and records a completed receipt per Event with each subscriber’s result. A subscriber error is counted and logged, and the remaining subscribers still receive the batch.

The harness registers the Alpyne subscriber on whichever reactor it is given and ticks the reactor and the platform’s schedules together through tickAll.

immediate runs Listeners without the reactor. In every Runtime it writes the Event with claimedAt and completedAt already set, inside the publishing execution, so no reactor ever claims it. It then loads the Item and runs each Listener registered on the Event PRN inline, under the publishing execution’s auth context and within its transaction. A Listener error during immediate propagates to the caller and stops the remaining inline Listeners; the commit outcome follows the caller. A failed publishing execution rolls back the Event and the Listeners’ writes with the rest of the attempt. Parent Listeners do not run on the immediate path.