featherweight.db¶
db ¶
The run log: what featherweight keeps of the orchestration, and the abstract class that a database implements.
The log is metadata only: jobs, and one row per attempt of an agent, with status, tokens, timing, the launch tree and hashes.
It never holds an input, an output, a message or the text of an error (see docs/specs/persistence.md). Database is the
abstract class; SqliteDatabase (local development) and PostgresDatabase (deployment) implement it. Records go in and rows
come out as Pydantic models, so the same constraints hold whatever the database is.
AttemptRecord ¶
Bases: _Model
What is known when an attempt is created. There is no input in it: only its hash.
AttemptFinish ¶
Bases: _Model
What is known when an attempt ends. There is no output and no error text in it: only hashes and the error's type.
JobOwner ¶
Bases: _Model
Who owns a running job: the worker, and which execution of the job it is. The writes of a running job are conditional on both, in the statement itself, so that a worker that lost the job cannot change it.
JobLost ¶
Bases: Exception
A write for a job that is not this worker's any more: another worker took it over, or it ended, or it was put back in the queue. Nothing was written. The worker that gets it stops the job and writes nothing more to it.
JobEvent ¶
Bases: _Model
A change in the life of a job. Metadata only: no content.
Database ¶
Bases: ABC
The run log. Subclass it to keep the log in a database of your own: write every abstract method, and run
featherweight.testing.check_database against it. The schema is documented in docs/specs/persistence.md.
Rows come back in the order they were made. Timestamps are timezone-aware datetimes: convert what your database stores.
connect
abstractmethod
async
¶
connect() -> None
Open the database and bring its schema up to date (the migrations).
create_job
async
¶
create_job(
name: str,
job_id: str | None = None,
*,
resume_from: str | None = None,
resumable: bool = False,
job_version: str | None = None,
job_hash: str | None = None,
input_hash: str | None = None,
parent_attempt_id: str | None = None,
) -> str
Create a job. resumable=True keeps the outputs of its steps (in the runner's result store), so that a later job
can resume it; it is decided here, before the pipeline runs.
With resume_from, the job resumes that one: its runs may reuse the successful steps of it. The old job must have
been resumable, or nothing was kept and ValueError says so. A job that resumes another is resumable itself, so that
a third attempt is possible.
insert_job
abstractmethod
async
¶
insert_job(
job_id: str,
name: str,
resumed_from: str | None,
resumable: bool,
job_version: str | None,
job_hash: str | None,
input_hash: str | None,
parent_attempt_id: str | None = None,
) -> str
Write a new job in status INIT, and return its id. The rules are checked in create_job.
set_job_status
abstractmethod
async
¶
set_job_status(
job_id: str,
status: Status,
*,
error_type: str | None = None,
error_hash: str | None = None,
owner: JobOwner | None = None,
) -> None
Set the status of a job. The error is kept only for FAILED. RUNNING sets started_at; going back to INIT (a job
that returns to the queue) clears it and its owner; a final status ends its lease. LookupError for a job that does not
exist.
With owner, the write is conditional, in the same statement, on the job being RUNNING and having that worker_id
and executions: if it does not, nothing is written (no status, no event) and JobLost is raised.
request_job_cancel
abstractmethod
async
¶
request_job_cancel(job_id: str) -> bool
Ask for a job that is not final to be cancelled: set cancel_requested and write a cancel_requested event, in one
transaction. Returns False, and changes nothing, for a job that does not exist or is already final. Asking twice writes
one event. The job's worker does the cancelling.
start_job
abstractmethod
async
¶
start_job(
job_id: str,
worker_id: str | None = None,
lease: float | None = None,
) -> bool
Mark a queued job RUNNING (started_at, a running event) only if it is still INIT and has no
cancel_requested, in one atomic step, and say whether it did. It is what a worker calls to take a job: it and
cancel_queued_job cannot both succeed, so a job is never cancelled and run at once.
The same step records the owner: worker_id, executions one higher, and, if lease (seconds) is given, a
lease_expires_at that far ahead (the database's own clock, where it has one). A job without a lease (one that
runs in the process that started it) has no lease to lose.
take_over_job
abstractmethod
async
¶
take_over_job(
job_id: str, worker_id: str, lease: float
) -> bool
Take a job whose worker was lost: only if it is RUNNING and its lease has expired, in one atomic step, and say
whether it did. The step writes a lost event (the job went back to INIT) and then starts the job for worker_id
(a running event, executions one higher, a new lease), in one transaction. False means the lease is live (a duplicate
delivery, or a slow worker that is still the owner), or the job is final, or another worker won. Where the database has
a clock, it is the database's that decides when the lease expired.
fail_queued_job
abstractmethod
async
¶
fail_queued_job(
job_id: str, error_type: str, error_hash: str
) -> bool
Mark a job FAILED (a failed event with the error) only if it is not running: it is still INIT, or it is
RUNNING with an expired lease (its worker was lost), in one atomic step, and say whether it did. It is for a job whose
message will never be delivered again (dead-lettered): JobClient.fail calls it. False means the job is running, or is
final, or does not exist.
renew_job_leases
abstractmethod
async
¶
renew_job_leases(
worker_id: str, jobs: Mapping[str, int], lease: float
) -> set[str]
A worker's heartbeat: in one in-place update, move lease_expires_at of each job in jobs (job id to the
executions the worker holds) lease seconds ahead, for those that are still RUNNING with this worker_id and
that executions. Returns the ids that were renewed: the others are lost. No rows are added, and nothing else changes.
owns_job
abstractmethod
async
¶
owns_job(job_id: str, owner: JobOwner) -> bool
Whether the job is RUNNING and still has this owner. A cheap read, for a check before a step that cannot be made
conditional (writing a result to the user's store); it is not atomic with what follows.
cancel_queued_job
abstractmethod
async
¶
cancel_queued_job(job_id: str) -> bool
Mark a job CANCELLED (a cancelled event) only if it is still INIT, in one atomic step, and say whether it
did. False means it was taken by a worker (or is final, or does not exist): the caller then asks for a running job to
be cancelled with request_job_cancel.
list_cancel_requested
abstractmethod
async
¶
list_cancel_requested(job_ids: Sequence[str]) -> set[str]
Which of these jobs have cancel_requested set and are not final yet, in one query. It is what a worker asks, about
the jobs it is running, to learn that a client wants one of them cancelled.
list_job_events
abstractmethod
async
¶
list_job_events(
job_id: str, after: int = 0, limit: int | None = None
) -> list[JobEvent]
The events of a job after the sequence number after, in order.
poll_job_events
abstractmethod
async
¶
poll_job_events(
job_ids: Sequence[str], after: int = 0
) -> list[JobEvent]
The events of any of these jobs after after, in order, in one query. It is what the one reader of a process asks
for, whatever the number of streams that are open.
create_attempt
abstractmethod
async
¶
create_attempt(run: AttemptRecord) -> str
Write an attempt in status INIT (waiting for a slot), and return its id. A job_id that does not exist is an
error.
start_attempt
abstractmethod
async
¶
start_attempt(attempt_id: str) -> None
The attempt holds a slot: status RUNNING, and started_at is now.
finish_attempt
abstractmethod
async
¶
finish_attempt(attempt_id: str, end: AttemptFinish) -> None
End the attempt. LookupError for a run that does not exist.
find_reusable_attempt
abstractmethod
async
¶
find_reusable_attempt(
job_id: str,
agent_hash: str,
input_hash: str,
identity_hash: str | None,
before_execution: int | None = None,
) -> AttemptRow | None
The first top-level run (depth 1) of job_id that succeeded with this agent, this input and this identity, or None. The
identity matches by its hash, and None (no identity) matches only a run that had none. With
before_execution, only a run of an earlier execution of the job (job_execution below it): that is how a takeover
looks in its own job without finding the runs of the execution that asks.
job_has_other_identity
abstractmethod
async
¶
job_has_other_identity(
job_id: str, identity_hash: str | None
) -> bool
Whether job_id already has a run whose identity differs from identity_hash, of any status and depth. None (no identity)
differs from any hash and the reverse, so a job that ran with none is not for an identity, and the other way round. It is
how a job stays for one identity (docs/specs/identity.md).
list_attempts
abstractmethod
async
¶
list_attempts(
*, job_id: str | None = None, run_id: str | None = None
) -> list[AttemptRow]
Runs in the order they were made, optionally of one job and/or one run.