Conversation
Adds a production-ready job runner that executes deferred work outside of the request path. Key components: - DB migration: new `jobs` table with state, timestamps, visibility timeout - Trait-based job handlers with registry for dynamic dispatch - Async worker supervisor supporting configurable concurrency and graceful shutdown - Exponential back-off w/ jitter for automatic retries - Visibility-timeout guard to avoid duplicate processing - Structured logs across the entire job lifecycle - Unit + integration tests for back-off, registry, state transitions, retries, and concurrency control - Docs: AGENTS.md now lists `cargo run --bin worker` usage Breaking change: - Requires running `make db-migrate` before starting workers Usage: - Start worker: `cargo run --bin worker` - Demo jobs: `cargo run --example job_runner_demo`
Closed
7 tasks
…o be working right now
Owner
Author
|
Merging with failing tests, will be fixed in another commit. JWTs seems to be randomly failing |
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.
Background Job Runner Implementation
Summary
This PR implements a complete background job runner system that enables reliable processing of deferred work outside the HTTP request path. The system provides configurable concurrency, automatic retry logic with exponential backoff, graceful shutdown, and comprehensive observability.
Addresses #20
Amp Thread: https://ampcode.com/threads/T-5b8f4b05-3ae3-4b3f-8c71-b3ad64e36d21
Changes Made
Detailed Changes
jobstable with state management, visibility timeouts, retry tracking, and worker coordinationExampleJobHandlerand demo program for testingTesting
make test)Test Commands Run
Test Coverage Added:
#[sqlx::test]Code Quality
make fmt)make lint)make check)Database Changes
make preparerun after schema changesMigration Details:
20250824104647_update_jobs_table.up.sqljob_statusenum:'done'→'succeeded'payload,max_attempts,backoff_seconds,visibility_till,reserved_by,created_at,updated_atkindfrom enum to text for extensibilityBreaking Changes
Breaking Changes Details
Database Migration Required:
make db-migratebefore starting workersjob_statusenum values (renames'done'to'succeeded')Job Entity Changes:
Jobentity updated with new fields matching database schemaDeployment Notes
Special Deployment Steps
Database Migration: Run
make db-migrateto apply schema changesWorker Configuration: Optional environment variables for worker tuning:
WORKER_CONCURRENCY=4(default: 4)WORKER_POLL_INTERVAL_MS=1000(default: 1000ms)WORKER_VISIBILITY_TIMEOUT_SECS=300(default: 5 minutes)WORKER_BASE_BACKOFF_SECS=30(default: 30s)Worker Deployment:
cargo run --bin workerDocumentation
Documentation Updates:
AGENTS.mdwith new commands:cargo run --bin worker(starts background job processor)cargo run --example job_runner_demo(enqueues example jobs)Architecture Overview
Core Components:
graph TD A[Producer<br/>API/cron] --> B[(jobs table<br/>PostgreSQL)] B --> C[Worker Pool N<br/>1 task = 1 job] B --> D[Fetcher<br/>polling] D --> C classDef producer fill:#2d3748,stroke:#4a5568,color:#fff classDef database fill:#1a365d,stroke:#2b6cb0,color:#fff classDef worker fill:#2d5016,stroke:#38a169,color:#fff classDef fetcher fill:#744210,stroke:#d69e2e,color:#fff class A producer class B database class C worker class D fetcherJob State Machine:
stateDiagram-v2 [*] --> queued queued --> running : worker picks up job running --> succeeded : job completes successfully running --> failed_retry : job fails (attempts < max) failed_retry --> queued : retry with backoff running --> failed : job fails (attempts >= max) failed --> [*] succeeded --> [*] state failed_retry { [*] --> calculating_backoff calculating_backoff --> scheduled_retry scheduled_retry --> [*] }Key Features:
Reviewer Checklist
Additional Notes
Future Extensibility:
Production Readiness:
Testing the Implementation:
make db-up && make db-migratecargo run --example job_runner_democargo run --bin worker