feat(#941): Prevent duplicate market-watcher jobs after failover - #950
Merged
greatest0fallt1me merged 1 commit intoAug 29, 2026
Conversation
… failover - Add durable market_watcher_jobs table with unique job_key idempotency boundary - Implement MarketWatcherJobCoordinator with atomic lease ownership and failover recovery - Prevent duplicate watcher notifications upon worker failovers, timeouts, and retries - Add bounded exponential backoff retry policy and terminal failure handling - Add market watcher BullMQ queue, worker, metrics, and comprehensive test suite
|
@chiomailekuba Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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
Closes #941
This PR implements a production-ready, durable, lease-coordinated market-watcher job processing system to prevent duplicate job execution and duplicate watcher notifications across worker failovers, timeouts, restarts, and concurrent executions.
Changes Made
Durable Database Model (
market_watcher_jobs):marketWatcherJobstable insrc/db/schema.tswith unique constraint onjobKey(${marketId}:${eventType}:${eventRef}) serving as the database-level idempotency boundary.leaseToken(UUID),leaseUntilexpiration timestamp, monotonicattemptcounter, execution timestamps,watchersNotifiedcount, and status transitions (pending,running,retryable,succeeded,failed).drizzle/migrations/0029_market_watcher_jobs.sql.Lease Coordination & Failover Recovery (
MarketWatcherJobCoordinator):claimJob: Uses atomic conditional UPDATE to acquire leases only if job is pending, ready for retry (nextAttemptAt <= now), or if a previous worker's lease expired (leaseUntil < now).markSucceeded: Commits completion conditioned onid,leaseToken, andrunningstatus. If a timed-out worker attempts to commit after a failover worker has taken over the lease, the stale commit is safely rejected.markFailed: Bounded retries with exponential backoff delay calculation (retryDelayMs). When attempts reachmaxAttempts, transitions to terminalfailedstatus and prevents further queue re-enqueues.recoverExpiredLeases: Reclaims stale or abandoned in-flight leases back toretryablefor failover processing.enqueueMarketWatcherJob: Idempotent enqueue helper ensuring duplicate events do not create multiple database records or redundant queue jobs.Queue & Worker Architecture:
marketWatcherQueueandmarketWatcherQueueName("market-watcher-jobs") insrc/queue/index.ts.MarketWatcherWorkerinsrc/workers/marketWatcherWorker.tswith concurrency support, BullMQ event hooks, failure logging, and graceful shutdown handling.Metrics & Observability:
src/metrics/registry.ts:marketWatcherJobRunsTotal,marketWatcherJobRetriesTotal,marketWatcherLeaseConflictsTotal, andmarketWatcherNotificationsTotal.Test Coverage:
tests/marketWatcherJobService.test.ts(14 unit tests) covering deterministic job keys, bounded exponential backoff, idempotency, lease claiming, failover recovery, stale commit rejection, retry delays, and retry exhaustion.tests/marketWatcherWorker.test.tsverifying worker lifecycle, idempotency, and graceful stop.tests/marketWatchers.test.tsfor clean test execution.Acceptance Criteria Verification