ferro_queue/
lib.rs

1//! # Cancer Queue
2//!
3//! Background job queue system for the Cancer 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 cancer_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<(), cancer_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::{dispatch, dispatch_later, dispatch_to, PendingDispatch};
54pub use error::Error;
55pub use job::{Job, JobPayload};
56pub use queue::{
57    FailedJobInfo, JobInfo, JobState, Queue, QueueConnection, QueueStats, SingleQueueStats,
58};
59pub use worker::{Worker, WorkerConfig};
60
61/// Re-export async_trait for convenience
62pub use async_trait::async_trait;
63
64/// Trait for types that can be dispatched to a queue.
65pub trait Queueable: Job + serde::Serialize + serde::de::DeserializeOwned {
66    /// Create a pending dispatch for this job.
67    fn dispatch(self) -> PendingDispatch<Self>
68    where
69        Self: Sized,
70    {
71        PendingDispatch::new(self)
72    }
73
74    /// Dispatch this job with a delay.
75    fn delay(self, duration: std::time::Duration) -> PendingDispatch<Self>
76    where
77        Self: Sized,
78    {
79        PendingDispatch::new(self).delay(duration)
80    }
81
82    /// Dispatch this job to a specific queue.
83    fn on_queue(self, queue: &'static str) -> PendingDispatch<Self>
84    where
85        Self: Sized,
86    {
87        PendingDispatch::new(self).on_queue(queue)
88    }
89}
90
91/// Blanket implementation for all types that implement Job + Serialize + DeserializeOwned.
92impl<T> Queueable for T where T: Job + serde::Serialize + serde::de::DeserializeOwned {}