wfe_core/models/
execution_pointer.rs1use std::collections::HashMap;
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6use super::status::PointerStatus;
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct ExecutionPointer {
11 pub id: String,
13 pub step_id: usize,
15 pub active: bool,
17 pub status: PointerStatus,
19 pub sleep_until: Option<DateTime<Utc>>,
21 pub persistence_data: Option<serde_json::Value>,
23 pub start_time: Option<DateTime<Utc>>,
25 pub end_time: Option<DateTime<Utc>>,
27 pub event_name: Option<String>,
29 pub event_key: Option<String>,
31 pub event_published: bool,
33 pub event_data: Option<serde_json::Value>,
35 pub step_name: Option<String>,
37 pub retry_count: u32,
39 pub children: Vec<String>,
41 pub context_item: Option<serde_json::Value>,
43 pub predecessor_id: Option<String>,
45 pub outcome: Option<serde_json::Value>,
47 pub scope: Vec<String>,
49 pub extension_attributes: HashMap<String, serde_json::Value>,
51}
52
53impl ExecutionPointer {
54 pub fn new(step_id: usize) -> Self {
55 Self {
56 id: uuid::Uuid::new_v4().to_string(),
57 step_id,
58 active: true,
59 status: PointerStatus::Pending,
60 sleep_until: None,
61 persistence_data: None,
62 start_time: None,
63 end_time: None,
64 event_name: None,
65 event_key: None,
66 event_published: false,
67 event_data: None,
68 step_name: None,
69 retry_count: 0,
70 children: Vec::new(),
71 context_item: None,
72 predecessor_id: None,
73 outcome: None,
74 scope: Vec::new(),
75 extension_attributes: HashMap::new(),
76 }
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83 use pretty_assertions::assert_eq;
84
85 #[test]
86 fn new_pointer_has_correct_defaults() {
87 let pointer = ExecutionPointer::new(0);
88 assert_eq!(pointer.step_id, 0);
89 assert!(pointer.active);
90 assert_eq!(pointer.status, PointerStatus::Pending);
91 assert_eq!(pointer.retry_count, 0);
92 assert!(pointer.children.is_empty());
93 assert!(pointer.scope.is_empty());
94 assert!(!pointer.event_published);
95 }
96
97 #[test]
98 fn new_pointer_generates_unique_ids() {
99 let p1 = ExecutionPointer::new(0);
100 let p2 = ExecutionPointer::new(0);
101 assert_ne!(p1.id, p2.id);
102 }
103
104 #[test]
105 fn serde_round_trip() {
106 let mut pointer = ExecutionPointer::new(3);
107 pointer.status = PointerStatus::Running;
108 pointer.retry_count = 2;
109 pointer.persistence_data = Some(serde_json::json!({"step_state": true}));
110 pointer.children = vec!["child-1".into(), "child-2".into()];
111
112 let json = serde_json::to_string(&pointer).unwrap();
113 let deserialized: ExecutionPointer = serde_json::from_str(&json).unwrap();
114
115 assert_eq!(pointer.id, deserialized.id);
116 assert_eq!(pointer.step_id, deserialized.step_id);
117 assert_eq!(pointer.status, deserialized.status);
118 assert_eq!(pointer.retry_count, deserialized.retry_count);
119 assert_eq!(pointer.persistence_data, deserialized.persistence_data);
120 assert_eq!(pointer.children, deserialized.children);
121 }
122}