celery/task/
signature.rs

1use super::{Task, TaskOptions};
2use crate::protocol::MessageContentType;
3use chrono::{DateTime, Utc};
4
5/// Wraps the parameters and execution options for a single task invocation.
6///
7/// When you define a task through the [`task`](macro@crate::task) attribute macro, calling
8/// `T::new(...)` with the arguments that your task function take will create a
9/// [`Signature<T>`](Signature).
10///
11/// # Examples
12///
13/// ```rust
14/// use celery::prelude::*;
15///
16/// #[celery::task]
17/// fn add(x: i32, y: i32) -> TaskResult<i32> {
18///     Ok(x + y)
19/// }
20///
21/// let signature = add::new(1, 2);
22/// ```
23#[derive(Clone)]
24pub struct Signature<T>
25where
26    T: Task,
27{
28    /// The parameters for the task invocation.
29    pub(crate) params: T::Params,
30
31    /// A queue to send the task to.
32    pub(crate) queue: Option<String>,
33
34    /// The number of seconds to wait before executing the task. This is equivalent to setting
35    /// [`eta`](struct.Signature.html#structfield.eta)
36    /// to `current_time + countdown`.
37    pub(crate) countdown: Option<u32>,
38
39    /// A future ETA at which to execute the task.
40    pub(crate) eta: Option<DateTime<Utc>>,
41
42    /// A number of seconds until the task expires, at which point it should no longer
43    /// be executed. This is equivalent to setting
44    /// [`expires`](struct.Signature.html#structfield.expires)
45    /// to `current_time + expires_in`.
46    pub(crate) expires_in: Option<u32>,
47
48    /// A future time at which the task will expire.
49    pub(crate) expires: Option<DateTime<Utc>>,
50
51    /// Additional options.
52    pub(crate) options: TaskOptions,
53}
54
55impl<T> Signature<T>
56where
57    T: Task,
58{
59    /// Create a new `Signature` from task parameters.
60    pub fn new(params: T::Params) -> Self {
61        Self {
62            params,
63            queue: None,
64            countdown: None,
65            eta: None,
66            expires_in: None,
67            expires: None,
68            options: T::DEFAULTS,
69        }
70    }
71
72    /// Get the name of the task.
73    pub fn task_name() -> &'static str {
74        T::NAME
75    }
76
77    /// Set the queue.
78    pub fn with_queue(mut self, queue: &str) -> Self {
79        self.queue = Some(queue.into());
80        self
81    }
82
83    /// Set the countdown.
84    pub fn with_countdown(mut self, countdown: u32) -> Self {
85        self.countdown = Some(countdown);
86        self
87    }
88
89    /// Set the ETA.
90    pub fn with_eta(mut self, eta: DateTime<Utc>) -> Self {
91        self.eta = Some(eta);
92        self
93    }
94
95    /// Set the number of seconds until the task expires.
96    pub fn with_expires_in(mut self, expires_in: u32) -> Self {
97        self.expires_in = Some(expires_in);
98        self
99    }
100
101    /// Set the expiration time.
102    pub fn with_expires(mut self, expires: DateTime<Utc>) -> Self {
103        self.expires = Some(expires);
104        self
105    }
106
107    /// Set the content type serialization format for the message body.
108    pub fn with_content_type(mut self, content_type: MessageContentType) -> Self {
109        self.options.content_type = Some(content_type);
110        self
111    }
112
113    /// Set a time limit (in seconds) for the task.
114    pub fn with_time_limit(mut self, time_limit: u32) -> Self {
115        self.options.time_limit = Some(time_limit);
116        self
117    }
118
119    /// Set a hard time limit (in seconds) for the task.
120    ///
121    /// *Note that this is really only for compatability with Python workers*.
122    /// `time_limit` and `hard_time_limit` are treated the same by Rust workers, and if both
123    /// are set, the minimum of the two will be used.
124    pub fn with_hard_time_limit(mut self, time_limit: u32) -> Self {
125        self.options.hard_time_limit = Some(time_limit);
126        self
127    }
128}