Skip to main content

wfe_core/models/
scheduled_command.rs

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