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