feat(gpu): fast orphan recovery — graceful release + 60s sweep (#114)

So work an agent orphaned gets picked back up quickly, three layers:
- GpuJobService.release(): a graceful agent stop hands its still-leased jobs back
  to pending instantly (POST /api/gpu/jobs/release), no waiting out the lease.
- GpuJobService.recover_orphaned() + recover_orphaned_gpu_jobs Celery task on a
  60s beat: resets expired leases (a hard-crashed agent) to pending and keeps the
  queue counts honest even when nothing is leasing.
- Lease TTL 300→180s: still well above any single job (a capped-frame video embed
  is tens of seconds, and a live worker heartbeats), but a hard crash recovers
  faster once the sweep fires.

Tests: release returns-to-pending (token-scoped), recover_orphaned resets only
expired leases, release API round-trip.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ttrj5P7upUTueSfoJcxEqa
This commit is contained in:
2026-06-29 19:07:40 -04:00
parent 614b6bc52a
commit 2cb0427868
6 changed files with 149 additions and 6 deletions
+35
View File
@@ -123,3 +123,38 @@ async def test_fail_requeues_until_cap(db):
assert await svc.fail("agent-1", job.id, "boom again") is True
await db.commit()
assert (await db.get(GpuJob, job.id)).status == "error"
@pytest.mark.asyncio
async def test_release_returns_to_pending(db):
img = await _img(db, "01" + "a" * 62)
svc = GpuJobService(db)
await svc.enqueue(img.id, "ccip")
await db.commit()
job = (await svc.lease("agent-1"))[0]
await db.commit()
assert await svc.release("other", [job.id]) == 0 # not this token's lease
assert await svc.release("agent-1", [job.id]) == 1 # graceful hand-back
await db.commit()
fresh = await db.get(GpuJob, job.id)
assert fresh.status == "pending" and fresh.lease_token is None
@pytest.mark.asyncio
async def test_recover_orphaned_resets_only_expired(db):
img1 = await _img(db, "02" + "a" * 62)
img2 = await _img(db, "03" + "a" * 62)
svc = GpuJobService(db)
await svc.enqueue(img1.id, "ccip")
await svc.enqueue(img2.id, "ccip")
await db.commit()
expired, fresh = await svc.lease("dead", batch_size=2)
# One lease is in the past (orphaned), the other still valid.
expired.lease_expires_at = datetime.now(UTC) - timedelta(minutes=10)
await db.commit()
assert await svc.recover_orphaned() == 1
await db.commit()
assert (await db.get(GpuJob, expired.id)).status == "pending"
assert (await db.get(GpuJob, fresh.id)).status == "leased" # untouched