Expand description
rustlavel-queue: background jobs and scheduling.
Work that should not happen while someone is waiting for a response — sending mail, resizing an image, calling a slow API — goes on a queue and is run by a worker. Work that should happen at a particular time goes on a schedule.
// Define a job.
struct SendWelcomeEmail { user_id: i64 }
impl Job for SendWelcomeEmail {
const NAME: &'static str = "send-welcome-email";
fn payload(&self) -> Json {
Json::object([("user_id", Json::from(self.user_id))])
}
fn from_payload(payload: &Json) -> Result<Self> {
Ok(SendWelcomeEmail {
user_id: payload.get("user_id").and_then(Json::as_i64).unwrap_or_default(),
})
}
fn handle(&self) -> impl Future<Output = Result<()>> + Send {
async move { Ok(()) }
}
}
// Dispatch it from a handler.
queue.dispatch(&SendWelcomeEmail { user_id }).await?;
// Run it, in `queue:work`.
let mut jobs = JobRegistry::new();
jobs.register::<SendWelcomeEmail>();
let shutdown = Shutdown::new();
shutdown.on_ctrl_c();
run_pool(Worker::new(queue, Arc::new(jobs)), 4, shutdown).await?;§The one thing that is not like Laravel
Laravel serialises the job object and asks PHP to bring the class back. A
compiled language cannot resurrect a type from a string, so the way back
from a stored name to running code is stated once, at boot, in a
JobRegistry. That is the only line of ceremony the design adds, and it
buys a queue:work that fails to compile against a renamed job rather than
failing at three in the morning against a class that is no longer there.
§What is in here
JobandJobRegistry— defining work and finding it again.QueueandQueueExt— the driver contract and its conveniences.MemoryQueue,DatabaseQueue,FakeQueue— the three drivers.Worker,run_pool,Shutdown— running jobs, and stopping well.SchedulerandCron— running work at a time rather than on demand.QueueDashboard— the plugin that exposes it all over HTTP.
Everything the queue does is reported on rustlavel_core::events as
queue.pushed, queue.processed and queue.failed, so Telescope shows
jobs without this crate knowing Telescope exists.
Re-exports§
pub use chain::Chain;pub use cron::Cron;pub use cron::Weekday;pub use database::CreateJobProgressTable;pub use database::CreateQueueTables;pub use database::DatabaseProgress;pub use database::DatabaseQueue;pub use fake::FakeQueue;pub use fake::fake;pub use job::BoxFuture;pub use job::DEFAULT_QUEUE;pub use job::DEFAULT_RETRY_AFTER;pub use job::DEFAULT_TRIES;pub use job::FailedJob;pub use job::Job;pub use job::JobRegistry;pub use job::QueuedJob;pub use job::ReservedJob;pub use memory::MemoryQueue;pub use progress::JobContext;pub use progress::MemoryProgress;pub use progress::Progress;pub use progress::ProgressStore;pub use plugin::QueueDashboard;pub use queue::Queue;pub use queue::QueueExt;pub use schedule::ScheduledEvent;pub use schedule::Scheduler;pub use schedule::TickReport;pub use schedule::schedule;pub use worker::Outcome;pub use worker::Shutdown;pub use worker::Worker;pub use worker::WorkerOptions;pub use worker::WorkerStats;pub use worker::run_pool;
Modules§
- chain
- Jobs that run one after another.
- cron
- A cron expression parser and next-run calculator, written from scratch.
- database
- The database driver: a
jobstable, afailed_jobstable, and a reservation that is safe when several workers race for the same row. - fake
Queue::fake()— a queue that records instead of running.- job
- What a job is, and how a worker finds one again after it has been stored.
- memory
- The in-process driver: the default, and the one tests use.
- plugin
- The dashboard, attached the way every optional package is:
- prelude
- What a job file imports.
- progress
- Where a job is, as far as somebody outside the worker can tell.
- queue
- The
Queuecontract every driver implements. - schedule
- The scheduler: cron, expressed as a builder.
- time
- The small amount of calendar arithmetic a scheduler needs.
- worker
- The worker loop: reserve, run, and then delete, retry, or bury.
Enums§
- Error
- Every failure the framework itself can produce.
Type Aliases§
- Result
- The framework-wide result type.