reinhardt_tasks/task.rs
1//! Task definitions and execution
2
3use async_trait::async_trait;
4use serde::{Deserialize, Serialize};
5use std::fmt;
6use std::str::FromStr;
7
8/// Default queue name used when no specific queue is specified.
9pub const DEFAULT_TASK_QUEUE_NAME: &str = "default";
10/// Minimum allowed task priority value.
11pub const TASK_MIN_PRIORITY: i32 = 0;
12/// Maximum allowed task priority value.
13pub const TASK_MAX_PRIORITY: i32 = 9;
14
15/// Unique identifier for a task
16///
17/// # Example
18///
19/// ```rust
20/// use reinhardt_tasks::TaskId;
21///
22/// let id1 = TaskId::new();
23/// let id2 = TaskId::new();
24/// assert_ne!(id1, id2);
25/// ```
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
27pub struct TaskId(pub uuid::Uuid);
28
29impl TaskId {
30 /// Create a new unique task ID
31 ///
32 /// # Example
33 ///
34 /// ```rust
35 /// use reinhardt_tasks::TaskId;
36 ///
37 /// let id = TaskId::new();
38 /// println!("Task ID: {}", id);
39 /// ```
40 pub fn new() -> Self {
41 Self(uuid::Uuid::new_v4())
42 }
43}
44
45impl Default for TaskId {
46 fn default() -> Self {
47 Self::new()
48 }
49}
50
51impl fmt::Display for TaskId {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 write!(f, "{}", self.0)
54 }
55}
56
57impl FromStr for TaskId {
58 type Err = uuid::Error;
59
60 fn from_str(s: &str) -> Result<Self, Self::Err> {
61 Ok(Self(uuid::Uuid::parse_str(s)?))
62 }
63}
64
65/// Status of a task
66///
67/// # Example
68///
69/// ```rust
70/// use reinhardt_tasks::TaskStatus;
71///
72/// let status = TaskStatus::Pending;
73/// assert_eq!(status, TaskStatus::Pending);
74/// ```
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
76pub enum TaskStatus {
77 /// The task is waiting to be executed.
78 Pending,
79 /// The task is currently being executed.
80 Running,
81 /// The task completed successfully.
82 Success,
83 /// The task failed during execution.
84 Failure,
85 /// The task is scheduled for retry after a failure.
86 Retry,
87}
88
89/// Task priority (0-9, where 9 is highest)
90///
91/// # Example
92///
93/// ```rust
94/// use reinhardt_tasks::TaskPriority;
95///
96/// let high = TaskPriority::new(9);
97/// let low = TaskPriority::new(0);
98/// assert!(high > low);
99/// assert_eq!(high.value(), 9);
100/// ```
101#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
102pub struct TaskPriority(i32);
103
104impl TaskPriority {
105 /// Create a new task priority, clamped to valid range (0-9)
106 ///
107 /// # Example
108 ///
109 /// ```rust
110 /// use reinhardt_tasks::TaskPriority;
111 ///
112 /// let p1 = TaskPriority::new(5);
113 /// assert_eq!(p1.value(), 5);
114 ///
115 /// // Out of range values are clamped
116 /// let p2 = TaskPriority::new(100);
117 /// assert_eq!(p2.value(), 9);
118 ///
119 /// let p3 = TaskPriority::new(-10);
120 /// assert_eq!(p3.value(), 0);
121 /// ```
122 pub fn new(priority: i32) -> Self {
123 Self(priority.clamp(TASK_MIN_PRIORITY, TASK_MAX_PRIORITY))
124 }
125
126 /// Get the priority value
127 ///
128 /// # Example
129 ///
130 /// ```rust
131 /// use reinhardt_tasks::TaskPriority;
132 ///
133 /// let priority = TaskPriority::new(7);
134 /// assert_eq!(priority.value(), 7);
135 /// ```
136 pub fn value(&self) -> i32 {
137 self.0
138 }
139}
140
141impl Default for TaskPriority {
142 fn default() -> Self {
143 Self(5)
144 }
145}
146
147/// Core trait that all tasks must implement, providing identity and metadata.
148pub trait Task: Send + Sync {
149 /// Returns the unique identifier for this task instance.
150 fn id(&self) -> TaskId;
151 /// Returns the name of this task type.
152 fn name(&self) -> &str;
153 /// Returns the priority of this task. Defaults to medium priority (5).
154 fn priority(&self) -> TaskPriority {
155 TaskPriority::default()
156 }
157}
158
159/// Trait for tasks that can be executed asynchronously.
160#[async_trait]
161pub trait TaskExecutor: Task {
162 /// Executes the task and returns the result.
163 async fn execute(&self) -> crate::TaskResult<()>;
164}