gabriel / musehub public
job_execution.py python
23 lines 1.3 KB
Raw
sha256:cb4b05e9d6caea5f351e3cb452ee6a1db8856169328ad96bfba9ee8e04b8afa6 fix(jobs): fence every execution of a job; an expired or su… Sonnet 5.5 minor ⚠ breaking 4 days ago
1 """Helpers that put a background job in the state a worker's claim leaves it in, for tests that stand in for a worker (musehub#8 execution fencing).
2
3 Terminal updates (``complete_job`` / ``skip_job`` / ``block_job`` / ``fail_job``) are conditional on ``status='running'`` and the claim's ``attempt``.
4 """
5 from __future__ import annotations
6
7 from sqlalchemy import select, update
8 from sqlalchemy.ext.asyncio import AsyncSession
9
10 from musehub.db.musehub_jobs_models import MusehubBackgroundJob
11
12
13 async def start_execution(session: AsyncSession, job_id: str, attempt: int | None = None) -> int:
14 """Mark the job running as a claim would (attempt + 1 unless given) and return this execution's attempt."""
15 current = (await session.execute(select(MusehubBackgroundJob.attempt).where(MusehubBackgroundJob.job_id == job_id))).scalar_one()
16 attempt = current + 1 if attempt is None else attempt
17 await session.execute(update(MusehubBackgroundJob).where(MusehubBackgroundJob.job_id == job_id).values(status="running", attempt=attempt))
18 await session.flush()
19 return attempt
20
21
22 async def current_attempt(session: AsyncSession, job_id: str) -> int:
23 return (await session.execute(select(MusehubBackgroundJob.attempt).where(MusehubBackgroundJob.job_id == job_id))).scalar_one()
File History 1 commit
sha256:cb4b05e9d6caea5f351e3cb452ee6a1db8856169328ad96bfba9ee8e04b8afa6 fix(jobs): fence every execution of a job; an expired or su… Sonnet 5.5 minor ⚠ 4 days ago