use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum OrphanPolicy {
Terminate,
Abandon,
#[default]
Cancel,
}
pub use crate::task::RetryPolicy;
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct TaskOptions {
pub timeout: Option<Duration>,
pub task_id: Option<String>,
pub retry_policy: Option<RetryPolicy>,
pub heartbeat_timeout: Option<Duration>,
pub queue_timeout: Option<Duration>,
}
impl TaskOptions {
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
pub fn with_task_id(mut self, task_id: impl Into<String>) -> Self {
self.task_id = Some(task_id.into());
self
}
pub fn with_retry_policy(mut self, retry_policy: RetryPolicy) -> Self {
self.retry_policy = Some(retry_policy);
self
}
pub fn with_heartbeat_timeout(mut self, timeout: Duration) -> Self {
self.heartbeat_timeout = Some(timeout);
self
}
pub fn with_queue_timeout(mut self, timeout: Duration) -> Self {
self.queue_timeout = Some(timeout);
self
}
}
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct ChildWorkflowOptions {
pub workflow_id: Option<String>,
pub execution_timeout: Option<Duration>,
pub task_queue: Option<String>,
pub orphan_policy: OrphanPolicy,
}
impl ChildWorkflowOptions {
pub fn new() -> Self {
Self::default()
}
pub fn workflow_id(mut self, workflow_id: impl Into<String>) -> Self {
self.workflow_id = Some(workflow_id.into());
self
}
pub fn execution_timeout(mut self, timeout: Duration) -> Self {
self.execution_timeout = Some(timeout);
self
}
pub fn task_queue(mut self, queue: impl Into<String>) -> Self {
self.task_queue = Some(queue.into());
self
}
pub fn orphan_policy(mut self, policy: OrphanPolicy) -> Self {
self.orphan_policy = policy;
self
}
}
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct RestartFreshOptions {
pub workflow_type: Option<String>,
pub task_queue: Option<String>,
pub timeout: Option<Duration>,
}
impl RestartFreshOptions {
pub fn new() -> Self {
Self::default()
}
pub fn workflow_type(mut self, workflow_type: impl Into<String>) -> Self {
self.workflow_type = Some(workflow_type.into());
self
}
pub fn task_queue(mut self, task_queue: impl Into<String>) -> Self {
self.task_queue = Some(task_queue.into());
self
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_task_options_defaults() {
let options = TaskOptions::default();
assert!(options.timeout.is_none());
assert!(options.task_id.is_none());
assert!(options.retry_policy.is_none());
assert!(options.heartbeat_timeout.is_none());
}
#[test]
fn test_child_workflow_options_defaults() {
let options = ChildWorkflowOptions::default();
assert!(options.workflow_id.is_none());
assert!(options.execution_timeout.is_none());
assert!(options.task_queue.is_none());
assert_eq!(options.orphan_policy, OrphanPolicy::Cancel);
}
#[test]
fn test_orphan_policy_default() {
assert_eq!(OrphanPolicy::default(), OrphanPolicy::Cancel);
}
#[test]
fn test_orphan_policy_variants() {
let terminate = OrphanPolicy::Terminate;
let abandon = OrphanPolicy::Abandon;
let request_cancel = OrphanPolicy::Cancel;
assert_ne!(terminate, abandon);
assert_ne!(terminate, request_cancel);
assert_ne!(abandon, request_cancel);
}
#[test]
fn test_restart_fresh_options_defaults() {
let options = RestartFreshOptions::default();
assert!(options.workflow_type.is_none());
assert!(options.task_queue.is_none());
assert!(options.timeout.is_none());
}
#[test]
fn test_restart_fresh_options_builder() {
let options = RestartFreshOptions::new()
.workflow_type("NewWorkflow")
.task_queue("new-queue")
.timeout(Duration::from_secs(600));
assert_eq!(options.workflow_type.as_deref(), Some("NewWorkflow"));
assert_eq!(options.task_queue.as_deref(), Some("new-queue"));
assert_eq!(options.timeout, Some(Duration::from_secs(600)));
}
}