Skip to content

featherweight.events

events

Following a job: its events, as an async stream, and waiting for it to end.

The database writes an event in the transaction of every status change (see Database). Here they are read: - in the process that wrote them, the database publishes each event at commit, so a listener hears it at once, with no polling; - from another process, the events table must be read, so each Database object has one reader task that polls it, with a growing delay, for every open stream together (one query, however many streams), and fans the events out. A stream reads the stored events first and listens second, and drops what it has already given, so it misses nothing and gives nothing twice. See docs/specs/submitted-jobs.md.

EventHub

EventHub(db: Database)

The listeners of one Database object, and the one reader task that serves the ones in other processes.

publish

publish(event: JobEvent) -> int

Give an event to the streams that follow its job and have not had it. Returns how many had it new.

hub_for

hub_for(db: Database) -> EventHub

The hub of a database object, made on first use.

job_events async

job_events(
    db: Database, job_id: str, after: int = 0
) -> AsyncGenerator[JobEvent, None]

The events of a job, after the sequence number after: the stored ones first, then each as it happens, ending after a final one (succeeded, failed or cancelled). A stream that starts late replays what it missed, and one that reconnects passes the seq of the last event it had. LookupError for a job that does not exist.

wait_for_job async

wait_for_job(
    db: Database, job_id: str, timeout: float | None = None
) -> JobRow

The job, once it is final (SUCCESS, FAILED or CANCELLED). It reads the status, and if the job is not final it follows the events, so there is no gap between looking and listening. TimeoutError after timeout seconds, LookupError for a job that does not exist. A convenience for scripts and tests: a web handler should return the job id.