1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! [`TriggerKind`] — how a run was triggered.
use serde::{Deserialize, Serialize};
use uuid::Uuid;
/// How a run was triggered.
///
/// # Examples
///
/// ```
/// use ironflow_store::entities::TriggerKind;
///
/// let trigger = TriggerKind::Manual;
/// let json = serde_json::to_string(&trigger).unwrap();
/// assert!(json.contains("manual"));
/// ```
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "kind")]
pub enum TriggerKind {
/// Triggered manually (CLI or programmatic call).
Manual,
/// Triggered by an incoming webhook.
Webhook {
/// The webhook path that received the request.
path: String,
},
/// Triggered by a cron schedule.
Cron {
/// The cron expression that fired.
schedule: String,
},
/// Triggered via the REST API.
Api,
/// Retry of a previously failed run.
Retry {
/// The original run that failed.
parent_run_id: Uuid,
},
/// Triggered by a parent workflow as a sub-workflow step.
Workflow,
/// Triggered by a message consumed from a NATS subject.
Nats {
/// The NATS subject the message was consumed from.
subject: String,
},
/// Triggered by an internal run event (workflow chaining).
RunEvent {
/// The run whose event triggered this run.
source_run_id: Uuid,
/// The event kind that fired (e.g. `"run_failed"`).
event_kind: String,
},
/// Triggered by a polling probe detecting new data.
Polling {
/// Name of the probe that fired (e.g. `"http"`, `"sql"`).
probe: String,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serde_roundtrip() {
let triggers = vec![
TriggerKind::Manual,
TriggerKind::Api,
TriggerKind::Webhook {
path: "/hooks/gh".to_string(),
},
TriggerKind::Cron {
schedule: "0 */5 * * * *".to_string(),
},
TriggerKind::Retry {
parent_run_id: Uuid::nil(),
},
TriggerKind::Nats {
subject: "workflows.deploy".to_string(),
},
TriggerKind::RunEvent {
source_run_id: Uuid::nil(),
event_kind: "run_failed".to_string(),
},
TriggerKind::Polling {
probe: "http".to_string(),
},
];
for trigger in triggers {
let json = serde_json::to_string(&trigger).expect("serialize");
let back: TriggerKind = serde_json::from_str(&json).expect("deserialize");
assert_eq!(trigger, back);
}
}
#[test]
fn nats_serializes_with_subject() {
let trigger = TriggerKind::Nats {
subject: "orders.created".to_string(),
};
let json = serde_json::to_string(&trigger).expect("serialize");
assert!(json.contains("\"kind\":\"nats\""));
assert!(json.contains("\"subject\":\"orders.created\""));
}
#[test]
fn run_event_serializes_with_source() {
let run_id = Uuid::nil();
let trigger = TriggerKind::RunEvent {
source_run_id: run_id,
event_kind: "step_failed".to_string(),
};
let json = serde_json::to_string(&trigger).expect("serialize");
assert!(json.contains("\"kind\":\"run_event\""));
assert!(json.contains("\"event_kind\":\"step_failed\""));
assert!(json.contains("\"source_run_id\""));
}
#[test]
fn polling_serializes_with_probe() {
let trigger = TriggerKind::Polling {
probe: "http".to_string(),
};
let json = serde_json::to_string(&trigger).expect("serialize");
assert!(json.contains("\"kind\":\"polling\""));
assert!(json.contains("\"probe\":\"http\""));
}
}