alun_task/handler.rs
1use async_trait::async_trait;
2use serde_json::Value;
3
4/// 任务处理器特质
5///
6/// 每个业务模块实现此特质以处理特定类型的异步任务。
7///
8/// ## 示例
9///
10/// ```ignore
11/// struct ExportHandler;
12///
13/// #[async_trait]
14/// impl TaskHandler for ExportHandler {
15/// fn task_type(&self) -> i16 { 1 }
16///
17/// async fn execute(&self, payload: Value) -> Result<Value, String> {
18/// let file_id = payload["file_id"].as_str().unwrap();
19/// // 执行导出逻辑 ...
20/// Ok(serde_json::json!({"url": "https://..."}))
21/// }
22/// }
23/// ```
24#[async_trait]
25pub trait TaskHandler: Send + Sync {
26 /// 返回该处理器对应的 task_type
27 fn task_type(&self) -> i16;
28
29 /// 执行任务,返回结果 JSON
30 ///
31 /// - `payload`: 任务携带的业务数据
32 /// - 成功时返回 `Ok(Value)`,结果写入 `task_results` 表
33 /// - 失败时返回 `Err(String)`,框架根据重试策略决定后续处理
34 async fn execute(&self, payload: Value) -> Result<Value, String>;
35}