featherweight.queue¶
queue ¶
The queue that connects a client to a worker, as an abstract class.
featherweight passes what the user gives to a job: the input travels in the message (as JSON, or as a reference inside it).
SqliteQueue is our only product implementation, for local development; how jobs travel in a deployment (Redis, SQS, a
database) is the user's decision, and featherweight.testing.check_queue checks an implementation against the contract.
See docs/specs/submitted-jobs.md.
JobMessage ¶
Bases: BaseModel
What goes on the queue: which job, which job class, and its input as JSON (or references inside it).
Delivery ¶
Bases: BaseModel
A message that was received, and the token that identifies the delivery to its queue (opaque: the queue's own).
JobQueue ¶
Bases: ABC
Subclass it and write the five methods; one that misses a method cannot be built.
The contract (featherweight.testing.check_queue runs it against any queue):
- delivery is at-least-once;
- a received message is invisible to other receivers for a lease: until it is acknowledged or nacked, or its lease
ends. If the lease ends first (the receiver died, or stalled), the message becomes available again, and its next
delivery has a higher delivery_count;
- extend renews the lease of a received message. A stale delivery (one whose message was delivered again since) has no
effect: not on the message, and not on the lease of the new delivery;
- nack(requeue=True) makes it available again; nack(requeue=False) drops it (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.
These are what real brokers already have: SQS's visibility timeout (ChangeMessageVisibility is extend) and
ApproximateReceiveCount (the delivery count), or a Redis stream's pending list (XAUTOCLAIM, and its delivery counter).
How many redeliveries are allowed is the queue's business: a broker has its own policy and dead-letter queue.
receive
abstractmethod
async
¶
receive(timeout: float) -> Delivery | None
The next message, claimed for this receiver, or None when there is none within timeout seconds.
SqliteQueue ¶
SqliteQueue(db: SqliteDatabase)
Bases: JobQueue
A job_queue table in the run log's sqlite file, for local development. A message is claimed atomically for
FTW_QUEUE_LEASE seconds (60 by default); if its receiver does not ack, nack or extend it in time, it is delivered again, with a higher
delivery_count. One that has been delivered FTW_QUEUE_MAX_DELIVERIES times (3 by default) and whose lease ends again is marked
dead: it stays in the table, and is not delivered. ack deletes the row; nack puts it back in its place or deletes it.
A delivery's token names its claim, so that a stale delivery cannot touch the message after it was delivered again. The
input stays in the table until the message is acknowledged, on the same terms as the sqlite result store: sqlite is for
local development.