taskflow-rs 0.1.1

A high-performance, async-first task orchestration framework for Rust
Documentation
pub mod definition;
pub mod handler;
pub mod result;
pub mod status;

use chrono::{DateTime, Utc};
use std::collections::HashMap;

pub use definition::TaskDefinition;
pub use handler::TaskHandler;
pub use result::TaskResult;
pub use status::TaskStatus;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Task {
    pub definition: TaskDefinition,
    pub status: TaskStatus,
    pub result: Option<TaskResult>,
    pub retry_count: u32,
    pub started_at: Option<DateTime<Utc>>,
    pub completed_at: Option<DateTime<Utc>>,
    pub assigned_worker: Option<String>,
    pub execution_log: Vec<String>,
}

impl Task {
    pub fn new(definition: TaskDefinition) -> Self {
        Self {
            definition,
            status: TaskStatus::Pending,
            result: None,
            retry_count: 0,
            started_at: None,
            completed_at: None,
            assigned_worker: None,
            execution_log: Vec::new(),
        }
    }

    pub fn start_execution(&mut self, worker_id: &str) {
        self.status = TaskStatus::Running;
        self.started_at = Some(Utc::now());
        self.assigned_worker = Some(worker_id.to_string());
        self.add_log(&format!("Task started by worker: {}", worker_id));
    }

    pub fn complete_execution(&mut self, result: TaskResult) {
        self.status = if result.success {
            TaskStatus::Completed
        } else {
            TaskStatus::Failed
        };
        self.result = Some(result);
        self.completed_at = Some(Utc::now());
        self.add_log("Task execution completed");
    }

    pub fn fail_execution(&mut self, error: &str) {
        self.status = TaskStatus::Failed;
        self.result = Some(TaskResult {
            success: false,
            output: None,
            error: Some(error.to_string()),
            execution_time_ms: 0,
            metadata: HashMap::new(),
        });
        self.completed_at = Some(Utc::now());
        self.add_log(&format!("Task failed: {}", error));
    }

    pub fn retry(&mut self) {
        self.retry_count += 1;
        self.status = TaskStatus::Retrying;
        self.started_at = None;
        self.completed_at = None;
        self.assigned_worker = None;
        self.add_log(&format!("Task retry attempt: {}", self.retry_count));
    }

    pub fn cancel(&mut self) {
        self.status = TaskStatus::Cancelled;
        self.completed_at = Some(Utc::now());
        self.add_log("Task cancelled");
    }

    pub fn can_retry(&self) -> bool {
        self.retry_count < self.definition.max_retries && self.status.can_retry()
    }

    pub fn is_ready_to_execute(&self) -> bool {
        matches!(self.status, TaskStatus::Pending | TaskStatus::Retrying)
    }

    pub fn is_finished(&self) -> bool {
        self.status.is_finished()
    }

    pub fn add_log(&mut self, message: &str) {
        let timestamp = Utc::now().format("%Y-%m-%d %H:%M:%S UTC");
        self.execution_log
            .push(format!("[{}] {}", timestamp, message));
    }
}