Skip to main content

laravel/
queue.rs

1//! Queues: jobs that run later, outside the request that dispatched them.
2//!
3//! Some work, such as transcoding an uploaded podcast or sending a thousand emails, takes too long
4//! to do while a request waits. This module lets you push that work to a queue and run it in a
5//! worker process. To learn more, check out the
6//! [queues guide](https://github.com/laravel/rust-framework/blob/0.x/docs/queues.md).
7//!
8//! A job is a [`Queueable`](derive@Queueable) struct with [`#[job]`](macro@job) on its `impl`
9//! block. Dispatching the job pushes it to a connection, and the connection keeps the job until a
10//! worker runs it:
11//!
12//! ```no_run
13//! use laravel::container::Inject;
14//! use laravel::queue::job;
15//! # pub struct AudioProcessor;
16//! # impl AudioProcessor { async fn process(&self, id: i64) -> laravel::Result<()> { Ok(()) } }
17//!
18//! #[derive(laravel::queue::Queueable)]
19//! pub struct ProcessPodcast {
20//!     pub podcast_id: i64,
21//! }
22//!
23//! #[job(
24//!     queue = "podcasts",
25//!     tries = 3,
26//!     backoff = [10.seconds(), 1.minutes()],
27//!     timeout = 2.minutes(),
28//! )]
29//! impl ProcessPodcast {
30//!     async fn handle(self, audio: Inject<AudioProcessor>) -> laravel::Result<()> {
31//!         audio.process(self.podcast_id).await
32//!     }
33//! }
34//!
35//! # async fn example() -> laravel::Result<()> {
36//! ProcessPodcast { podcast_id: 42 }.dispatch().await?;
37//! # Ok(()) }
38//! ```
39//!
40//! `config.queue` names the application's connections. Each connection uses the `database`,
41//! `redis`, `sqs`, `cloud`, `sync`, or `discard` driver, or a driver that an application or
42//! package registered with [`Drivers::extend`]. Laravel creates a connection's driver the first
43//! time a job is pushed to the connection or a worker starts working it.
44//!
45//! # Running jobs
46//!
47//! Each job runs in a fresh application context that starts with the dispatching context's
48//! values. Values that the job adds to its context carry on to its error reports, its `failed`
49//! hook, the worker's [events] about the job, the callbacks the job triggers, and the next job in
50//! its chain. The job's log lines carry a `job` span with its ID, name, connection, queue, and
51//! attempt number.
52//!
53//! Before each run, the worker checks whether the job may still run. If the job's `retry_for` has
54//! passed, or the job has no `retry_for` and has already run `tries` times, the worker fails the
55//! job for good without running it.
56//!
57//! When a run fails, the worker fails the job for good if the job called [`Attempt::fail`], the
58//! job can't be decoded, the run was the job's last, or the run used up the job's `max_errors`.
59//! Otherwise, the worker retries the job after the backoff for that run's number. A release also
60//! uses up a run, so a retry after a release waits as long as it would after a failed run.
61//!
62//! When a job fails for good, the worker removes it from its queue and stores it with the failed
63//! jobs. Then the job's `failed` hook runs, the worker dispatches [`events::JobFailed`], and the
64//! worker reports the job's error. A job that can't be decoded doesn't run its `failed` hook or
65//! dispatch events.
66//!
67//! `queue:retry` pushes a failed job back onto its queue with the same ID, all of its tries, and
68//! a fresh `retry_for`. When the retried job succeeds, its chain continues, and its batch stops
69//! counting it as failed. A retried job on an SQS FIFO queue gets a new deduplication ID.
70//!
71//! # Workers
72//!
73//! `queue:work` fails for the `sync` and `discard` connections. When a connection's backend is
74//! unavailable, the worker reports the error and tries again, doubling its wait each time up to a
75//! minute. A worker started with `--stop-when-empty` exits with status 1 instead.
76//!
77//! When a worker is killed, its jobs stay reserved until their reservations lapse, about a minute
78//! later. The interrupted run counts as an attempt and as an error toward `max_errors`.
79//!
80//! `queue:restart` and `queue:pause` reach workers through the default cache store. With the
81//! `memory` store, `queue:restart` warns that other processes won't see the restart. With the
82//! `discard` store, `queue:restart` fails. An idle worker sees a restart after its current sleep
83//! or long poll, and a worker that started after the restart ignores it. While a worker can't
84//! read the cache, it logs a warning, keeps working, and uses the pauses it last read.
85//!
86//! `queue:pause --all` is separate from pauses of single queues. `queue:continue --all` doesn't
87//! resume a queue that was paused by name, and `queue:continue emails` doesn't resume `emails`
88//! while every queue is paused. A worker started with `--stop-when-empty` stops when all of its
89//! queues are paused.
90
91pub(crate) use laravel_queue::queue::*;
92#[doc(hidden)]
93pub use laravel_queue::queue::{__CallbackRegistration, __Registration, __queueable};
94#[doc(inline)]
95pub use laravel_queue::queue::{
96    Attempt, Batch, BatchId, Callback, CallbackList, Catch, ChainCatch, ChainDispatched, ConnectionDefinition, Driver,
97    Drivers, Failed, Finally, HEARTBEAT, IntoBatchJob, IntoBatchJobs, IntoJobs, IntoMiddleware, Job, JobId, JobList,
98    JobOptions, JobResult, JobTimedOut, JobTypes, LEASE, Middleware, MiddlewareGroup, MissingModels, Next,
99    ParseIdError, PendingBatch, PendingBulk, PendingChain, PendingDispatch, Pushed, PushedBatch, QueueFake,
100    QueueManager, QueueSizes, Queueable, Reservation, ReservedJob, Then, batch, bulk, callback, cancel_batch, chain,
101    dispatch, dispatch_sync, events, fake, find_batch, job, manager, middleware,
102};
103
104/// Define a migration's `migrate` function to create the `database` queue driver's tables.
105///
106/// Put the macro alone in a migration file, and it creates the `jobs`, `failed_jobs`,
107/// `job_batches`, and `job_batch_jobs` tables. To change the tables, replace the macro with the
108/// migration the
109/// [queues guide](https://github.com/laravel/rust-framework/blob/0.x/docs/queues.md#the-jobs-table) shows,
110/// then edit it.
111///
112/// ```
113/// // database/migrations/0001_01_01_000002_create_jobs_table.rs
114/// laravel::queue::tables!();
115/// ```
116#[cfg(feature = "database")]
117pub use laravel_rust_database_macros::queue_tables as tables;