Skip to main content

TaskHandler

Trait TaskHandler 

Source
pub trait TaskHandler: Send + Sync {
    // Required methods
    fn task_type(&self) -> i16;
    fn execute<'life0, 'async_trait>(
        &'life0 self,
        payload: Value,
    ) -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

任务处理器特质

每个业务模块实现此特质以处理特定类型的异步任务。

§示例

struct ExportHandler;

#[async_trait]
impl TaskHandler for ExportHandler {
    fn task_type(&self) -> i16 { 1 }

    async fn execute(&self, payload: Value) -> Result<Value, String> {
        let file_id = payload["file_id"].as_str().unwrap();
        // 执行导出逻辑 ...
        Ok(serde_json::json!({"url": "https://..."}))
    }
}

Required Methods§

Source

fn task_type(&self) -> i16

返回该处理器对应的 task_type

Source

fn execute<'life0, 'async_trait>( &'life0 self, payload: Value, ) -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

执行任务,返回结果 JSON

  • payload: 任务携带的业务数据
  • 成功时返回 Ok(Value),结果写入 task_results
  • 失败时返回 Err(String),框架根据重试策略决定后续处理

Implementors§