Skip to main content

Queue

Struct Queue 

Source
pub struct Queue { /* private fields */ }
Expand description

A Postgres-backed job queue.

Implementations§

Source§

impl Queue

Source

pub fn from_pool(pool: PgPool, options: QueueOptions) -> Self

A queue on pool, checking and changing nothing. Call Queue::check at boot. Give the pool at least workers + 2 connections.

Source

pub async fn connect_existing(uri: &str, options: QueueOptions) -> Result<Self>

Connect to tables that a migration made, and change no schema, for a database role that may not. A migration runs SETUP_STATEMENTS.

§Errors

Fails when the database cannot be reached, a table is missing or out of date, or a trigger of axa_job_acts is missing or not set to fire always, so a service finds out at boot rather than at its first job. There is no way to run without the triggers.

Source

pub async fn connect(uri: &str, options: QueueOptions) -> Result<Self>

Connect and create the tables if needed, which runs DDL at boot. Use Queue::connect_existing where a service’s role may not.

§Errors

Fails when the database cannot be reached or the tables not created.

Source

pub async fn check(&self) -> Result<()>

Check that the tables exist and are up to date, and that the triggers of axa_job_acts are there and set to fire always.

§Errors

Fails when the database cannot be reached, a table is missing or out of date, or a trigger is missing or turned off.

Source

pub const fn pool(&self) -> &PgPool

The pool, for a service’s own queries or enqueue_on outside a transaction.

Source

pub const fn options(&self) -> &QueueOptions

The options this queue runs with.

Source

pub fn handle<F, Fut, E>(&mut self, name: impl Into<String>, handler: F)
where F: Fn(Value) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<(), E>> + Send + 'static, E: Display + 'static,

Run handler for jobs named name. Its error text is kept on the job’s row. Registering a name again replaces its handler.

Source

pub fn limit(&mut self, name: impl Into<String>, jobs: usize)

Run at most jobs jobs named name at once in this process, and at least one. Other jobs keep the remaining loops busy.

Source

pub fn schedule(&mut self, schedule: Schedule)

Queue a job on every due time of schedule.

§Panics

When a schedule of that name was added already.

Source

pub async fn enqueue(&self, name: &str, args: Value) -> Result<Uuid>

Add a job and return its id.

§Errors

Fails on a database error.

Source

pub async fn enqueue_limited( &self, name: &str, args: Value, max_attempts: Option<i32>, ) -> Result<Uuid>

Add a job with its own limit on tries, or the queue’s when None.

§Errors

Fails on a database error.

Source

pub async fn enqueue_job(&self, job: &NewJob) -> Result<Uuid>

Add job and return its id.

§Errors

Fails on a database error.

Source

pub async fn enqueue_all(&self, jobs: &[NewJob]) -> Result<Vec<Uuid>>

Add every job in jobs in one transaction, so all are queued or none.

§Errors

Fails on a database error, and then none was queued.

Source

pub async fn job(&self, id: Uuid) -> Result<Option<JobInfo>>

What the queue knows of the job id, or None when there is no such job, as after retention deleted it.

§Errors

Fails on a database error.

Source

pub async fn acts(&self, id: Uuid) -> Result<Vec<Act>>

Every act a person did to the job id, oldest first, for a service’s own review screens. Acts are kept after retention deletes the job’s row. A person acts with cancel() and retry(), in the caller’s transaction.

§Errors

Fails on a database error.

Source

pub async fn counts(&self) -> Result<Counts>

How many jobs are in each state.

§Errors

Fails on a database error.

Source

pub async fn ping(&self) -> Result<()>

Check the database connection.

§Errors

Fails when the database does not answer.

Source

pub async fn requeue_stale(&self) -> Result<u64>

Recover jobs abandoned by a worker that died: queue again those with attempts left and fail the rest, returning how many were queued again.

§Errors

Fails on a database error.

Source

pub async fn purge_finished(&self) -> Result<u64>

Delete finished and cancelled rows older than the retention period, returning how many went.

§Errors

Fails on a database error.

Source

pub async fn tick_schedules(&self) -> Result<usize>

Queue a job for each schedule that is due, returning how many were queued. Queue::run calls this on every poll. Any number of processes may call it at once: each due time is queued once.

§Errors

Fails on a database error, which leaves that schedule due for the next tick.

Source

pub async fn archive_schedules(&self) -> Result<u64>

Archive the schedules that no live process has in code, returning how many were archived. Rows are kept, for audit. Housekeeping runs this.

§Errors

Fails on a database error.

Source

pub async fn schedules(&self) -> Result<Vec<ScheduleInfo>>

Every schedule the table holds, archived or not, with its last job’s state.

§Errors

Fails on a database error.

Source

pub async fn run_one(&self) -> Result<bool>

Claim and run one job, returning whether there was one. A job’s own failure is recorded on its row, not returned.

§Errors

Fails on a database error.

Source

pub async fn run(self: Arc<Self>, shutdown: Receiver<bool>)

Run job loops, housekeeping, and schedules until shutdown changes to true. Jobs in progress finish first, so a caller that must stop by a time limit drops this future at it, which aborts those jobs and leaves them for stale recovery. Housekeeping runs straight away, to recover anything a previous process left behind, and then on a timer.

Source

pub fn handled(&self) -> HashSet<&str>

The names of the job types this queue handles.

Trait Implementations§

Source§

impl Debug for Queue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Queue

§

impl !UnwindSafe for Queue

§

impl Forget for Queue

§

impl Freeze for Queue

§

impl Send for Queue

§

impl Sync for Queue

§

impl Unpin for Queue

§

impl UnsafeUnpin for Queue

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more