feat(jobs): Postgres job queue, worker mode and seat sweep (#29) #95

Merged
amiryahaya merged 11 commits from feat/29-job-queue-worker into main 2026-10-25 14:53:40 +01:00
Owner

Closes #29. A Postgres job queue, KWS_MODE=worker, and the seat-hold sweep that checkout (#26) depends on (ADR 0010).

What's in it

  • Migration 0016: jobs and append-only dead_jobs
  • internal/jobs + pgstore: FOR UPDATE SKIP LOCKED claims under a 5 min lease, fenced Complete/Fail (id + worker + attempt), retries with backoff (5 s x 2^n, cap 15 min, 5 attempts), dead letters, poison-job dead-lettering, queue depth
  • Runner: handler registry, concurrency limit, panic recovery, schedules deduped by key, graceful and bounded shutdown
  • seats.ReleaseExpired: sweep through the same settle code as Release (skips payment-pending and locked holds), batches of 50
  • Worker mode: seats.sweep every 1 min, idempotency.purge every 1 h (7-day window), /healthz, /readyz, /metrics (queue depth), exits non-zero if the runner dies
  • Docs: ADR 0010, CLAUDE.md "Jobs" section, spec and ADR 0009 updates

Verification

  • make cover: 94.2% total (minimum 80%)
  • go test ./... -race, make lint, make contract: clean
  • Dual-pass review (ponytail + correctness): Must-fix clear, no way to oversell seats
  • 4 workers claiming 50 jobs claim each exactly once; superseded-worker and stale-attempt fencing tested

Needs attention

  • The worker must be deployed and running before #26 goes live. deploy/ has no api service yet, so no worker service was added.
  • A job interrupted by shutdown spends an attempt (dead-lettered if it was the last).
  • Follow-ups: batch the idempotency purge (one DELETE under the 5 s statement timeout); dead_jobs replay/retention tool; alert on increase(kws_jobs_dead[15m]) > 0, not > 0.
  • KWS_HTTP_ADDR defaults to :8080 in both modes: API and worker collide when run side by side locally.

🤖 Generated with Claude Code

Closes #29. A Postgres job queue, `KWS_MODE=worker`, and the seat-hold sweep that checkout (#26) depends on (ADR 0010). ## What's in it - Migration 0016: `jobs` and append-only `dead_jobs` - `internal/jobs` + `pgstore`: `FOR UPDATE SKIP LOCKED` claims under a 5 min lease, fenced `Complete`/`Fail` (id + worker + attempt), retries with backoff (5 s x 2^n, cap 15 min, 5 attempts), dead letters, poison-job dead-lettering, queue depth - Runner: handler registry, concurrency limit, panic recovery, schedules deduped by key, graceful and bounded shutdown - `seats.ReleaseExpired`: sweep through the same `settle` code as `Release` (skips payment-pending and locked holds), batches of 50 - Worker mode: `seats.sweep` every 1 min, `idempotency.purge` every 1 h (7-day window), `/healthz`, `/readyz`, `/metrics` (queue depth), exits non-zero if the runner dies - Docs: ADR 0010, CLAUDE.md "Jobs" section, spec and ADR 0009 updates ## Verification - `make cover`: **94.2%** total (minimum 80%) - `go test ./... -race`, `make lint`, `make contract`: clean - Dual-pass review (ponytail + correctness): Must-fix clear, no way to oversell seats - 4 workers claiming 50 jobs claim each exactly once; superseded-worker and stale-attempt fencing tested ## Needs attention - **The worker must be deployed and running before #26 goes live.** `deploy/` has no api service yet, so no worker service was added. - A job interrupted by shutdown spends an attempt (dead-lettered if it was the last). - Follow-ups: batch the idempotency purge (one DELETE under the 5 s statement timeout); `dead_jobs` replay/retention tool; alert on `increase(kws_jobs_dead[15m]) > 0`, not `> 0`. - `KWS_HTTP_ADDR` defaults to `:8080` in both modes: API and worker collide when run side by side locally. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
fix(jobs): review wave (handler deadline, sweep batch 50, logging, docs) (#29)
All checks were successful
ci / changes (pull_request) Successful in 16s
ci / clients (pull_request) Has been skipped
ci / api (pull_request) Successful in 13m53s
ci / ci-gate (pull_request) Successful in 4s
2e317dda70
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
primatekuntech/kws!95
No description provided.