featherweight.worker¶
worker ¶
The worker: the process that runs submitted jobs.
It receives messages from a queue, runs the job each one names, writes the output through the user's result store, and records the outcome in the run log. It holds no state of its own beyond the tasks it is running: everything else is the database, the queue and the user's store. A failed job is never retried here (the user resumes it). See docs/specs/submitted-jobs.md.
UnknownJob ¶
Bases: Exception
A message named a job that this worker was not given.
InvalidJobInput ¶
Bases: Exception
The input of a message does not fit the input model of its job.
JobLookupError ¶
Bases: Exception
jobs_lookup raised, returned something that is not a job, returned a job with another name than the one asked for, or returned a
job whose hash is not the one recorded when the job was submitted (it changed while the job waited). The job is not run.
InvalidJobIdentity ¶
Bases: Exception
The identity of a message is missing and the worker's identity_type requires one, or does not fit that type, or the worker
declares no type. The job is not run.
JobWorker ¶
JobWorker(
db: Database,
queue: JobQueue,
runner: AgentRunner,
jobs: Sequence[type[Job] | type[AgentBase]],
results: ResultStore
| None
| Literal["default"] = "default",
*,
concurrency: int | None = None,
worker_id: str | None = None,
identity_type: Any = None,
jobs_lookup: JobsLookup | None = None,
)
JobWorker(db, queue, runner, jobs, results): jobs are the Job classes (or plain agent classes) it can run, by name.
results is where the output of a job is written (a ResultStore; the default is a sqlite store in the run log's file
for a SqliteDatabase, and none for another database; None keeps nothing). concurrency is how many jobs run at once
(FTW_WORKER_CONCURRENCY, else 10).
The job's lease is how long, in seconds, the worker's claim on a job lasts without being renewed (FTW_JOB_LEASE, 60 by default).
While it runs jobs, the worker renews the lease of each, and extends the queue's lease of its message, every FTW_HEARTBEAT
seconds (a third of the lease by default). A worker that lost a job (another one took it over) stops it and writes nothing more
to it. How long it waits for a message and how often it asks about cancellations are FTW_WORKER_RECEIVE_TIMEOUT and
FTW_WORKER_CANCEL_POLL (docs/specs/configuration.md): the settings that are not concurrency have no argument.
worker_id names the worker in jobs.worker_id (a new id by default).
identity_type is the type of the identity of a run (docs/specs/identity.md): the worker rebuilds the identity of a message with it and
gives it to the run. A type that does not allow None makes a message without an identity fail its job (InvalidJobIdentity), and so
does an identity that does not fit the type, or one when no type is declared. The job is not run, and not retried.
jobs_lookup is a fallback for jobs that are not declared in code (an agent that a platform's user defined, kept in the platform's own
storage): async def jobs_lookup(name, job_hash, identity) returns the job class (or a plain agent class) for the name, or None.
jobs are tried first; the lookup is called only for a name jobs does not have, with the job_hash recorded when the job was
submitted (None for a job that has none) and the rebuilt identity (None if there is none). The worker checks that what it returns has the
name that was asked for and the recorded hash, and caches nothing. None is UnknownJob; a lookup that raises, a result that is
not a job, another name and another hash fail the job with JobLookupError. jobs may be empty. See docs/specs/submitted-jobs.md.
worker = JobWorker(db, queue, runner, jobs=[Summarise])
await worker.run() # until `await worker.stop()` is called, from another task
run
async
¶
run() -> None
Receive and run jobs until stop() is called. If the task that runs this is cancelled, the running jobs are
cancelled and returned to the queue, and the cancellation is raised.
stop
async
¶
stop(grace: float = 30.0) -> None
Take no new messages, wait up to grace seconds for the running jobs, then cancel the ones still running and
return their messages to the queue (nack with requeue=True): those jobs go back to INIT, and run again.
cancel ¶
cancel(job_id: str) -> bool
Cancel a job this worker is running, at once (in this process). False if it is not running here. A client in
another process sets cancel_requested instead, and the worker finds it within cancel_poll seconds.