use crate::workflow::TaskOptions;
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Debug, Clone)]
pub enum WorkflowCommand {
ScheduleTask(ScheduleTaskCommand),
StartTimer(StartTimerCommand),
CompleteWorkflow(CompleteWorkflowCommand),
FailWorkflow(FailWorkflowCommand),
StartChildWorkflow(StartChildWorkflowCommand),
CancelTimer(CancelTimerCommand),
SendEvent(SendEventCommand),
CancelChildWorkflow(CancelChildWorkflowCommand),
WaitForEvent(WaitForEventCommand),
RestartFresh(RestartFreshCommand),
RecordStepResult(RecordStepResultCommand),
}
#[derive(Debug, Clone)]
pub struct ScheduleTaskCommand {
pub sequence: u64,
pub task_id: String,
pub task_type: String,
pub task_queue: String,
pub input: Vec<u8>,
pub timeout: Duration,
pub heartbeat_timeout: Option<Duration>,
pub queue_timeout: Option<Duration>,
pub retry_policy: Option<TaskRetryPolicy>,
pub headers: Vec<(String, Vec<u8>)>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskRetryPolicy {
pub max_attempts: u32,
pub initial_interval: Duration,
pub max_interval: Duration,
pub backoff_coefficient: f64,
pub non_retryable_errors: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct RecordStepResultCommand {
pub step_name: String,
pub step_type: i32,
pub result: Vec<u8>,
pub failure: Option<orcher_proto::orcher::v1::Failure>,
pub execution_attempt: i32,
}
#[derive(Debug, Clone)]
pub struct StartTimerCommand {
pub sequence: u64,
pub timer_id: String,
pub duration: Duration,
}
#[derive(Debug, Clone)]
pub struct CompleteWorkflowCommand {
pub result: Vec<u8>,
}
#[derive(Debug, Clone)]
pub struct FailWorkflowCommand {
pub message: String,
pub details: Option<Vec<u8>>,
pub stack_trace: Option<String>,
}
#[derive(Debug, Clone)]
pub struct StartChildWorkflowCommand {
pub sequence: u64,
pub workflow_id: String,
pub workflow_type: String,
pub task_queue: String,
pub input: Vec<u8>,
pub timeout: Option<Duration>,
pub orphan_policy: OrphanPolicy,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum OrphanPolicy {
Terminate,
Abandon,
Cancel,
}
#[derive(Debug, Clone)]
pub struct CancelTimerCommand {
pub sequence: u64,
pub timer_id: String,
}
#[derive(Debug, Clone)]
pub struct SendEventCommand {
pub sequence: u64,
pub workflow_id: String,
pub event_name: String,
pub data: Vec<u8>,
}
#[derive(Debug, Clone)]
pub struct CancelChildWorkflowCommand {
pub sequence: u64,
pub workflow_id: String,
}
#[derive(Debug, Clone)]
pub struct WaitForEventCommand {
pub sequence: u64,
pub event_name: String,
pub timeout: Option<Duration>,
}
#[derive(Debug, Clone)]
pub struct RestartFreshCommand {
pub workflow_type: Option<String>,
pub input: Vec<u8>,
pub task_queue: Option<String>,
pub timeout: Option<Duration>,
}
impl WorkflowCommand {
pub fn is_terminal(&self) -> bool {
matches!(
self,
WorkflowCommand::CompleteWorkflow(_)
| WorkflowCommand::FailWorkflow(_)
| WorkflowCommand::RestartFresh(_)
)
}
pub fn command_type(&self) -> &'static str {
match self {
WorkflowCommand::ScheduleTask(_) => "ScheduleTask",
WorkflowCommand::StartTimer(_) => "StartTimer",
WorkflowCommand::CompleteWorkflow(_) => "CompleteWorkflow",
WorkflowCommand::FailWorkflow(_) => "FailWorkflow",
WorkflowCommand::StartChildWorkflow(_) => "StartChildWorkflow",
WorkflowCommand::CancelTimer(_) => "CancelTimer",
WorkflowCommand::SendEvent(_) => "SendEvent",
WorkflowCommand::CancelChildWorkflow(_) => "CancelChildWorkflow",
WorkflowCommand::WaitForEvent(_) => "WaitForEvent",
WorkflowCommand::RestartFresh(_) => "RestartFresh",
WorkflowCommand::RecordStepResult(_) => "RecordStepResult",
}
}
}
impl From<&TaskOptions> for Option<TaskRetryPolicy> {
fn from(options: &TaskOptions) -> Self {
options.retry_policy.as_ref().map(|policy| TaskRetryPolicy {
max_attempts: policy.max_attempts,
initial_interval: policy.initial_interval,
max_interval: policy.max_interval,
backoff_coefficient: policy.backoff_coefficient,
non_retryable_errors: policy.non_retryable_errors.clone(),
})
}
}
impl From<crate::workflow::options::OrphanPolicy> for OrphanPolicy {
fn from(policy: crate::workflow::options::OrphanPolicy) -> Self {
match policy {
crate::workflow::options::OrphanPolicy::Terminate => OrphanPolicy::Terminate,
crate::workflow::options::OrphanPolicy::Abandon => OrphanPolicy::Abandon,
crate::workflow::options::OrphanPolicy::Cancel => OrphanPolicy::Cancel,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_command_is_terminal() {
let complete =
WorkflowCommand::CompleteWorkflow(CompleteWorkflowCommand { result: vec![] });
assert!(complete.is_terminal());
let fail = WorkflowCommand::FailWorkflow(FailWorkflowCommand {
message: "error".to_string(),
details: None,
stack_trace: None,
});
assert!(fail.is_terminal());
let timer = WorkflowCommand::StartTimer(StartTimerCommand {
sequence: 1,
timer_id: "timer-1".to_string(),
duration: Duration::from_secs(60),
});
assert!(!timer.is_terminal());
}
#[test]
fn test_command_type() {
let task = WorkflowCommand::ScheduleTask(ScheduleTaskCommand {
sequence: 1,
task_id: "task-1".to_string(),
task_type: "MyTask".to_string(),
task_queue: "default".to_string(),
input: vec![],
timeout: Duration::from_secs(300),
heartbeat_timeout: None,
queue_timeout: None,
retry_policy: None,
headers: vec![],
});
assert_eq!(task.command_type(), "ScheduleTask");
}
#[test]
fn test_retry_policy_from_task_options() {
use crate::workflow::{RetryPolicy, TaskOptions};
let options = TaskOptions {
retry_policy: Some(RetryPolicy {
max_attempts: 5,
initial_interval: Duration::from_secs(1),
max_interval: Duration::from_secs(30),
backoff_coefficient: 2.0,
jitter: crate::task::JitterStrategy::None,
non_retryable_errors: vec![],
}),
..Default::default()
};
let retry: Option<TaskRetryPolicy> = (&options).into();
assert!(retry.is_some());
let retry = retry.unwrap();
assert_eq!(retry.max_attempts, 5);
assert_eq!(retry.backoff_coefficient, 2.0);
}
}