Submitted jobs¶
Status: implemented, including several workers (epic #103): the queue's lease (#104), the job lease, the heartbeat and the owner check (#105), takeover (#106), the job timeout (#107), and the guide, the example and the skill reference (#108). #72 (the Job class, JobContext, the job columns and CANCELLED), #73 (the queue), #74 (job events and SSE), #75 (the client), #76 (the worker, with the compare-and-set steps for taking and cancelling a job), #77 (the @job decorator) and #78 (the guide, the example and the skill reference) are done. It is tracked in epic #71, and each part has
its own issue (listed at the end). When a part is built, this page says so, and the guide docs/submitted-jobs.md
shows how to use it. The jobs_lookup fallback is implemented too (#199, in epic #190).
It builds on the persistence spec (the Database, the result store, the rule that featherweight keeps no
data). It changes jobs (the job columns, the CANCELLED status, the job events),
the runner (a job context runs agents in the job) and agents (jobs).
- Goal
- The principle
- The job
- The queue
- The client
- The worker
- Jobs that are not declared in code
- Several workers
- Cancelling
- Job events
- Decisions
- Later
- Not supported
- Tests
- The work
Goal¶
A job is a declared class that runs in your process (run_job) or on a worker, by the same code. It should be something a client can
submit, from anywhere, that a separate worker picks up and runs, with the client able to follow it, cancel it, and read
its result. The client and the worker are independent: the queue and the database are their only connection, so they can live
in different processes, or on different machines. It covers the queue, the client and the worker, and several workers on one queue
(leases, a dead worker: several workers). Background jobs have their own spec.
The principle¶
featherweight passes what the user gives to the job's agents, and writes the result into the user's result store. It stores no inputs and no results of its own (the rule):
- The input travels in the queue message, as JSON. It can also be a reference (an id, a URL) that the job looks up itself.
- The result goes to the user's
ResultStore, and the client reads it from there. Our tables hold the job's status, its timing, hashes and its events, and nothing of the content.
The job¶
A job is a declared class, like an agent:
class Summarise(Job):
name = "summarise"
version = "1"
input_model = SummariseInput # Pydantic
output_model = SummariseOutput # Pydantic
async def run(self, ctx: JobContext, input: SummariseInput) -> SummariseOutput:
result = await ctx.run(Summariser, SummaryInput(url=input.url)) # an agent of this job
...
Jobis an abstract class: one that missesruncannot be built, andname,versionand the two models are checked when the class is defined.JobContextgivesrunner,job_id, andrun(...)andrun_many(...), which arerunner.runandrunner.run_manywith the job's id, so the agents of a job share it (and its trace, and its resume).job_hashis the SHA-256 of the name, the version, and the JSON schemas of both models: it says that the job changed.- A worker knows its jobs by name:
JobWorker(..., jobs=[Summarise, ...]). There is no global registry. - A plain agent can be a job: a non-streaming agent class can be submitted directly. Its name is the job name, its input model
the input, its output model the output, and
agent_hashstands in for the job hash. A streaming agent cannot be a job (it streams to a person). timeout: how long one execution of the job may run, in seconds. The default is 600 (ten minutes), andNonemeans no limit. It is checked when the class is defined (a positive number, orNone), it is not part ofjob_hash, and@job(timeout=...)sets it. An agent used as a job has the default. See the timeout.@jobis sugar: it builds aJobclass from an async function, taking the name from the function (orname=), the version"1"(orversion=), and the models from the annotations ofinputand the return type.run_jobruns aJobclass in your process, with the same code as the worker (running a job class). A job whose task is cancelled isCANCELLED.
The queue¶
JobQueue is an abstract class that connects a client to a worker:
class JobMessage(BaseModel): # frozen
job_id: str
job_name: str
input: dict[str, Any] # JSON, or references inside it
trace_context: dict[str, str] = {} # the W3C trace context of the submitter, if an observer gave one (see below)
identity: dict[str, Any] | None = None # the identity of the run as JSON, if it has one ([identity](identity.md#across-the-queue))
identity_kind: str | None = None # which model the identity is: the name of its class; set whenever there is an identity
class Delivery(BaseModel): # frozen
message: JobMessage
token: str # opaque: the implementation's own
delivery_count: int = 1 # how many times this message has been delivered, this one included
class JobQueue(ABC):
async def enqueue(self, message: JobMessage) -> None
async def receive(self, timeout: float) -> Delivery | None
async def ack(self, delivery: Delivery) -> None
async def nack(self, delivery: Delivery, *, requeue: bool) -> None
async def extend(self, delivery: Delivery) -> None # renew the lease of a received message
The contract: delivery is at-least-once. A received message is invisible to other receivers until it is acknowledged or
nacked. nack(requeue=True) makes it available again; nack(requeue=False) drops it, and a broker may dead-letter it. Messages
come out in the order they went in where the implementation can. receive returns None after the timeout. Calls can be
concurrent. A lease (built: #104): a received message is invisible to other receivers for a limited
time, and not for ever. If it is neither acknowledged, nacked nor extended before the lease ends, it becomes available again,
with a higher delivery_count, so that a worker that died does not hold a message for ever. extend renews the lease, and a stale delivery (one whose message was delivered again since) changes nothing. These are
what real brokers already have (SQS's visibility timeout, ChangeMessageVisibility and receive count; a Redis stream's pending list), so an implementation
over one is thin. How many redeliveries are allowed is the queue's business. featherweight.testing.check_queue(make_queue) runs the contract against any queue; make_queue(lease) makes a fresh queue with that lease in seconds, and the kit uses a short one to see a message come back.
What we ship:
- SqliteQueue(db): a job_queue table in the run log's sqlite file, for local development, and our only product
implementation. A message is claimed atomically, ack deletes the row, nack requeues or deletes it. With the lease (FTW_QUEUE_LEASE, 60 seconds, and FTW_QUEUE_MAX_DELIVERIES, 3: configuration): SqliteQueue(db); a message whose lease ends is delivered again (ready or claimed with an expired lease, in the order it went in), and one that was delivered FTW_QUEUE_MAX_DELIVERIES times and whose lease ends again is marked dead, kept in the table for inspection, and logged (queue_message_dead). Failing its job (DeadLettered) is part of #106, which is not built yet. A delivery's token names its claim (<seq>:<delivery count>), so that a stale ack, nack or extend cannot touch a message that was delivered again. A nack(requeue=True) is a deliberate return, and does not count against max_deliveries. The payload is in the table
until it is acknowledged, on the same terms as the sqlite result store: sqlite is for local development.
- MemoryQueue(lease=60), in featherweight.testing, for tests: the same lease semantics (pass a very short lease in a test), and no limit on redeliveries.
There is no Redis, SQS or Postgres queue: how jobs travel in a deployment is the user's decision, and the abstract class and its kit are how they check theirs. Dead-lettering is the user's broker's business.
The trace context (#93, built) is how a worker's span joins the trace of
the client that submitted the job (observers and OpenTelemetry). It is empty unless the client has an observer
that gives one, and it travels in the message only: it is stored in no table. Every queue carries it, and check_queue checks that it does.
The identity (identity) is how a tool on a worker knows who the run is for. It is the identity as JSON and its kind (the name of its class), and it too travels in the message only. Every queue
carries both, and check_queue checks that a queue returns them unchanged, so a queue that drops a field fails the kit. SqliteQueue keeps them in its job_queue table until
the message is acknowledged, on the same terms as the input. The queue is trusted: how to secure it (and a queue that signs its messages) is the deployment's, see identity.
The client¶
JobClient(db, queue, results, observers=[...], identity_type=None) submits and follows jobs, from any process. The observers are heard on submit only: each can return a
carrier (the trace context) that travels in the message (the queue), and an observer that raises changes nothing.
client = JobClient(db, queue, results)
job_id = await client.submit(Summarise, SummariseInput(url="https://..."))
async for event in client.events(job_id):
... # queued, running, succeeded
row = await client.wait(job_id, timeout=60) # the same stream, until a final status
output = await client.result(Summarise, job_id) # read from YOUR result store
| Method | |
|---|---|
submit(job, input, *, resumable=False, resume_from=None, job_id=None, identity=None) |
Validates the input against the job's model, creates the job row (name, job_hash, input_hash, resumable, resumed_from, and a queued event), then puts a JobMessage on the queue. If the enqueue fails the job is marked FAILED and the error is raised |
status(job_id) |
The JobRow |
cancel(job_id) |
See cancelling. Returns whether there was anything to cancel |
result(job, job_id) |
The output, read from the user's ResultStore under ResultKey(job_id, job_hash, input_hash) and parsed into the job's output model, or None while there is none. It never reads our tables, which hold no results |
events(job_id, after=0) |
The events of the job |
wait(job_id, timeout=None) |
The JobRow once the job is final (SUCCESS, FAILED or CANCELLED); TimeoutError otherwise. A convenience for scripts and tests: a web handler should return the job id, not wait |
identity= is the identity of the run (identity). With identity_type the client checks it before it creates a job; the message carries it and its kind, and the worker
rebuilds it as the model the kind names.
Resuming is submit(job, input, resume_from=failed_job_id): a new job points to the old one, and the worker's runner reuses the
finished steps exactly as run_job(..., resume_from=...) does. Nothing resumes a failed job by itself.
The worker¶
JobWorker(db, queue, runner, jobs, results, concurrency, jobs_lookup=None, identity_type=None) is the process that runs jobs. It holds no state of its own: it is the
database, the queue and the user's store. jobs are the jobs declared in code; jobs_lookup is an optional fallback for jobs that are not
(below).
await worker.run() loops:
- Receive a message. If the job is already
CANCELLED, acknowledge and skip it. If the job name is unknown (not injobs, andjobs_lookup, if there is one, found nothing), or the input does not validate, or the identity does not (it is missing and the worker'sidentity_typedoes not allowNone, or it does not fit the type, or its kind is not one the type has, or there is no kind:InvalidJobIdentity), mark the jobFAILED(error_type,error_hash) and acknowledge. A job that fails for this is not run, and is not retried. - Start: take the job (
start_job:RUNNING,started_at, arunningevent) and run it as an asyncio task, up toconcurrencyat a time. What the task does isexecute_job, the function that also runs a job forrun_job(running a job class): the worker adds the queue. - Success: write the output through the user's
ResultStoreunderResultKey(job_id, job_hash, input_hash), mark the jobSUCCESS, acknowledge. If the store fails to keep the result, the job isFAILED, because the result is its product (a lost step result only reruns the step; a lost job result is the deliverable).results=Nonekeeps nothing, andclient.resulthas nothing to read. - Failure: mark the job
FAILEDwith the error's type and hash, and acknowledge. A failed job is never retried by the worker: the user resumes it. The failure is a recorded outcome, not a delivery failure, so there is nothing to dead-letter.
Shutdown. await worker.stop(grace) takes no new messages, waits the grace period for running jobs, then cancels the rest and
nacks their messages with requeue=True: those jobs return to INIT (a queued event), the interrupted attempts are CANCELLED,
and a worker (this one, later, or another) runs them again, as a takeover does (several workers): the steps that finished
before the interruption are reused if the job is resumable.
Jobs that are not declared in code¶
A worker is given its jobs when it starts. A platform where users define their own agents, stored in the platform's own database and
built into classes when they are needed, has jobs that a worker could not have been given. jobs_lookup is how the worker finds them.
async def jobs_lookup(name: str, job_hash: str | None, identity: Any | None) -> type[Job] | type[AgentBase] | None: ...
worker = JobWorker(db, queue, runner, jobs=[Summarise], jobs_lookup=jobs_lookup)
- It is a fallback. The worker looks the job name up in
jobsfirst. Ifjobshas the name,jobs_lookupis not called, so declaring jobs in code behaves exactly as without it. Only for a namejobsdoes not have does the worker calljobs_lookup(name, job_hash, identity).jobsmay be empty. - What it gets: the name from the message, the
job_hashrecorded at submit, and the identity the worker rebuilt from the message (Noneif there is none). A lookup that does not need the identity ignores it; one that does can scope what it searches (by tenant, say). - What it returns: a
Jobclass, a plain (non-streaming) agent class, which is made a job as injobs(as_job), orNone.Noneis an unknown job, as before: the job isFAILEDwithUnknownJob, and the message is acknowledged. - What the worker checks about a class it got from
jobs_lookup, before it runs anything: - its name is the name that was asked for (a lookup that returns another job is a bug, and a job is run by its name);
- its
job_hash()is the hash recorded on the job row when the job was submitted. If the definition changed while the job was queued (a user edited their agent), the job is not run as the new definition: it isFAILED, and the user can resubmit. - A job whose row has no recorded hash (one that a client did not make) is not checked against one, and the lookup gets
Nonefor it. - A job that fails either check, or whose
jobs_lookupraises, or that returns something that is not a job, isFAILEDwith the error typeJobLookupError(the message says which), and the message is acknowledged. The worker rebuilds the identity first, because the lookup receives it, so a message whose identity does not fit fails before the lookup is asked. A failed job is not retried by the worker (as always), so a lookup that can fail for a transient reason (a database that is briefly away) retries inside itself. - A lookup that takes too long fails the job. The lookup runs inline in the worker's receive loop, so a lookup that hangs (a database or an object store that does not answer) would stop the worker
from taking any message. It has a timeout,
FTW_JOBS_LOOKUP_TIMEOUTseconds (15 by default; configuration), read when the worker is built and with no argument and no way to switch it off. When it passes, the lookup is cancelled and the job isFAILEDwithJobLookupError("jobs_lookup took longer than N seconds"), the message is acknowledged and the worker goes on to the next one. So a lookup must be safe to cancel, and a message behind a slow lookup waits at most the timeout. A lookup that raises aTimeoutErrorof its own is reported as one that raised, not as one that was too slow. - The name is checked first. A message whose job name does not match the pattern below is not a job: it is
FAILEDwithUnknownJob(the text names the rule, not the value), the message is acknowledged, and neitherjobsnorjobs_lookupis asked. The pattern is only checked when aJobclass is made, so a message that did not come fromsubmit(a forged one, a client of another version or language) could otherwise hand the application's lookup any string, such as../../other-tenant/agent, before the application has had a chance to validate it. The queue is trusted, so this is a safeguard and not a boundary. job_hashis the hash of the job's declaration: for an agent, the whole of its declaration (its prompt, model settings, tools, sub-agents, skills, MCP servers, the system prompt and the schemas of its input and output); for aJobclass, its name, its version and the schemas of its input and output (not the code ofrun: bumpversionwhen that changes). A lookup may ignore it, and then a job queued before an edit fails, which is safe. A lookup that keeps saved versions of a definition uses it to load the exact one. It is made from the content, so a lookup may cache what it built by(name, job_hash)without it going stale. The worker caches nothing.- Names are the application's. The name is the job's key: a worker runs a job by it, and the worker refuses a name that does not (see above). A name must match, whole,
^[A-Za-z][A-Za-z0-9_.-]*$(a newline at the end does not) (a letter first, then letters, digits,_,.and-). It is a plain string that the application chooses: unique in what its lookup searches (with the identity, unique within a tenant is enough), and stable, because a rename breaks the jobs queued under the old name and changes the agent's hash. An agent'snameis also what a model sees when it launches the agent as a sub-agent and what the run log shows, so a readable name and a unique one pull apart: an application that needs ids in the name can use them (agent-7f3a...), and one that wants readable names keeps them unique per tenant. featherweight has nokeyand does not decide which. - The client needs nothing.
submittakes a class: the platform builds it from its definition, the same way, and sends it. - Not for classes that are known. It is a fallback for what the code cannot name. It does not scan for jobs, and it is not a registry.
Several workers¶
Built (epic #103, with one issue for each part: #104 the queue, #105 the job lease and the owner check, #106 takeover, #107 the timeout, #108 the guide). Several workers on one queue already work today: a message is claimed by one receiver, start_job is a
compare-and-set that exactly one worker wins, a cancel is checked by the worker that owns the job, the events have one reader per
process, and a shutdown requeues. What does not work is a worker that dies: its message stays claimed, and its job stays RUNNING
for ever. This section is the recovery from that, and it is all that it is: we do not build a broker, a scheduler or a registry of workers.
- Two leases, with one job each (built, #105). The queue's lease keeps a message from other receivers, and it comes back by itself when the worker
stops renewing it. The job's lease is on the job row (
jobs.worker_id,jobs.lease_expires_at), and is what makes it exclusive: at-least-once delivery can deliver a message twice even without a lease running out, so the job needs a lock of its own. The default lease is 60 seconds (FTW_JOB_LEASE). A job that runs in your process (run_job) has an owner and no lease: nothing renews one and nobody takes it over, so it has none to lose. - The heartbeat (built, #105). One task per worker renews, every third of the lease (20 seconds;
FTW_HEARTBEAT), the job lease of every job it runs (oneUPDATE, in place, no new rows) and the queue lease of each of their messages (extend). It proves that the worker is alive, and not that a job makes progress: that is the timeout. - Takeover (built, #106). A message comes back after its lease ended and reaches a worker. If the job is
RUNNINGand its lease has expired, the worker takes it over with one compare-and-set (Database.take_over_job): the job goes back toINIT(alostevent), and at once toRUNNINGwith the new owner andexecutionshigher by one (arunningevent), in one transaction. If the lease has not expired (a duplicate delivery of a job that is alive), the worker acknowledges the message and leaves the job alone; so it does for a message for a job that this worker is running (its heartbeat can still renew a lease that merely expired, because renewing needs only the ownership). A job whose message never comes back is not found: the queue's redelivery is the only trigger, and there is no sweeper. - Executions.
jobs.executionscounts how many times a worker started the job: 1 for the first, and one more each time it is started again (built, #105: a job that a shutdown put back in the queue is started again as execution 2; a takeover, #106, does the same). (It is notattempt, which is the retry of one agent call.) Each run records the execution that made it (attempts.job_execution, built, #106), and the job's span hasftw.job.execution. The execution comes from the task that runs the job (execute_jobsets it, as it sets who the owner is), and not from the job's row, so that a worker that lost the job and has not noticed yet still records the execution it was. - A takeover reuses the steps that finished (built, #106), if the job is
resumable, with the same mechanism as a resume (the runner): the source of the reuse is this same job, executions before the current one, and not another job. The results are already under this job's key, so nothing is copied forward. A job that is not resumable kept no results, and a takeover reruns it from the start. - The owner is checked on every write (built, #105). The writes of a running job (renewing the lease, the final status and its event) are conditional on
worker_idandexecutionsbeing the worker's own, and on the job beingRUNNING, in the same statement (set_job_status(..., owner=JobOwner(...));Database.owns_jobis the cheap read for the check before a result is written). A worker that was stalled and wakes up after a takeover finds that its write changes no row, which is how it learns that it lost the job. There is no token and no timer in it: the database decides, and the stalled worker's clock does not matter. - A lost job (built, #105) is dropped quietly: the worker cancels the task at once (so that it stops spending), writes nothing to the job,
does not acknowledge the message (the delivery is stale), logs
job_lost job_id=... execution=..., and tells its observers (the job's span ends with error typeJobLost, for tracing only: nothing is written to the job; its attempts endCANCELLED, as for any cancelled task). It keeps running its other jobs. Before the worker writes a result it checks that it still owns the job; that is not atomic, so in a very small window a stalled worker can write a result under the same key as the new owner, for the same input, and the last write wins. The effects that the job's own code already had (a mail sent, an API called) are not undone: a job must be safe to run twice. - The timeout (built: #107). One execution of a job may run for
Job.timeoutseconds (default 600,Nonefor no limit). When it is exceeded the task is cancelled and the job isFAILEDwith error typeJobTimeout; it is not retried (the user resumes it). It applies to a job run byrun_jobtoo, because it is in the sharedexecute_job. Only the limit's own expiry counts: aTimeoutErrorthat the job raises itself, and a cancellation from outside, do not (the job endsFAILEDwith that error, andCANCELLED). The runs that it interrupts endCANCELLED. A heartbeat cannot do this, because a hung job in a healthy worker still heartbeats. - Delivery count.
Delivery.delivery_countsays how many times a message has been delivered. How many redeliveries are allowed is the queue's business: a broker has its own policy and dead-letter queue. Ours,SqliteQueue, allows 3 by default (FTW_QUEUE_MAX_DELIVERIES), then marks the messagedeadand fails the job with error typeDeadLettered. A user's queue that dead-letters a message ends its job withclient.fail(job_id)(built, #106), from its dead-letter handler: a compare-and-set that moves a job that is not running toFAILEDwith error typeDeadLettered(afailedevent): one that is stillINIT, orRUNNINGwith an expired lease (its worker was lost). It returnsFalsefor a job that runs with a live lease, is final, or does not exist. Until it is called, the job stays as it was.SqliteQueuecalls it itself.
Cancelling¶
- A queued job (
INIT):cancelmarks itCANCELLED(acancelledevent), in one atomic step that succeeds only if the job is stillINIT(Database.cancel_queued_job). The worker takes a job with the matching step (Database.start_job:RUNNINGonly if stillINITand no one asked to cancel it). The two cannot both succeed, so a job is never cancelled and run at once. A cancel that loses falls through to a running job's flag. Its message stays on the queue, because brokers cannot remove by id; the worker sees the status when the message arrives, acknowledges it and skips it. - A running job:
cancelsetscancel_requested(acancel_requestedevent). Inside the worker each running job is an asyncio task, so cancelling it istask.cancel(), which is event-driven. In the same process,worker.cancel(job_id)does it at once. From another process, one background task of the worker checks, about every second, which of its own running jobs have the flag, in one query, and cancels them. The job is thenCANCELLEDand its interrupted attempts areCANCELLED. - A final job:
cancelreturnsFalseand changes nothing.
Job events¶
The worker, the client and run_job all change a job's status, and every change is an event, for anyone who wants to follow
a job. Events are metadata only: no content (content goes out only through an observer).
class JobEvent(BaseModel): # frozen
seq: int # monotonic
job_id: str
type: Literal[
"queued", "running", "succeeded", "failed", "cancelled", "cancel_requested", "lost"
] # "lost": a takeover, see several workers
status: Status
error_type: str | None
error_hash: str | None
at: datetime
The database writes the event in the same transaction as the status change (the outbox pattern), not the worker after it: a crash
cannot leave a change without its event, and every change makes exactly one event whoever makes it. insert_job writes queued,
set_job_status writes the event of the new status, and request_job_cancel(job_id) sets the flag and writes cancel_requested.
list_job_events(job_id, after, limit) reads them, and poll_job_events(job_ids, after) reads those of several jobs in one query (what the one reader asks for). These are methods of the Database abstract class, and its conformance kit covers them. insert_job writes queued, and a job going back to INIT writes queued again. After the commit the database publishes the event to the listeners in its own process.
client.events(job_id, after=0) is an async stream: it replays the stored events after a sequence number, then goes live, and
ends after a final event. A listener that starts late misses nothing, and one that reconnects resumes from its last seq.
wait reads the status and, if the job is not final, consumes the stream, so there is no gap between checking and listening.
How it is delivered. In the same process the database wakes the listeners when the transaction commits: instantly, with no polling. Across processes the events table must be read, so one reader task per process polls it with a growing delay (one query for every open stream) and fans out to them: a hundred open connections cost one query. The polling is inside the library, in one place.
SSE is the only streaming protocol. sse.encode(events) turns the stream into server-sent events and sse.HEADERS
has the headers:
id: 12
event: running
data: {"seq":12,"job_id":"…","status":"RUNNING","at":"2026-11-20T10:00:01Z"}
id is the sequence number, so a browser that reconnects sends Last-Event-ID and the stream resumes after it. A comment line goes
out every 15 seconds to keep the connection open, and the stream ends after a final event. featherweight runs no server: the
user's API sends the response (a StreamingResponse in FastAPI, about a dozen lines, in the example). No WebSockets, one job per
stream, no stream of all jobs.
Decisions¶
- Everything is passed, nothing is kept: inputs in the message, results in the user's store, and the metadata in ours.
- A job is a declared class, registered with the worker by name, with a decorator as sugar. An agent can be a job.
jobs_lookupis a fallback, not a registry:jobsis tried first, and the lookup is called only for a namejobsdoes not have. The worker checks the name and the hash of what it returns, and caches nothing. Finding a user's definition and building its class is the platform's code, because where definitions live is its decision, as with the result store and the queue.JobQueueis an abstract class with sqlite as our only product implementation and a conformance kit; the user brings Redis, SQS or anything else.- Client and worker are independent: the queue and the database are their only connection.
- A failed job is not retried by the worker; the user resumes it. A failed job is an outcome, not a delivery failure.
- A lost job result fails the job; a lost step result only reruns the step.
- Events are written by the database, in the transaction of the status change, and streamed with SSE only.
- Polling is centralised, not removed: one reader per process, behind
events(). - Cancel: a queued job is marked and skipped; a running job's flag is checked by the worker about every second.
- Shutdown: a grace period, then cancel and requeue.
- A lease on the queue (built, #104) and one on the job (built, #105): the queue's keeps the message, the job's keeps the job exclusive. Heartbeats are
in-place updates. A lost lease puts the job back to
INIT, andFAILEDcomes only when the user's policy runs out. - A takeover is the same job with
executions+ 1, not a new job: the client holds the id, and its events, its cancel and its result are all keyed by it. A resume is a new job; a takeover is not, and both reuse finished steps with one mechanism. - The write is checked against the owner, not against a clock.
- Job timeout, default ten minutes. Redelivery limits are the queue's, except for ours.
Later¶
- An event channel as an abstract class, like
Database: PostgresLISTEN/NOTIFY, Redis pub/sub or a cloud broker would remove the polling across processes. Pub/sub is vendor-specific and not available everywhere, so we do not write one per vendor now. If it is wanted, it is one abstract class behind the sameevents(), with a conformance kit. - A worker-level limit on redeliveries: the queue's business, so there is none.
- Background jobs: their own spec, built on jobs and the job events.
Not supported¶
- A durable queue we ship beyond sqlite, and a pub/sub channel.
- Automatic retry of a failed job.
- A sweeper for a job whose message never comes back.
- A worker that finds jobs by itself.
jobs_lookupis the platform's code, and the worker scans nothing. - A worker registry, scheduling, priorities, autoscaling, and a broker of our own.
- Fencing the user's stores and the effects of their code: only our own writes are checked.
- Progress with content in events, and a stream of all jobs.
- A server, a router or WebSockets. The example shows an SSE endpoint.
- Pruning events: the table grows, and pruning is the user's for now.
Tests¶
No live model call. Fake models, MemoryQueue, the dict result store.
- The job class: the contract, the hash, an agent as a job, a streaming agent refused,
@job. - The queue:
check_queueagainstSqliteQueueandMemoryQueue, two receivers on one queue. - Events: every status change makes exactly one event, whoever makes it; a late listener replays; the same-process wake-up; a
writer and a reader on one file in two processes; one reader serving many streams; the stream ends at a final event; the SSE
format and
Last-Event-ID; theDatabaseconformance kit on sqlite and Postgres. - Several workers:
check_queuewith a short lease (built, #104: a message comes back with a higher count,extendkeeps it, an acknowledged one does not, a stale delivery changes nothing);SqliteQueuewithFTW_QUEUE_MAX_DELIVERIES(dead, and its jobFAILED); two workers, one killed mid-job: the other takes the job over,executionsis 2 and the events read queued, running, lost, running, succeeded; a duplicate delivery of a live job is skipped; a stalled worker that wakes up loses its job quietly and writes nothing; the heartbeat keeps a long job alive; a takeover of a resumable job reuses its finished steps (and one that is not resumable reruns them), and never its own execution's;client.failon anINITjob, and on a final one; the timeout fails a hung job (in the worker and inrun_job). jobs_lookup: it is not called for a namejobshas; a namejobslacks is looked up, and the class it returns runs (an agent class too);NoneisUnknownJob; a lookup that returns another name, a job whose hash is not the one recorded at submit, and a lookup that raises each fail the job withJobLookupErrorand do not run it; a lookup that ignores the hash runs a job whose definition did not change; an emptyjobs; the identity reaches the lookup.- The timeout and the name: a lookup that sleeps past a short timeout fails the job with
JobLookupError, is not asked again, its message is acknowledged and the next job runs (also one that is injobs); a lookup that raises its ownTimeoutErroris a lookup that raised; unset, the timeout is 15 seconds, and a bad value is aValueErrorthat names the variable; a name with/, with.., empty or starting with a digit fails withUnknownJobwithout the lookup being asked, and the text does not hold the name. - The identity (the spec): a job submitted with an identity whose tool reads it on a worker; a message without one for a worker whose type does not allow
None, and one that does not validate, one whose kind the type does not have, and one with no kind, fail the job without running it; a worker whose type its runner does not accept is refused when it is built;check_queueand the identity and its kind. - The client and the worker: submit, status, wait, result; a failing enqueue; every cancel case, in the same process and from
another; success, failure, an unknown job, an invalid input, a failing result store, a skipped cancelled job; shutdown with the
grace period and the requeue; concurrency; resuming through
submit; and a client in one process with a worker in another.
The work¶
| Issue | |
|---|---|
| #71 | The epic |
| #72 | The Job class, JobContext, the job columns and CANCELLED |
| #73 | The JobQueue abstract class, SqliteQueue, MemoryQueue and the kit |
| #74 | Job events and SSE |
| #75 | JobClient |
| #76 | JobWorker |
| #77 | The @job decorator |
| #78 | The guide, an example and the skill reference |
| #103 | The epic for several workers (see several workers), with #104 to #108 |