featherweight.client¶
client ¶
JobClient: submit a job, follow it, cancel it, and read its result, from any process.
The client and the worker are independent: the queue and the database are all that connects them. The client writes the job row and puts a message on the queue; it never runs a job. The result is read from the user's result store, because featherweight keeps no results of its own. See docs/specs/submitted-jobs.md.
JobClient ¶
JobClient(
db: Database,
queue: JobQueue,
results: ResultStore
| None
| Literal["default"] = "default",
observers: Sequence[Observer] = (),
identity_type: Any = None,
)
Submits jobs to a queue and follows them. results is the user's ResultStore, where the worker wrote the outputs; the
default is the sqlite store of a SqliteDatabase, and with another database there is none, so pass one (or None, and
result has nothing to read).
submit
async
¶
submit(
job: type[Job] | type[AgentBase],
input: BaseModel,
*,
resumable: bool = False,
resume_from: str | None = None,
job_id: str | None = None,
parent_attempt_id: str | None = None,
parent_tool_call_id: str | None = None,
identity: Any = None,
) -> str
Create the job and put it on the queue; returns its id. job is a Job class, or a plain (non-streaming) agent
class. The input must be an instance of the job's input model (TypeError otherwise).
resume_from is a failed job to resume: a new job that points to it, whose finished steps are reused when it runs
(the old job must have been resumable, or ValueError says nothing was kept). If the queue refuses the message, the
job is marked FAILED and the error is raised.
parent_attempt_id is set by a runner that starts the job as a background job: the attempt that started it. It is stored.
parent_tool_call_id is the tool call that started it: it goes to the observers (so that a trace nests the job under
the call) and is stored nowhere.
identity is who the job is for. It goes in the message as JSON, and the worker rebuilds it with its own identity_type; it is
stored nowhere. With an identity_type the client checks it first (TypeError); without one it accepts any Pydantic model.
status
async
¶
status(job_id: str) -> JobRow
The job's row. LookupError for a job that does not exist.
cancel
async
¶
cancel(job_id: str) -> bool
Cancel a job. A queued job becomes CANCELLED (its message stays on the queue: the worker sees the status when it
arrives, and skips it). A running job gets cancel_requested, and its worker stops it. Returns False for a job that is
already final, or does not exist.
fail
async
¶
fail(job_id: str, error_type: str = DEAD_LETTERED) -> bool
End a job that will never run: its message was dead-lettered by a queue that gave up on it. Call it from your dead-letter
handler. The job becomes FAILED (a failed event) with this error type, in one atomic step, only if it is not running:
it is still INIT, or RUNNING with an expired lease (its worker was lost). Returns False for a job that is running, is
already final, or does not exist. The user resumes it, as any failed job.
result
async
¶
The output of a job that succeeded, read from your result store and parsed into the job's output model; None while
there is none (the job is not SUCCESS, the store has nothing, or results is None). LookupError for a job that
does not exist. It never reads our tables, which hold no results.
events ¶
events(
job_id: str, after: int = 0
) -> AsyncGenerator[JobEvent, None]
The events of the job after after: the stored ones, then each as it happens, ending at a final one. Send it as SSE
with sse.encode.
wait
async
¶
wait(job_id: str, timeout: float | None = None) -> JobRow
The job once it is final. TimeoutError after timeout seconds. A convenience for scripts and tests: a web handler
should return the job id and let the caller follow events.