feat(outbox): transactional outbox + relay (AddCaliberOutbox, RelayHost) (#36)#61
Merged
Conversation
…st) (#36) Transactional-outbox mode behind the documented one-liner: builder.Services.AddCaliberWebhooks(o => o.UseEntityFramework<AppDbContext>()); // in AppDbContext: protected override void OnModelCreating(ModelBuilder b) => b.AddCaliberOutbox(); - AddCaliberOutbox(ModelBuilder): maps the thin caliber_outbox table onto the caller's model (they own its migration); columns snake_case. created_at stored as UTC ticks so the relay's ORDER BY translates on SQLite (which can't ORDER BY a DateTimeOffset) and Postgres alike. - OutboxPublisher<TContext> (scoped IWebhookPublisher): PublishAsync STAGES a row into the caller's scoped DbContext and returns — no SaveChanges. The caller's SaveChangesAsync commits it atomically with their business data. No-magic-SaveChanges per design #6. - RelayProcessor + RelayHost<TContext> (BackgroundService): drains committed outbox rows oldest-first, fans out to matching endpoints (as-of-relay-time), inserts via the existing IMessageStore.AddAsync (idempotent on (event_id, endpoint_id)), then deletes the drained rows. Crash-safety rests on that idempotency — a crash between insert and delete re-fans-out and no-ops on retry. Mirrors DispatcherHost's PeriodicTimer/TimeProvider drain-while-full + transient-survival idiom. - UseEntityFramework<TContext>(): wires the EF message/endpoint stores against TContext's own database (provider + connection read from the caller's context; SQLite in v1, Postgres arrives with #38), the scoped outbox publisher, the relay, and a shared-schema initializer that CREATE-TABLEs Caliber's messages/endpoints into the caller DB when absent (idempotent; production Postgres ships migrations in #38). - Core: IngestionManager publisher registration → TryAddSingleton, so a provider's StoreConfigurator can supply an outbox-mode publisher that wins. Both standalone (direct PublishAsync → messages) and outbox (stage → relay) modes work behind the same IWebhookPublisher; the guarantee tier surfaces at the store-config line, never a silent downgrade. 11 new tests (203 total, 0 warnings, net8+net10): staging-doesn't-commit, payload/clock stamp, fan-out (single + multi endpoint), idempotent re-drain (crash-before-delete), unmatched-event drains, oldest-first ordering, empty-outbox, null guards, and UseEntityFramework DI wiring. Docs: transactional-outbox.md relay section reconciled to the idempotency-based crash-safety the code actually implements. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Transactional-outbox mode — the heart of M2 — behind the documented one-liner:
AddCaliberOutbox(ModelBuilder)— maps the thincaliber_outboxtable onto the caller's model (they migrate it via their normaldotnet efworkflow).created_atis stored as UTC ticks so the relay'sORDER BYtranslates on SQLite (which cannotORDER BYaDateTimeOffset) and Postgres alike.OutboxPublisher<TContext>(scopedIWebhookPublisher) —PublishAsyncstages a row into the caller's scopedDbContextand returns; noSaveChanges. The caller'sSaveChangesAsynccommits it atomically with their business data (no-magic-SaveChanges, design feat(config): AddCaliberWebhooks options + validation #6).RelayProcessor+RelayHost<TContext>(BackgroundService) — drains committed outbox rows oldest-first, fans out to matching endpoints (as-of-relay-time), inserts via the existingIMessageStore.AddAsync(idempotent on(event_id, endpoint_id)), then deletes the drained rows. Crash-safety rests on that idempotency: a crash between insert and delete re-fans-out and no-ops on retry. MirrorsDispatcherHost'sPeriodicTimer/TimeProviderdrain-while-full + transient-survival idiom.UseEntityFramework<TContext>()— wires the EF stores againstTContext's own database (provider + connection read from the caller's context; SQLite in v1, Postgres lands with feat(storage): durable Postgres store + shipped migrations (FOR UPDATE SKIP LOCKED) #38), the scoped outbox publisher, the relay, and a shared-schema initializer thatCREATE TABLEs Caliber'smessages/endpointsinto the caller DB when absent (idempotent; production Postgres ships reviewed migrations in feat(storage): durable Postgres store + shipped migrations (FOR UPDATE SKIP LOCKED) #38).IngestionManagerpublisher registration →TryAddSingleton, so a provider'sStoreConfiguratorcan supply an outbox-mode publisher that wins.Both standalone (direct
PublishAsync→messages) and outbox (stage → relay) modes work behind the sameIWebhookPublisher; the guarantee tier surfaces at the store-config line, never a silent downgrade.Design note (reviewers)
The design doc floated a single-transaction relay (insert
messages+ delete outbox in one tx). This PR implements the issue-#36 acceptance criteria literally — fan-out via the existingIMessageStore+ idempotent insert — which gives the same crash-safety through idempotency rather than a shared transaction, and letsmessagesreuse the exact store the dispatcher already claims from.transactional-outbox.mdis reconciled to match.Closes
Closes #36. Part of the #8 M2 epic. (SQLite tier #37 already merged via #59/#60; Postgres + migrations is #38.)
Test plan
dotnet build -c Release— 0 warnings (net8 + net10)dotnet test -c Release— 203/203 (+11: staging-doesn't-commit, fan-out single/multi, idempotent re-drain, unmatched drains, oldest-first, empty, null guards, DI wiring)🤖 Generated with Claude Code