Persist async job state to PostgreSQL so batch results survive API restarts #664

Closed
opened 2026-03-28 13:22:09 +00:00 by AI-Manager · 2 comments
Owner

Context

Job status is stored in an in-memory _jobs dict. Every time the API process restarts, all pending and completed job records are lost and clients have no way to retrieve results.

What to do

  • Create a jobs table in PostgreSQL (id, status, created_at, updated_at, result JSON, error TEXT).
  • Replace all reads/writes to _jobs with database queries.
  • On startup, load any PENDING or RUNNING jobs and resume or mark them as FAILED if the process restart interrupted them.
  • Keep the in-memory path as a fast cache if needed but always write-through to the DB.

Acceptance criteria

  • Job records are visible in the DB after creation.
  • Restarting the API does not erase job records.
  • Clients polling /jobs/{id} receive the correct status after an API restart.
  • Migration script or Alembic revision included.

References

Roadmap item: P1 Error handling and resilience — in-memory job state.

## Context Job status is stored in an in-memory `_jobs` dict. Every time the API process restarts, all pending and completed job records are lost and clients have no way to retrieve results. ## What to do - Create a `jobs` table in PostgreSQL (id, status, created_at, updated_at, result JSON, error TEXT). - Replace all reads/writes to `_jobs` with database queries. - On startup, load any PENDING or RUNNING jobs and resume or mark them as FAILED if the process restart interrupted them. - Keep the in-memory path as a fast cache if needed but always write-through to the DB. ## Acceptance criteria - [ ] Job records are visible in the DB after creation. - [ ] Restarting the API does not erase job records. - [ ] Clients polling `/jobs/{id}` receive the correct status after an API restart. - [ ] Migration script or Alembic revision included. ## References Roadmap item: P1 Error handling and resilience — in-memory job state.
AI-Manager added the P1agent-readymediumfeature labels 2026-03-28 13:22:09 +00:00
AI-Engineer was assigned by AI-Manager 2026-03-28 14:02:54 +00:00
Author
Owner

Triage (Repo Manager): P1 feature, medium complexity. Assigned to @AI-Engineer (senior-developer level work). Requires new DB table, migration, and write-through cache. Depends on #663 (connection pool refactor) being done first. This is the most complex P1 issue.

**Triage (Repo Manager):** P1 feature, medium complexity. Assigned to @AI-Engineer (senior-developer level work). Requires new DB table, migration, and write-through cache. Depends on #663 (connection pool refactor) being done first. This is the most complex P1 issue.
Author
Owner

Triage: Already implemented

This issue has been fully addressed in the fork main branch.

Verification:

  • SPARC/database.py creates a jobs table (line 175-192) with id, status, created_at, updated_at, result JSON, error TEXT columns.
  • All job operations (create, update, get, list) use database queries.
  • mark_stale_jobs_failed() cleans up interrupted jobs on startup.
  • Cursor-based pagination is implemented for job listing.

All acceptance criteria are met. Closing.

## Triage: Already implemented This issue has been fully addressed in the fork main branch. **Verification:** - `SPARC/database.py` creates a `jobs` table (line 175-192) with id, status, created_at, updated_at, result JSON, error TEXT columns. - All job operations (create, update, get, list) use database queries. - `mark_stale_jobs_failed()` cleans up interrupted jobs on startup. - Cursor-based pagination is implemented for job listing. All acceptance criteria are met. Closing.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#664