use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(i16)]
pub enum TaskStatus {
Pending = 1,
Processing = 2,
Completed = 3,
Failed = 4,
Cancelled = 5,
Scheduled = 6,
DeadLetter = 7,
}
impl TaskStatus {
pub fn from_i16(v: i16) -> Option<Self> {
match v {
1 => Some(Self::Pending),
2 => Some(Self::Processing),
3 => Some(Self::Completed),
4 => Some(Self::Failed),
5 => Some(Self::Cancelled),
6 => Some(Self::Scheduled),
7 => Some(Self::DeadLetter),
_ => None,
}
}
pub fn to_i16(self) -> i16 {
self as i16
}
pub fn is_terminal(self) -> bool {
matches!(self, Self::Completed | Self::Cancelled | Self::DeadLetter)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(i16)]
pub enum TaskPriority {
Low = 1,
Normal = 2,
High = 3,
Critical = 4,
}
impl TaskPriority {
pub fn from_i16(v: i16) -> Self {
match v {
3 => Self::High,
4 => Self::Critical,
1 => Self::Low,
_ => Self::Normal,
}
}
pub fn to_i16(self) -> i16 {
self as i16
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RetryStrategy {
Fixed,
Linear,
Exponential,
}
#[derive(Debug, Clone)]
pub struct TaskConfig {
pub task_type: i16,
pub priority: TaskPriority,
pub topic: String,
pub timeout_seconds: u64,
pub max_retries: u32,
pub retry_strategy: RetryStrategy,
pub retry_delay_seconds: u64,
pub max_retry_delay_seconds: u64,
pub description: &'static str,
pub dead_letter_topic: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct TaskWorkerConfig {
pub brokers: String,
pub group_id: String,
pub scan_interval_secs: u64,
pub max_batch_size: usize,
pub max_message_age_secs: u64,
pub auto_create_topics: bool,
pub topic_partitions: i32,
pub topic_replication: i16,
}
impl Default for TaskWorkerConfig {
fn default() -> Self {
Self {
brokers: "localhost:9092".into(),
group_id: "alun-task-worker".into(),
scan_interval_secs: 30,
max_batch_size: 100,
max_message_age_secs: 3600,
auto_create_topics: false,
topic_partitions: 1,
topic_replication: 1,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(i16)]
pub enum ResourceType {
User = 1,
Order = 2,
Product = 3,
Store = 4,
File = 5,
}
impl ResourceType {
pub fn from_i16(v: i16) -> Option<Self> {
match v {
1 => Some(Self::User),
2 => Some(Self::Order),
3 => Some(Self::Product),
4 => Some(Self::Store),
5 => Some(Self::File),
_ => None,
}
}
pub fn to_i16(self) -> i16 {
self as i16
}
}
#[derive(Debug, Clone)]
pub struct SubmitTaskParams {
pub task_type: i16,
pub payload: serde_json::Value,
pub priority: Option<TaskPriority>,
pub user_id: Option<String>,
pub resource_id: Option<String>,
pub resource_type: Option<ResourceType>,
}
#[derive(Debug, Clone)]
pub struct SubmitBatchParams {
pub tasks: Vec<SubmitTaskParams>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskMessage {
pub task_id: String,
pub task_type: i16,
pub payload: serde_json::Value,
pub priority: i16,
pub user_id: Option<String>,
pub resource_id: Option<String>,
pub resource_type: Option<i16>,
pub submitted_at: String,
}