1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7#[repr(i16)]
8pub enum TaskStatus {
9 Pending = 1,
11 Processing = 2,
13 Completed = 3,
15 Failed = 4,
17 Cancelled = 5,
19 Scheduled = 6,
21 DeadLetter = 7,
23}
24
25impl TaskStatus {
26 pub fn from_i16(v: i16) -> Option<Self> {
28 match v {
29 1 => Some(Self::Pending),
30 2 => Some(Self::Processing),
31 3 => Some(Self::Completed),
32 4 => Some(Self::Failed),
33 5 => Some(Self::Cancelled),
34 6 => Some(Self::Scheduled),
35 7 => Some(Self::DeadLetter),
36 _ => None,
37 }
38 }
39
40 pub fn to_i16(self) -> i16 {
42 self as i16
43 }
44
45 pub fn is_terminal(self) -> bool {
47 matches!(self, Self::Completed | Self::Cancelled | Self::DeadLetter)
48 }
49}
50
51#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
53#[repr(i16)]
54pub enum TaskPriority {
55 Low = 1,
57 Normal = 2,
59 High = 3,
61 Critical = 4,
63}
64
65impl TaskPriority {
66 pub fn from_i16(v: i16) -> Self {
68 match v {
69 3 => Self::High,
70 4 => Self::Critical,
71 1 => Self::Low,
72 _ => Self::Normal,
73 }
74 }
75
76 pub fn to_i16(self) -> i16 {
78 self as i16
79 }
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
84pub enum RetryStrategy {
85 Fixed,
87 Linear,
89 Exponential,
91}
92
93#[derive(Debug, Clone)]
95pub struct TaskConfig {
96 pub task_type: i16,
98 pub priority: TaskPriority,
100 pub topic: String,
102 pub timeout_seconds: u64,
104 pub max_retries: u32,
106 pub retry_strategy: RetryStrategy,
108 pub retry_delay_seconds: u64,
110 pub max_retry_delay_seconds: u64,
112 pub description: &'static str,
114 pub dead_letter_topic: Option<String>,
116}
117
118#[derive(Debug, Clone, Deserialize)]
120pub struct TaskWorkerConfig {
121 pub brokers: String,
123 pub group_id: String,
125 pub scan_interval_secs: u64,
127 pub max_batch_size: usize,
129 pub max_message_age_secs: u64,
131 pub auto_create_topics: bool,
133 pub topic_partitions: i32,
135 pub topic_replication: i16,
137}
138
139impl Default for TaskWorkerConfig {
140 fn default() -> Self {
141 Self {
142 brokers: "localhost:9092".into(),
143 group_id: "alun-task-worker".into(),
144 scan_interval_secs: 30,
145 max_batch_size: 100,
146 max_message_age_secs: 3600,
147 auto_create_topics: false,
148 topic_partitions: 1,
149 topic_replication: 1,
150 }
151 }
152}
153
154#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
156#[repr(i16)]
157pub enum ResourceType {
158 User = 1,
160 Order = 2,
162 Product = 3,
164 Store = 4,
166 File = 5,
168}
169
170impl ResourceType {
171 pub fn from_i16(v: i16) -> Option<Self> {
173 match v {
174 1 => Some(Self::User),
175 2 => Some(Self::Order),
176 3 => Some(Self::Product),
177 4 => Some(Self::Store),
178 5 => Some(Self::File),
179 _ => None,
180 }
181 }
182
183 pub fn to_i16(self) -> i16 {
185 self as i16
186 }
187}
188
189#[derive(Debug, Clone)]
191pub struct SubmitTaskParams {
192 pub task_type: i16,
194 pub payload: serde_json::Value,
196 pub priority: Option<TaskPriority>,
198 pub user_id: Option<String>,
200 pub resource_id: Option<String>,
202 pub resource_type: Option<ResourceType>,
204}
205
206#[derive(Debug, Clone)]
208pub struct SubmitBatchParams {
209 pub tasks: Vec<SubmitTaskParams>,
211}
212
213#[derive(Debug, Clone, Serialize, Deserialize)]
215pub struct TaskMessage {
216 pub task_id: String,
218 pub task_type: i16,
220 pub payload: serde_json::Value,
222 pub priority: i16,
224 pub user_id: Option<String>,
226 pub resource_id: Option<String>,
228 pub resource_type: Option<i16>,
230 pub submitted_at: String,
232}