Skip to content

fix(events): enqueue shift and coverage jobs atomically in transactions - #220

Draft
theosiemensrhodes wants to merge 1 commit into
mainfrom
fix/neuron-n07-durable-domain-events
Draft

theosiemensrhodes wants to merge 1 commit into
mainfrom
fix/neuron-n07-durable-domain-events

Conversation

@theosiemensrhodes

Copy link
Copy Markdown
Collaborator

Summary

Shift creation, shift cancellation, and coverage mutations previously committed domain state before fire-and-forget notification or scheduled-job enqueue calls (void jobService.run(...) / void notificationEventService.notify*(...)). An enqueue failure or process crash could leave committed domain changes without reminders or notifications, and retrying a shift cancellation returned early once canceled = true was already persisted.

This stacked PR builds directly on feat/transactional-email-outbox (#209) and its transactional pg-boss enqueueing (JobService.run(..., { transaction }) and JobService.cancelPending(..., { transaction })) without introducing a competing outbox table or migration:

  • Transactional notification & check job intent (NotificationService, NotificationEventService, NotifyParams):
    • Propagate transaction through NotificationEventService reads/writes and NotificationService.notify(...) into JobService.run("jobs.process-notification", ..., { transaction, singletonKey, idempotencyKey }).
    • Add transactional cancelPending support for withdrawn coverage request notifications.
    • Scope coverage.filled / coverage.filled-personal idempotency keys by (coverageRequestId, requestingVolunteerUserId, coveredByVolunteerUserId) so unassigning and re-fulfilling a coverage request still delivers notifications for the new fulfillment while retries remain idempotent.
  • Transactional shift lifecycle (ShiftService, check-shift-notifications.job.ts, process-notification.job.ts):
    • createShift always executes inside a PostgreSQL transaction (caller-provided tx for ClassService.publishClass, or a new transaction for standalone calls) and atomically enqueues delayed jobs.check-shift-notifications (reminder / no-checkin) jobs with deterministic idempotencyKey and singletonKey.
    • cancelShift locks the shift row FOR UPDATE, updates cancellation state, cancels pending reminder/no-checkin jobs, and enqueues the shift.cancelled notification job in one transaction.
    • deleteShift deletes the shift and cancels pending reminder/no-checkin jobs atomically.
    • checkShiftNotificationsJob and processNotificationJob honor cooperative cancellation (isCancelled) and run check reads + notification enqueue in a transaction.
  • Transactional coverage lifecycle (CoverageService):
    • createCoverageRequest validates shift eligibility, inserts the request, and enqueues coverage.requested + coverage.available jobs in one transaction.
    • cancelCoverageRequest locks the request FOR UPDATE, marks it withdrawn, and cancels pending coverage.requested / coverage.available jobs atomically.
    • fulfillCoverageRequest locks the request FOR UPDATE, checks shift roster assignment within the same transaction, resolves the request, and enqueues coverage.filled + coverage.filled-personal jobs atomically.
    • unassignCoverage locks the request FOR UPDATE and reopens it atomically.

Delivery Semantics

  • PostgreSQL domain state and pg-boss job intent commit or roll back atomically in one transaction.
  • Retries are idempotent via row locking (FOR UPDATE), deterministic native pg-boss job IDs (idempotencyKey), and notification.idempotencyKey conflict handling.
  • External transport delivery (SMTP in jobs.send-email) cannot be atomic with PostgreSQL job completion and remains explicitly at-least-once.

Dependencies

Validation

  • SKIP_ENV_VALIDATION=1 pnpm exec next typegen && pnpm typecheck (tsc --noEmit): passed
  • SKIP_ENV_VALIDATION=1 pnpm build: passed
  • pnpm exec prettier --check on all touched files: passed
  • Full test suite (pnpm test:run) against an isolated disposable PostgreSQL database (neuron_test_n07_7f0e56d2, dropped after run): 12 test files, 131 tests passed, including new failure-injection integration tests in src/test/integration/durable-domain-events.test.ts covering:
    • Commit boundary (rollback on enqueue failure across cancelShift, createShift, classService.publishClass, createCoverageRequest, fulfillCoverageRequest; transactional job cancellation on cancelShift, deleteShift, cancelCoverageRequest; concurrent fulfillment serialization; unassign + re-fulfill)
    • Dispatch boundary (mid-fanout rollback + retry, SMTP retry without duplicating in-app notifications, check-shift-notifications retry + attendance/coverage-swap handling, idempotent re-execution)
    • Restart boundary (worker stop/restart recovery of committed shift creation, coverage request, coverage fulfillment, shift cancellation, and mid-pipeline SMTP retry without duplicate notifications)
  • Note: pnpm lint / eslint remains blocked by the pre-existing Next 16 / ESLint flat-config incompatibility in this repository (unchanged by this PR).

@theosiemensrhodes
theosiemensrhodes marked this pull request as draft November 23, 2026 20:47
Base automatically changed from feat/transactional-email-outbox to main November 23, 2026 22:33
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