Skip to main content

ferro_queue/
lib.rs

1//! # Ferro Queue
2//!
3//! Background job queue system for the Ferro framework.
4//!
5//! Provides a Laravel-inspired queue system with support for:
6//! - Redis-backed job queues
7//! - Job delays and retries
8//! - Multiple named queues
9//! - Job chaining
10//! - Graceful shutdown
11//!
12//! ## Example
13//!
14//! ```rust,ignore
15//! use ferro_queue::{Job, Queueable, dispatch};
16//! use serde::{Deserialize, Serialize};
17//!
18//! #[derive(Debug, Clone, Serialize, Deserialize)]
19//! struct SendEmail {
20//!     to: String,
21//!     subject: String,
22//! }
23//!
24//! #[async_trait::async_trait]
25//! impl Job for SendEmail {
26//!     async fn handle(&self) -> Result<(), ferro_queue::Error> {
27//!         println!("Sending email to {}: {}", self.to, self.subject);
28//!         Ok(())
29//!     }
30//! }
31//!
32//! // Dispatch a job
33//! SendEmail { to: "user@example.com".into(), subject: "Hello".into() }
34//!     .dispatch()
35//!     .await?;
36//!
37//! // Dispatch with delay
38//! SendEmail { to: "user@example.com".into(), subject: "Reminder".into() }
39//!     .delay(std::time::Duration::from_secs(60))
40//!     .on_queue("emails")
41//!     .dispatch()
42//!     .await?;
43//! ```
44
45mod config;
46mod dispatcher;
47mod error;
48mod job;
49mod queue;
50mod worker;
51
52pub use config::QueueConfig;
53pub use dispatcher::{
54    dispatch, dispatch_later, dispatch_to, register_tenant_capture_hook, PendingDispatch,
55};
56pub use error::Error;
57pub use job::{Job, JobPayload};
58pub use queue::{
59    FailedJobInfo, JobInfo, JobState, Queue, QueueConnection, QueueStats, SingleQueueStats,
60};
61pub use worker::{TenantScopeProvider, Worker, WorkerConfig};
62
63/// Re-export async_trait for convenience
64pub use async_trait::async_trait;
65
66/// Trait for types that can be dispatched to a queue.
67pub trait Queueable: Job + serde::Serialize + serde::de::DeserializeOwned {
68    /// Create a pending dispatch for this job.
69    fn dispatch(self) -> PendingDispatch<Self>
70    where
71        Self: Sized,
72    {
73        PendingDispatch::new(self)
74    }
75
76    /// Dispatch this job with a delay.
77    fn delay(self, duration: std::time::Duration) -> PendingDispatch<Self>
78    where
79        Self: Sized,
80    {
81        PendingDispatch::new(self).delay(duration)
82    }
83
84    /// Dispatch this job to a specific queue.
85    fn on_queue(self, queue: &'static str) -> PendingDispatch<Self>
86    where
87        Self: Sized,
88    {
89        PendingDispatch::new(self).on_queue(queue)
90    }
91}
92
93/// Blanket implementation for all types that implement Job + Serialize + DeserializeOwned.
94impl<T> Queueable for T where T: Job + serde::Serialize + serde::de::DeserializeOwned {}