Datenbank-Port mit SQLite und Job-Queue #44

Merged
torben merged 5 commits from 16-sqlite-database-port into main 2026-12-04 21:05:25 +00:00
2 changed files with 68 additions and 0 deletions
Showing only changes of commit 3e001e5ae4 - Show all commits

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
"""Job: a unit of queued work and its lifecycle state."""
from dataclasses import dataclass
from datetime import datetime
from enum import StrEnum
from uuid import UUID
type JsonValue = str | int | float | bool | list[JsonValue] | dict[str, JsonValue] | None
type JsonObject = dict[str, JsonValue]
class JobState(StrEnum):
QUEUED = "queued"
RUNNING = "running"
DONE = "done"
FAILED = "failed"
@dataclass(frozen=True, slots=True, kw_only=True)
class Job:
id: UUID
kind: str
payload: JsonObject
state: JobState
attempts: int
enqueued_at: datetime # aware, UTC
result: JsonObject | None = None
reason: str | None = None
+40
View File
@@ -0,0 +1,40 @@
"""Port: a durable queue of jobs."""
from typing import Protocol
from uuid import UUID
from chemenu.domain.job import Job, JsonObject
class JobNotFoundError(Exception):
"""No job with the given id exists."""
class InvalidTransitionError(Exception):
"""The job is not in a state that allows the requested change."""
class JobQueue(Protocol):
def enqueue(self, kind: str, payload: JsonObject) -> Job:
"""Add a queued job; ValueError if the payload is not valid JSON."""
...
def claim(self) -> Job | None:
"""Atomically move the oldest queued job to running; None if there is none."""
...
def complete(self, job_id: UUID, result: JsonObject) -> Job:
"""Move a running job to done.
ValueError if the result is not valid JSON; the job stays running, so the
caller must call `fail`.
"""
...
def fail(self, job_id: UUID, reason: str) -> Job: ...
def get(self, job_id: UUID) -> Job: ...
def recover(self) -> list[Job]:
"""Requeue or fail jobs left running; only a starting worker calls this."""
...