293 lines
7.5 KiB
Rust
293 lines
7.5 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use std::fmt;
|
|
|
|
pub mod embedded_redis;
|
|
pub use embedded_redis::{EmbeddedRedisServer, EmbeddedRedisConfig, EmbeddedRedisManager};
|
|
|
|
/// 任务类型枚举
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
pub enum TaskType {
|
|
/// 文本处理任务
|
|
TextProcessing,
|
|
/// 数据分析任务
|
|
DataAnalysis,
|
|
/// AI对话任务
|
|
AIChat,
|
|
/// 文件处理任务
|
|
FileProcessing,
|
|
/// 自定义任务
|
|
Custom(String),
|
|
}
|
|
|
|
impl fmt::Display for TaskType {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
TaskType::TextProcessing => write!(f, "text_processing"),
|
|
TaskType::DataAnalysis => write!(f, "data_analysis"),
|
|
TaskType::AIChat => write!(f, "ai_chat"),
|
|
TaskType::FileProcessing => write!(f, "file_processing"),
|
|
TaskType::Custom(s) => write!(f, "custom_{}", s),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 任务状态枚举
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
pub enum TaskStatus {
|
|
/// 待处理
|
|
Pending,
|
|
/// 处理中
|
|
Processing,
|
|
/// 已完成
|
|
Completed,
|
|
/// 失败
|
|
Failed,
|
|
/// 已取消
|
|
Cancelled,
|
|
}
|
|
|
|
impl fmt::Display for TaskStatus {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
TaskStatus::Pending => write!(f, "pending"),
|
|
TaskStatus::Processing => write!(f, "processing"),
|
|
TaskStatus::Completed => write!(f, "completed"),
|
|
TaskStatus::Failed => write!(f, "failed"),
|
|
TaskStatus::Cancelled => write!(f, "cancelled"),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 任务请求数据结构
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct TaskRequest {
|
|
/// 用户ID
|
|
pub user_id: String,
|
|
/// 任务类型
|
|
pub task_type: TaskType,
|
|
/// 任务内容
|
|
pub content: String,
|
|
/// 时间戳
|
|
pub timestamp: i64,
|
|
/// 优先级 (1-10, 数字越大优先级越高)
|
|
pub priority: u8,
|
|
/// 超时时间(秒)
|
|
pub timeout: Option<u64>,
|
|
/// 额外参数
|
|
pub extra_params: Option<serde_json::Value>,
|
|
}
|
|
|
|
/// 任务响应数据结构
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct TaskResponse {
|
|
/// 是否成功
|
|
pub success: bool,
|
|
/// 消息
|
|
pub message: String,
|
|
/// 任务ID
|
|
pub task_id: Option<String>,
|
|
/// 结果数据
|
|
pub result: Option<serde_json::Value>,
|
|
/// 处理时间(毫秒)
|
|
pub processing_time: Option<u64>,
|
|
/// 错误信息
|
|
pub error: Option<String>,
|
|
}
|
|
|
|
/// 任务状态数据结构
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct TaskStatusResponse {
|
|
/// 任务ID
|
|
pub task_id: String,
|
|
/// 任务状态
|
|
pub status: TaskStatus,
|
|
/// 进度 (0-100)
|
|
pub progress: u8,
|
|
/// 状态消息
|
|
pub status_message: String,
|
|
/// 创建时间
|
|
pub created_at: i64,
|
|
/// 更新时间
|
|
pub updated_at: i64,
|
|
/// 结果数据
|
|
pub result: Option<serde_json::Value>,
|
|
}
|
|
|
|
/// 服务健康检查响应
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct HealthResponse {
|
|
/// 服务状态
|
|
pub status: String,
|
|
/// 服务名称
|
|
pub service: String,
|
|
/// 时间戳
|
|
pub timestamp: i64,
|
|
/// 版本号
|
|
pub version: String,
|
|
/// 额外信息
|
|
pub extra: Option<serde_json::Value>,
|
|
}
|
|
|
|
/// 错误响应数据结构
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct ErrorResponse {
|
|
/// 错误代码
|
|
pub code: String,
|
|
/// 错误消息
|
|
pub message: String,
|
|
/// 详细错误信息
|
|
pub details: Option<String>,
|
|
/// 时间戳
|
|
pub timestamp: i64,
|
|
}
|
|
|
|
/// WebSocket 消息类型
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub enum WebSocketMessage {
|
|
/// 任务请求
|
|
TaskRequest(TaskRequest),
|
|
/// 任务响应
|
|
TaskResponse(TaskResponse),
|
|
/// 任务状态更新
|
|
TaskStatusUpdate(TaskStatusResponse),
|
|
/// 心跳消息
|
|
Heartbeat,
|
|
/// 错误消息
|
|
Error(ErrorResponse),
|
|
}
|
|
|
|
/// 企业微信消息数据结构
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct WeChatMessage {
|
|
/// 消息ID
|
|
pub msg_id: String,
|
|
/// 发送者
|
|
pub from_user: String,
|
|
/// 接收者
|
|
pub to_user: String,
|
|
/// 消息类型
|
|
pub msg_type: String,
|
|
/// 消息内容
|
|
pub content: String,
|
|
/// 时间戳
|
|
pub timestamp: i64,
|
|
}
|
|
|
|
/// 微信小程序消息数据结构
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct MiniProgramMessage {
|
|
/// 用户ID
|
|
pub user_id: String,
|
|
/// 消息类型
|
|
pub msg_type: String,
|
|
/// 消息内容
|
|
pub content: String,
|
|
/// 时间戳
|
|
pub timestamp: i64,
|
|
/// 额外参数
|
|
pub extra: Option<serde_json::Value>,
|
|
}
|
|
|
|
/// 常量定义
|
|
pub mod constants {
|
|
/// 默认任务超时时间(秒)
|
|
pub const DEFAULT_TASK_TIMEOUT: u64 = 300;
|
|
|
|
/// 最大任务优先级
|
|
pub const MAX_TASK_PRIORITY: u8 = 10;
|
|
|
|
/// 最小任务优先级
|
|
pub const MIN_TASK_PRIORITY: u8 = 1;
|
|
|
|
/// 默认服务端口
|
|
pub const DEFAULT_GATEWAY_PORT: u16 = 3000;
|
|
pub const DEFAULT_SMARTCLAW_PORT: u16 = 3001;
|
|
|
|
/// WebSocket 心跳间隔(秒)
|
|
pub const WEBSOCKET_HEARTBEAT_INTERVAL: u64 = 30;
|
|
|
|
/// WebSocket 超时时间(秒)
|
|
pub const WEBSOCKET_TIMEOUT: u64 = 300;
|
|
}
|
|
|
|
/// 工具函数
|
|
pub mod utils {
|
|
use super::*;
|
|
use chrono::Utc;
|
|
|
|
/// 生成任务ID
|
|
pub fn generate_task_id(user_id: &str) -> String {
|
|
format!("task_{}_{}", user_id, Utc::now().timestamp_millis())
|
|
}
|
|
|
|
/// 生成消息ID
|
|
pub fn generate_msg_id() -> String {
|
|
format!("msg_{}", Utc::now().timestamp_millis())
|
|
}
|
|
|
|
/// 验证任务优先级
|
|
pub fn validate_priority(priority: u8) -> u8 {
|
|
priority.clamp(constants::MIN_TASK_PRIORITY, constants::MAX_TASK_PRIORITY)
|
|
}
|
|
|
|
/// 获取当前时间戳
|
|
pub fn current_timestamp() -> i64 {
|
|
Utc::now().timestamp()
|
|
}
|
|
|
|
/// 创建成功响应
|
|
pub fn create_success_response(message: &str, task_id: Option<String>, result: Option<serde_json::Value>) -> TaskResponse {
|
|
TaskResponse {
|
|
success: true,
|
|
message: message.to_string(),
|
|
task_id,
|
|
result,
|
|
processing_time: None,
|
|
error: None,
|
|
}
|
|
}
|
|
|
|
/// 创建错误响应
|
|
pub fn create_error_response(message: &str, error: Option<String>) -> TaskResponse {
|
|
TaskResponse {
|
|
success: false,
|
|
message: message.to_string(),
|
|
task_id: None,
|
|
result: None,
|
|
processing_time: None,
|
|
error,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_task_type_display() {
|
|
assert_eq!(TaskType::TextProcessing.to_string(), "text_processing");
|
|
assert_eq!(TaskType::DataAnalysis.to_string(), "data_analysis");
|
|
assert_eq!(TaskType::Custom("test".to_string()).to_string(), "custom_test");
|
|
}
|
|
|
|
#[test]
|
|
fn test_task_status_display() {
|
|
assert_eq!(TaskStatus::Pending.to_string(), "pending");
|
|
assert_eq!(TaskStatus::Completed.to_string(), "completed");
|
|
assert_eq!(TaskStatus::Failed.to_string(), "failed");
|
|
}
|
|
|
|
#[test]
|
|
fn test_generate_task_id() {
|
|
let task_id = utils::generate_task_id("user123");
|
|
assert!(task_id.starts_with("task_user123_"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_validate_priority() {
|
|
assert_eq!(utils::validate_priority(0), 1);
|
|
assert_eq!(utils::validate_priority(5), 5);
|
|
assert_eq!(utils::validate_priority(15), 10);
|
|
}
|
|
} |