Skip to main content

wfe_core/models/
scheduled_command.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
4pub enum CommandName {
5    ProcessWorkflow,
6    ProcessEvent,
7}
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct ScheduledCommand {
11    pub command_name: CommandName,
12    pub data: String,
13    /// Epoch milliseconds when the command should execute.
14    pub execute_time: i64,
15}
16
17impl ScheduledCommand {
18    pub fn process_workflow(workflow_id: impl Into<String>, execute_time: i64) -> Self {
19        Self {
20            command_name: CommandName::ProcessWorkflow,
21            data: workflow_id.into(),
22            execute_time,
23        }
24    }
25
26    pub fn process_event(event_id: impl Into<String>, execute_time: i64) -> Self {
27        Self {
28            command_name: CommandName::ProcessEvent,
29            data: event_id.into(),
30            execute_time,
31        }
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use pretty_assertions::assert_eq;
39
40    #[test]
41    fn process_workflow_factory() {
42        let cmd = ScheduledCommand::process_workflow("wf-123", 1000);
43        assert_eq!(cmd.command_name, CommandName::ProcessWorkflow);
44        assert_eq!(cmd.data, "wf-123");
45        assert_eq!(cmd.execute_time, 1000);
46    }
47
48    #[test]
49    fn process_event_factory() {
50        let cmd = ScheduledCommand::process_event("evt-456", 2000);
51        assert_eq!(cmd.command_name, CommandName::ProcessEvent);
52        assert_eq!(cmd.data, "evt-456");
53        assert_eq!(cmd.execute_time, 2000);
54    }
55
56    #[test]
57    fn serde_round_trip() {
58        let cmd = ScheduledCommand::process_workflow("wf-1", 500);
59        let json = serde_json::to_string(&cmd).unwrap();
60        let deserialized: ScheduledCommand = serde_json::from_str(&json).unwrap();
61        assert_eq!(cmd.command_name, deserialized.command_name);
62        assert_eq!(cmd.data, deserialized.data);
63        assert_eq!(cmd.execute_time, deserialized.execute_time);
64    }
65}