apalis_core/task/
mod.rs

1//! Utilities for creating and managing tasks.
2//!
3//! The [`Task`] component encapsulates a unit of work to be executed,
4//! along with its associated context, metadata, and execution status. The [`Parts`]
5//! struct contains metadata, attempt tracking, extensions, and scheduling information for each task.
6//!
7//! # Overview
8//!
9//! In `apalis`, tasks are designed to represent discrete units of work that can be scheduled, retried, and tracked
10//! throughout their lifecycle. Each task consists of arguments (`args`) describing the work to be performed,
11//! and an [`Parts`] (`parts`) containing metadata and control information.
12//!
13//! ## [`Task`]
14//!
15//! The [`Task`] struct is generic over:
16//! - `Args`: The type of arguments or payload for the task.
17//! - `Ctx`: Ctxdata associated with the task, such as custom fields or backend-specific information.
18//! - `IdType`: The type used for uniquely identifying the task (defaults to [`RandomId`]).
19//!
20//! ## [`Parts`]
21//!
22//! The [`Parts`] struct provides the following:
23//! - `task_id`: Optionally stores a unique identifier for the task.
24//! - `data`: An [`Extensions`] container for storing arbitrary per-task data (e.g., middleware extensions).
25//! - `attempt`: Tracks how many times the task has been attempted.
26//! - `metadata`: Custom metadata for the task, provided by the backend or user.
27//! - `status`: The current [`Status`] of the task (e.g., Pending, Running, Completed, Failed).
28//! - `run_at`: The UNIX timestamp (in seconds) when the task should be run.
29//!
30//! The execution context is essential for tracking the state and metadata of a task as it moves through
31//! the system. It enables features such as retries, scheduling, and extensibility via the `Extensions` type.
32//!
33//! # Modules
34//!
35//! - [`attempt`]: Tracks the number of attempts a task has been executed.
36//! - [`builder`]: Utilities for constructing tasks.
37//! - [`data`]: Data types for task payloads.
38//! - [`extensions`]: Extension storage for tasks.
39//! - [`metadata`]: Ctxdata types for tasks.
40//! - [`status`]: Status tracking for tasks.
41//! - [`task_id`]: Types for uniquely identifying tasks.
42//!
43//! # Examples
44//!
45//! ## Creating a new task with default metadata
46//!
47//! ```rust
48//! # use apalis_core::task::{Task, Parts};
49//! # use apalis_core::task::builder::TaskBuilder;
50//! # use apalis_core::task::task_id::RandomId;
51//! let task: Task<String, (), RandomId> = TaskBuilder::new("my work".to_string()).build();
52//! ```
53//!
54//! ## Creating a task with custom metadata
55//!
56//! ```rust
57//! # use apalis_core::task::{Task, Parts};
58//! # use apalis_core::task::builder::TaskBuilder;
59//! # use apalis_core::task::extensions::Extensions;
60//! # use apalis_core::task::task_id::RandomId;
61//!
62//! #[derive(Default, Clone)]
63//! struct MyCtx { priority: u8 }
64//! let task: Task<String, Extensions, RandomId> = TaskBuilder::new("important work".to_string())
65//!     .meta(MyCtx { priority: 5 })
66//!     .build();
67//! ```
68//!
69//! ## Accessing and modifying the execution context
70//!
71//! ```rust
72//! # use apalis_core::task::task_id::RandomId;
73//! use apalis_core::task::{Task, Parts, status::Status};
74//! let mut task = Task::<String, (), RandomId>::new("work".to_string());
75//! task.parts.status = Status::Running.into();
76//! task.parts.attempt.increment();
77//! ```
78//!
79//! ## Using Extensions for per-task data
80//!
81//! ```rust
82//! # use apalis_core::task::builder::TaskBuilder;
83//! # use apalis_core::task::task_id::RandomId;
84//! use apalis_core::task::{Task, extensions::Extensions};
85//! #[derive(Debug, Clone, PartialEq)]
86//! pub struct TracingId(String);
87//! let mut extensions = Extensions::default();
88//! extensions.insert(TracingId("abc123".to_owned()));
89//! let task: Task<String, (), RandomId> = TaskBuilder::new("work".to_string()).with_data(extensions).build();
90//! assert_eq!(task.parts.data.get::<TracingId>(), Some(&TracingId("abc123".to_owned())));
91//! ```
92//!
93//! # See Also
94//!
95//! - [`Task`]: Represents a unit of work to be executed.
96//! - [`Parts`]: Holds metadata, status, and control information for a task.
97//! - [`Extensions`]: Type-safe storage for per-task data.
98//! - [`Status`]: Enum representing the lifecycle state of a task.
99//! - [`Attempt`]: Tracks the number of execution attempts for a task.
100//! - [`TaskId`]: Unique identifier type for tasks.
101//! - [`FromRequest`]: Trait for extracting data from task contexts.
102//! - [`IntoResponse`]: Trait for converting tasks into response types.
103//! - [`TaskBuilder`]: Fluent builder for constructing tasks with optional configuration.
104//! - [`RandomId`]: Default unique identifier type for tasks.
105//!
106//! [`TaskBuilder`]: crate::task::builder::TaskBuilder
107//! [`IntoResponse`]: crate::task_fn::into_response::IntoResponse
108//! [`FromRequest`]: crate::task_fn::from_request::FromRequest
109
110use std::{
111    fmt::Debug,
112    time::{SystemTime, UNIX_EPOCH},
113};
114
115use crate::{
116    task::{
117        attempt::Attempt,
118        builder::TaskBuilder,
119        extensions::Extensions,
120        status::{AtomicStatus, Status},
121        task_id::TaskId,
122    },
123    task_fn::FromRequest,
124};
125
126pub mod attempt;
127pub mod builder;
128pub mod data;
129pub mod extensions;
130pub mod metadata;
131pub mod status;
132pub mod task_id;
133
134/// Represents a task which will be executed
135/// Should be considered a single unit of work
136#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
137#[derive(Debug, Clone, Default)]
138pub struct Task<Args, Context, IdType> {
139    /// The argument task part
140    pub args: Args,
141    /// Parts of the task eg id, attempts and context
142    pub parts: Parts<Context, IdType>,
143}
144
145/// Component parts of a `Task`
146#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
147#[derive(Default)]
148pub struct Parts<Context, IdType> {
149    /// The task's id if allocated
150    pub task_id: Option<TaskId<IdType>>,
151
152    /// The tasks's extensions
153    #[cfg_attr(feature = "serde", serde(skip))]
154    pub data: Extensions,
155
156    /// The tasks's attempts
157    /// Keeps track of the number of attempts a task has been worked on
158    pub attempt: Attempt,
159
160    /// The task specific data provided by the backend
161    pub ctx: Context,
162
163    /// The task status that is wrapped in an atomic status
164    pub status: AtomicStatus,
165
166    /// The time a task should be run
167    pub run_at: u64,
168}
169
170impl<Ctx: Debug, IdType: Debug> Debug for Parts<Ctx, IdType> {
171    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
172        f.debug_struct("Parts")
173            .field("task_id", &self.task_id)
174            .field("data", &"<Extensions>")
175            .field("attempt", &self.attempt)
176            .field("ctx", &self.ctx)
177            .field("status", &self.status.load())
178            .field("run_at", &self.run_at)
179            .finish()
180    }
181}
182
183impl<Ctx, IdType: Clone> Clone for Parts<Ctx, IdType>
184where
185    Ctx: Clone,
186{
187    fn clone(&self) -> Self {
188        Self {
189            task_id: self.task_id.clone(),
190            data: self.data.clone(),
191            attempt: self.attempt.clone(),
192            ctx: self.ctx.clone(),
193            status: self.status.clone(),
194            run_at: self.run_at,
195        }
196    }
197}
198
199impl<Args, Ctx, IdType> Task<Args, Ctx, IdType> {
200    /// Creates a new [Task]
201    pub fn new(args: Args) -> Self
202    where
203        Ctx: Default,
204    {
205        Self::new_with_data(args, Extensions::default(), Ctx::default())
206    }
207
208    /// Creates a task with context provided
209    pub fn new_with_ctx(req: Args, ctx: Ctx) -> Self {
210        Self {
211            args: req,
212            parts: Parts {
213                ctx,
214                task_id: Default::default(),
215                attempt: Default::default(),
216                data: Default::default(),
217                status: Status::Pending.into(),
218                run_at: {
219                    let now = SystemTime::now();
220                    let duration_since_epoch =
221                        now.duration_since(UNIX_EPOCH).expect("Time went backwards");
222                    duration_since_epoch.as_secs()
223                },
224            },
225        }
226    }
227
228    /// Creates a task with data and context provided
229    pub fn new_with_data(req: Args, data: Extensions, ctx: Ctx) -> Self {
230        Self {
231            args: req,
232            parts: Parts {
233                ctx,
234                task_id: Default::default(),
235                attempt: Default::default(),
236                data,
237                status: Status::Pending.into(),
238                run_at: {
239                    let now = SystemTime::now();
240                    let duration_since_epoch =
241                        now.duration_since(UNIX_EPOCH).expect("Time went backwards");
242                    duration_since_epoch.as_secs()
243                },
244            },
245        }
246    }
247
248    /// Take the task into its parts
249    pub fn take(self) -> (Args, Parts<Ctx, IdType>) {
250        (self.args, self.parts)
251    }
252
253    /// Extract a value of type `T` from the task's context
254    ///
255    /// Uses [FromRequest] trait to extract the value.
256    pub async fn extract<T: FromRequest<Self>>(&self) -> Result<T, T::Error> {
257        T::from_request(self).await
258    }
259
260    /// Converts the task into a builder pattern.
261    pub fn into_builder(self) -> TaskBuilder<Args, Ctx, IdType> {
262        TaskBuilder {
263            args: self.args,
264            ctx: self.parts.ctx,
265            attempt: Some(self.parts.attempt),
266            data: self.parts.data,
267            status: Some(self.parts.status.into()),
268            run_at: Some(self.parts.run_at),
269            task_id: self.parts.task_id,
270        }
271    }
272}
273
274impl<Args, Ctx, IdType> Task<Args, Ctx, IdType> {
275    /// Maps the `args` field using the provided function, consuming the task.
276    pub fn try_map<F, NewArgs, Err>(self, f: F) -> Result<Task<NewArgs, Ctx, IdType>, Err>
277    where
278        F: FnOnce(Args) -> Result<NewArgs, Err>,
279    {
280        Ok(Task {
281            args: f(self.args)?,
282            parts: self.parts,
283        })
284    }
285    /// Maps the `args` field using the provided function, consuming the task.
286    pub fn map<F, NewArgs>(self, f: F) -> Task<NewArgs, Ctx, IdType>
287    where
288        F: FnOnce(Args) -> NewArgs,
289    {
290        Task {
291            args: f(self.args),
292            parts: self.parts,
293        }
294    }
295
296    /// Maps both `args` and `parts` together.
297    pub fn map_all<F, NewArgs, NewCtx>(self, f: F) -> Task<NewArgs, NewCtx, IdType>
298    where
299        F: FnOnce(Args, Parts<Ctx, IdType>) -> (NewArgs, Parts<NewCtx, IdType>),
300    {
301        let (args, parts) = f(self.args, self.parts);
302        Task { args, parts }
303    }
304
305    /// Maps only the `parts` field.
306    pub fn map_parts<F, NewCtx>(self, f: F) -> Task<Args, NewCtx, IdType>
307    where
308        F: FnOnce(Parts<Ctx, IdType>) -> Parts<NewCtx, IdType>,
309    {
310        Task {
311            args: self.args,
312            parts: f(self.parts),
313        }
314    }
315}