Skip to main content

assay_workflow/
state.rs

1use serde::{Deserialize, Serialize};
2
3use crate::types::WorkflowStatus;
4
5/// Commands yielded by a workflow execution turn.
6/// The engine processes these to advance workflow state.
7#[derive(Clone, Debug, Serialize, Deserialize)]
8#[serde(tag = "type")]
9pub enum WorkflowCommand {
10    ScheduleActivity {
11        name: String,
12        input: Option<String>,
13        task_queue: Option<String>,
14        #[serde(default = "default_max_attempts")]
15        max_attempts: i32,
16        #[serde(default = "default_initial_interval")]
17        initial_interval_secs: f64,
18        #[serde(default = "default_backoff")]
19        backoff_coefficient: f64,
20        #[serde(default = "default_start_to_close")]
21        start_to_close_secs: f64,
22        heartbeat_timeout_secs: Option<f64>,
23    },
24    StartTimer {
25        duration_secs: f64,
26    },
27    CompleteWorkflow {
28        result: Option<String>,
29    },
30    FailWorkflow {
31        error: String,
32    },
33    StartChildWorkflow {
34        workflow_type: String,
35        workflow_id: String,
36        input: Option<String>,
37        task_queue: Option<String>,
38    },
39    ContinueAsNew {
40        input: Option<String>,
41    },
42    SideEffect {
43        value: String,
44    },
45}
46
47fn default_max_attempts() -> i32 {
48    3
49}
50fn default_initial_interval() -> f64 {
51    1.0
52}
53fn default_backoff() -> f64 {
54    2.0
55}
56fn default_start_to_close() -> f64 {
57    300.0
58}
59
60/// Validates whether a workflow status transition is legal.
61pub fn is_valid_transition(from: WorkflowStatus, to: WorkflowStatus) -> bool {
62    use WorkflowStatus::*;
63    matches!(
64        (from, to),
65        // Normal forward transitions
66        (Pending, Running)
67            | (Running, Waiting)
68            | (Running, Completed)
69            | (Running, Failed)
70            | (Waiting, Running)
71            | (Waiting, Completed)
72            | (Waiting, Failed)
73            // Cancellation from any non-terminal state
74            | (Pending, Cancelled)
75            | (Running, Cancelled)
76            | (Waiting, Cancelled)
77            // Timeout from running/waiting
78            | (Running, TimedOut)
79            | (Waiting, TimedOut)
80    )
81}
82
83/// Result of processing a single workflow execution turn.
84#[derive(Debug)]
85pub enum TurnResult {
86    /// Workflow yielded commands and needs to continue
87    Continue(Vec<WorkflowCommand>),
88    /// Workflow completed with a result
89    Completed(Option<String>),
90    /// Workflow failed with an error
91    Failed(String),
92}