use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use crate::service::ValorServiceId;
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Serialize, Deserialize, ToSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum ValorTaskPriority {
Low = 0,
#[default]
Normal = 1,
High = 2,
Critical = 3,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum ValorTaskStatus {
Pending,
Assigned,
Running,
Completed,
Failed,
Cancelled,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, ToSchema)]
pub struct ValorTaskId(pub String);
impl ValorTaskId {
pub fn new(id: String) -> Self {
Self(id)
}
pub fn generate() -> Self {
Self(uuid::Uuid::new_v4().to_string())
}
}
impl std::fmt::Display for ValorTaskId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<String> for ValorTaskId {
fn from(id: String) -> Self {
Self(id)
}
}
impl AsRef<str> for ValorTaskId {
fn as_ref(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum ValorTaskType {
ExecuteService {
service_id: ValorServiceId,
method: Option<String>,
},
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum ValorTaskInput {
Binary(Vec<u8>),
Json(serde_json::Value),
Text(String),
FilePath(String),
Empty,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum ValorTaskOutput {
Binary(Vec<u8>),
Json(serde_json::Value),
Text(String),
FilePath(String),
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum ValorTaskError {
ExecutionFailed {
code: TaskErrorCode,
message: String,
context: Option<String>,
},
Timeout {
timeout_ms: u64,
},
ResourceUnavailable {
resource_type: String,
details: String,
},
ServiceUnavailable {
service_name: String,
reason: String,
},
Unknown(String),
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum TaskErrorCode {
ExecutionError = 1000,
InvalidInput = 1001,
RuntimeError = 1002,
NetworkError = 1003,
PermissionError = 1004,
ConfigurationError = 1005,
}
impl std::fmt::Display for ValorTaskError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ValorTaskError::ExecutionFailed {
code,
message,
context,
} => {
write!(f, "Execution failed ({}): {}", *code as u32, message)?;
if let Some(ctx) = context {
write!(f, " (context: {ctx})")?;
}
Ok(())
}
ValorTaskError::Timeout { timeout_ms } => {
write!(f, "Task timeout after {timeout_ms}ms")
}
ValorTaskError::ResourceUnavailable {
resource_type,
details,
} => write!(f, "Resource unavailable {resource_type}: {details}"),
ValorTaskError::ServiceUnavailable {
service_name,
reason,
} => write!(f, "Service '{service_name}' unavailable: {reason}"),
ValorTaskError::Unknown(msg) => write!(f, "Unknown error: {msg}"),
}
}
}
impl std::error::Error for ValorTaskError {}
impl From<String> for ValorTaskError {
fn from(msg: String) -> Self {
ValorTaskError::Unknown(msg)
}
}
impl From<&str> for ValorTaskError {
fn from(msg: &str) -> Self {
ValorTaskError::Unknown(msg.to_string())
}
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub struct ValorTask {
pub task_id: ValorTaskId,
pub task_type: ValorTaskType,
pub priority: ValorTaskPriority,
#[serde(default)]
pub timeout_ms: Option<u64>,
#[serde(default)]
pub attempt: u32,
pub status: ValorTaskStatus,
pub assigned_worker: Option<String>,
pub created_at: u64,
pub assigned_at: Option<u64>,
pub completed_at: Option<u64>,
pub input: ValorTaskInput,
pub output: Option<ValorTaskOutput>,
pub error: Option<ValorTaskError>,
}
pub type ValorMasterTask = ValorTask;