wfe_core/primitives/
delay.rs1use std::time::Duration;
2
3use async_trait::async_trait;
4
5use crate::models::ExecutionResult;
6use crate::traits::step::{StepBody, StepExecutionContext};
7
8pub struct DelayStep {
10 pub duration: Duration,
12}
13
14impl Default for DelayStep {
15 fn default() -> Self {
16 Self {
17 duration: Duration::ZERO,
18 }
19 }
20}
21
22#[async_trait]
23impl StepBody for DelayStep {
24 async fn run(&mut self, context: &StepExecutionContext<'_>) -> crate::Result<ExecutionResult> {
25 let duration = if self.duration == Duration::ZERO {
27 context
28 .step
29 .step_config
30 .as_ref()
31 .and_then(|c| c.get("duration_millis"))
32 .and_then(|v| v.as_u64())
33 .map(Duration::from_millis)
34 .unwrap_or(self.duration)
35 } else {
36 self.duration
37 };
38 Ok(ExecutionResult::sleep(duration, None))
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45 use crate::models::ExecutionPointer;
46 use crate::primitives::test_helpers::*;
47
48 #[tokio::test]
49 async fn returns_correct_sleep_duration() {
50 let mut step = DelayStep {
51 duration: Duration::from_secs(60),
52 };
53 let pointer = ExecutionPointer::new(0);
54 let wf_step = default_step();
55 let workflow = default_workflow();
56 let ctx = make_context(&pointer, &wf_step, &workflow);
57
58 let result = step.run(&ctx).await.unwrap();
59 assert!(!result.proceed);
60 assert_eq!(result.sleep_for, Some(Duration::from_secs(60)));
61 assert!(result.persistence_data.is_none());
62 }
63
64 #[tokio::test]
65 async fn returns_zero_duration() {
66 let mut step = DelayStep {
67 duration: Duration::ZERO,
68 };
69 let pointer = ExecutionPointer::new(0);
70 let wf_step = default_step();
71 let workflow = default_workflow();
72 let ctx = make_context(&pointer, &wf_step, &workflow);
73
74 let result = step.run(&ctx).await.unwrap();
75 assert_eq!(result.sleep_for, Some(Duration::ZERO));
76 }
77}