Repository navigation
fix: Recover orphaned market-watcher jobs and actually start the watcher worker - #990
Merged
greatest0fallt1me merged 1 commit intoOct 19, 2026
Conversation
|
@innocentharuna 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
I fixed #983 in the code, but nothing has been run. I couldn't run anything here, so the new tests, lint and
tscare all unverified until CI runs.Each in-scope item is implemented, but I took a few liberties:
heartbeatis an optional field on the handler input, not a second argument.defaultMarketWatcherHandleralready uses its second argument for the database. The handler also takes an optional batch size so the tests can use 3 batches without 1,500 watchers.recoverExpiredLeases(now, maxAttempts)now marks a jobfailedonce its attempts are used up, so a job that keeps crashing workers can't loop forever.<jobKey>:recover:<attempt>:<leaseUntilMs>, so it can't collide with the sweeper's:recover:<attempt>id.startMarketWatcheris exported fromsrc/workers/marketWatcher.tsso the drain order can be tested without real signals.What I changed:
src/services/marketWatcherJobService.ts):renewLeaseandlistDueRetryableto the repo, plus an exportedLeaseLostError.heartbeat()is passed to the handler. Losing the lease makes the job returnskipped, with nosucceededcommit.process()now re-adds the job delayed untillease_until + 1swhen a claim fails on a live lease.src/jobs/marketWatcherLeaseSweeper.ts):recoverExpiredLeasesand picks up dueretryablejobs.jobId: "<jobKey>:recover:<attempt>".jobKey.src/workers/marketWatcher.tsstarts the worker and the sweeper and drains on SIGTERM/SIGINT. I added themarket-watcherandmarket-watcher:devscripts topackage.json.MARKET_WATCHER_SWEEP_INTERVAL_MS(default 30000) and the countersmarket_watcher_jobs_recovered_totalandmarket_watcher_lease_renewals_total{outcome}.docs/job-queue.md.The PR still needs the sequence diagram or timeline for the crash-recovery case, and the test output and coverage summary. I didn't write them.
{"summary": "Added lease recovery for orphaned market-watcher jobs, lease renewal during fan-out, a delayed re-add on live-lease conflict, and a runnable worker entrypoint. New `src/jobs/marketWatcherLeaseSweeper.ts` (Redis SET NX PX lock, recoverExpiredLeases, re-enqueue of due retryable jobs with no live BullMQ job under `<jobKey>:recover:<attempt>`). The repo gained `renewLease` and `listDueRetryable`, and `recoverExpiredLeases` now fails jobs that hit maxAttempts. The handler gets `heartbeat()` and throws `LeaseLostError`, which the coordinator turns into `skipped`. A claim that fails on a live lease now schedules a delayed re-add at lease_until+1s. Added `src/workers/marketWatcher.ts` (worker + sweeper, SIGTERM/SIGINT drain), npm scripts `market-watcher` and `market-watcher:dev`, the `MARKET_WATCHER_SWEEP_INTERVAL_MS` env var, the metrics `market_watcher_jobs_recovered_total` and `market_watcher_lease_renewals_total{outcome}`, and docs in `docs/job-queue.md`.", "test_plan": "Nothing was run locally. CI should run `npm test -- tests/marketWatcherJobService.test.ts tests/marketWatcherWorker.test.ts tests/marketWatcherLeaseSweeper.test.ts`, then `npm run lint`, `npx tsc -p tsconfig.json --noEmit` and `npm test` with coverage. Tests cover: crash then sweeper recovery then success (row 1, distinct `:recover:` id although the original job id is completed, row 2); delayed re-add on live-lease conflict (3); 3-batch fan-out with heartbeat renewal (4); renewal failure stops before the next batch and returns skipped with no succeeded co ## Changed files - `docs/job-queue.md` - `package.json` - `src/config/env-schema.ts` - `src/metrics/registry.ts` - `src/services/marketWatcherJobService.ts` - `tests/marketWatcherJobService.test.ts` - `tests/marketWatcherWorker.test.ts` - `src/jobs/marketWatcherLeaseSweeper.ts` - `src/workers/marketWatcher.ts` - `tests/marketWatcherLeaseSweeper.test.ts` ## Test plan Repository CI. This PR was created as a draft by the issue solver bot. It will remain a draft until repository CI passes. Closes #983