Skip to main content

TaskExecutor

Trait TaskExecutor 

Source
pub trait TaskExecutor: Send + Sync {
    // Required methods
    fn execute<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        job: &'life1 NewScheduledJob,
        ctx: &'life2 ExecutionContext,
    ) -> Pin<Box<dyn Future<Output = Result<ExecutionResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn cancel<'life0, 'life1, 'async_trait>(
        &'life0 self,
        job_id: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn name(&self) -> &str;
}
Expand description

任务执行器 trait

定义任务执行的标准接口,支持不同的执行策略(主会话、隔离会话等)。

§需求映射

  • Requirement 7.7: 任务执行器 trait 定义

§实现者

  • MainSessionExecutor: 在主会话中执行任务
  • IsolatedSessionExecutor: 在隔离会话中执行任务

§示例

use aster::scheduler::executor::{TaskExecutor, ExecutionResult, ExecutionContext};

struct MyExecutor;

#[async_trait]
impl TaskExecutor for MyExecutor {
    async fn execute(
        &self,
        job: &ScheduledJob,
        ctx: &ExecutionContext,
    ) -> Result<ExecutionResult> {
        // 执行任务逻辑
        Ok(ExecutionResult::success("session-id", None, 100))
    }

    async fn cancel(&self, job_id: &str) -> Result<()> {
        // 取消任务逻辑
        Ok(())
    }
}

Required Methods§

Source

fn execute<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, job: &'life1 NewScheduledJob, ctx: &'life2 ExecutionContext, ) -> Pin<Box<dyn Future<Output = Result<ExecutionResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

执行任务

§参数
  • job: 要执行的调度任务
  • ctx: 执行上下文
§返回值
  • Ok(ExecutionResult): 执行结果
  • Err: 执行过程中的错误
Source

fn cancel<'life0, 'life1, 'async_trait>( &'life0 self, job_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

取消执行

§参数
  • job_id: 要取消的任务 ID
§返回值
  • Ok(()): 取消成功
  • Err: 取消失败
Source

fn name(&self) -> &str

获取执行器名称

Implementors§