Skip to main content

dag_executor/tasks/
trait.rs

1//! The core [`Task`] abstraction.
2
3use crate::context::Context;
4use crate::error::TaskError;
5use async_trait::async_trait;
6use std::sync::Arc;
7
8/// The value a task produces on success.
9pub type TaskOutput = serde_json::Value;
10
11/// A unit of work in the DAG.
12///
13/// Implementations must be `Send + Sync` because tasks are shared across worker
14/// threads (`Arc<dyn Task>`) and executed concurrently. The default
15/// [`Task::dependencies`] and [`Task::priority`] make leaf, normal-priority
16/// tasks the zero-boilerplate case.
17#[async_trait]
18pub trait Task: Send + Sync {
19    /// Stable, unique identifier for this task within a DAG.
20    fn id(&self) -> &str;
21
22    /// Ids of tasks that must complete successfully before this one runs.
23    fn dependencies(&self) -> Vec<String> {
24        Vec::new()
25    }
26
27    /// Scheduling priority; higher values are scheduled first among ready tasks.
28    fn priority(&self) -> u8 {
29        0
30    }
31
32    /// Execute the task.
33    ///
34    /// The returned future is run on the executor's worker pool. Implementations
35    /// should periodically check [`Context::is_cancelled`] for long-running work.
36    async fn execute(&self, ctx: Arc<Context>) -> Result<TaskOutput, TaskError>;
37}