pub struct Queue { /* private fields */ }Expand description
A Postgres-backed job queue.
Implementations§
Source§impl Queue
impl Queue
Sourcepub fn from_pool(pool: PgPool, options: QueueOptions) -> Self
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.
Sourcepub async fn connect_existing(uri: &str, options: QueueOptions) -> Result<Self>
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.
Sourcepub async fn connect(uri: &str, options: QueueOptions) -> Result<Self>
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.
Sourcepub async fn check(&self) -> Result<()>
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.
Sourcepub const fn pool(&self) -> &PgPool
pub const fn pool(&self) -> &PgPool
The pool, for a service’s own queries or enqueue_on outside a
transaction.
Sourcepub const fn options(&self) -> &QueueOptions
pub const fn options(&self) -> &QueueOptions
The options this queue runs with.
Sourcepub fn handle<F, Fut, E>(&mut self, name: impl Into<String>, handler: F)
pub fn handle<F, Fut, E>(&mut self, name: impl Into<String>, handler: F)
Run handler for jobs named name. Its error text is kept on the job’s
row. Registering a name again replaces its handler.
Sourcepub fn limit(&mut self, name: impl Into<String>, jobs: usize)
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.
Sourcepub async fn enqueue_limited(
&self,
name: &str,
args: Value,
max_attempts: Option<i32>,
) -> Result<Uuid>
pub async fn enqueue_limited( &self, name: &str, args: Value, max_attempts: Option<i32>, ) -> Result<Uuid>
Sourcepub async fn enqueue_job(&self, job: &NewJob) -> Result<Uuid>
pub async fn enqueue_job(&self, job: &NewJob) -> Result<Uuid>
Sourcepub async fn enqueue_all(&self, jobs: &[NewJob]) -> Result<Vec<Uuid>>
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.
Sourcepub async fn job(&self, id: Uuid) -> Result<Option<JobInfo>>
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.
Sourcepub async fn requeue_stale(&self) -> Result<u64>
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.
Sourcepub async fn purge_finished(&self) -> Result<u64>
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.
Sourcepub async fn tick_schedules(&self) -> Result<usize>
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.
Sourcepub async fn archive_schedules(&self) -> Result<u64>
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.
Sourcepub async fn schedules(&self) -> Result<Vec<ScheduleInfo>>
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.
Sourcepub async fn run_one(&self) -> Result<bool>
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.
Sourcepub async fn run(self: Arc<Self>, shutdown: Receiver<bool>)
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.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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