Skip to main content

butterflow_models/
task.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4use ts_rs::TS;
5use uuid::Uuid;
6
7/// Status of a task
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
9pub enum TaskStatus {
10    /// Task hasn't started execution yet
11    Pending,
12
13    /// Task is currently being executed
14    Running,
15
16    /// Task has completed successfully
17    Completed,
18
19    /// Task execution failed with an error
20    Failed,
21
22    /// Task is waiting for a manual trigger
23    AwaitingTrigger,
24
25    /// Task is blocked by dependencies
26    Blocked,
27
28    /// Task will not be executed
29    WontDo,
30}
31
32/// Represents a task (runtime instance of a node)
33#[derive(Debug, Clone, Serialize, Deserialize, TS)]
34pub struct Task {
35    /// Unique identifier for the task
36    pub id: Uuid,
37
38    /// ID of the workflow run this task belongs to
39    pub workflow_run_id: Uuid,
40
41    /// ID of the node this task is an instance of
42    pub node_id: String,
43
44    /// Current status of the task
45    pub status: TaskStatus,
46
47    /// Whether or not this task is a master task for other matrix tasks.
48    pub is_master: bool,
49
50    /// For matrix tasks, the master task ID
51    #[serde(default)]
52    #[ts(optional=nullable)]
53    pub master_task_id: Option<Uuid>,
54
55    /// For matrix tasks, the matrix values
56    #[serde(default)]
57    #[ts(optional=nullable)]
58    pub matrix_values: Option<HashMap<String, serde_json::Value>>,
59
60    /// Start time of the task
61    #[serde(default)]
62    #[ts(optional=nullable)]
63    pub started_at: Option<DateTime<Utc>>,
64
65    /// End time of the task (if completed or failed)
66    #[serde(default)]
67    #[ts(optional=nullable)]
68    pub ended_at: Option<DateTime<Utc>>,
69
70    /// Error message (if failed)
71    #[serde(default)]
72    #[ts(optional=nullable)]
73    pub error: Option<String>,
74
75    /// Logs from the task
76    #[serde(default)]
77    pub logs: Vec<String>,
78}
79
80impl Task {
81    /// Create a new task
82    pub fn new(workflow_run_id: Uuid, node_id: String, is_master: bool) -> Self {
83        Self {
84            id: Uuid::new_v4(),
85            workflow_run_id,
86            node_id,
87            is_master,
88            status: TaskStatus::Pending,
89            master_task_id: None,
90            matrix_values: None,
91            started_at: None,
92            ended_at: None,
93            error: None,
94            logs: Vec::new(),
95        }
96    }
97
98    /// Create a new matrix task
99    pub fn new_matrix(
100        workflow_run_id: Uuid,
101        node_id: String,
102        master_task_id: Uuid,
103        matrix_values: HashMap<String, serde_json::Value>,
104    ) -> Self {
105        Self {
106            id: Uuid::new_v4(),
107            workflow_run_id,
108            node_id,
109            status: TaskStatus::Pending,
110            master_task_id: Some(master_task_id),
111            matrix_values: Some(matrix_values),
112            started_at: None,
113            ended_at: None,
114            error: None,
115            logs: Vec::new(),
116            is_master: false,
117        }
118    }
119}