Skip to main content

queuey_core/
job.rs

1//! The [`Job`] trait: a serializable payload bound to one queue.
2
3use serde::{Serialize, de::DeserializeOwned};
4
5use crate::{queue::QueueSet, retry::RetryPolicy};
6
7/// A unit of work. Normally implemented via `#[derive(Job)]` from the macros
8/// crate; the hand-written equivalent is:
9///
10/// ```
11/// use queuey_core::{Job, QueueConfig, QueueSet, RetryPolicy};
12/// use serde::{Deserialize, Serialize};
13///
14/// #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15/// enum AppQueues { Emails }
16///
17/// impl QueueSet for AppQueues {
18///     fn all() -> &'static [Self] { &[AppQueues::Emails] }
19///     fn name(&self) -> &'static str { "emails" }
20///     fn config(&self) -> QueueConfig { QueueConfig::new(self.name()) }
21/// }
22///
23/// #[derive(Serialize, Deserialize)]
24/// struct SendEmail { to: String }
25///
26/// impl Job for SendEmail {
27///     type Queue = AppQueues;
28///     const NAME: &'static str = "SendEmail";
29///     const QUEUE: AppQueues = AppQueues::Emails;
30///     fn retry_policy() -> Option<RetryPolicy> { Some(RetryPolicy::exponential(5)) }
31/// }
32///
33/// assert_eq!(SendEmail::QUEUE.name(), "emails");
34/// ```
35pub trait Job: Serialize + DeserializeOwned + Send + Sync + 'static {
36    /// The queue set this job belongs to.
37    type Queue: QueueSet;
38
39    /// Unique, stable identifier for this job type. Used for routing to the right
40    /// handler. Defaults to the fully-qualified type path via the derive macro.
41    const NAME: &'static str;
42
43    /// The queue (variant) this job is published to and consumed from.
44    const QUEUE: Self::Queue;
45
46    /// Per-job retry override. `None` means "use the queue's policy".
47    fn retry_policy() -> Option<RetryPolicy> {
48        None
49    }
50}