| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use crate::domain::{GateKind, GateRunId, GitSha, NodeId, TierId, Version}; |
| 9 |
use crate::outcome::{DeployFailureKind, GateOutcome}; |
| 10 |
use chrono::{DateTime, Utc}; |
| 11 |
use serde::{Deserialize, Serialize}; |
| 12 |
use tokio::sync::broadcast; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
pub const CAPACITY: usize = 256; |
| 18 |
|
| 19 |
pub type EventTx = broadcast::Sender<EventEnvelope>; |
| 20 |
|
| 21 |
#[derive(Clone, Debug, Serialize, Deserialize)] |
| 22 |
pub struct EventEnvelope { |
| 23 |
pub at: DateTime<Utc>, |
| 24 |
#[serde(flatten)] |
| 25 |
pub event: Event, |
| 26 |
} |
| 27 |
|
| 28 |
#[derive(Clone, Debug, Serialize, Deserialize)] |
| 29 |
#[serde(tag = "kind", rename_all = "snake_case")] |
| 30 |
pub enum Event { |
| 31 |
|
| 32 |
RebuildRequested { sha: GitSha }, |
| 33 |
|
| 34 |
BuildAborted { sha_aborted: GitSha }, |
| 35 |
BuildStart { sha: GitSha, version: Version }, |
| 36 |
BuildOk { sha: GitSha, version: Version, elapsed_s: u64 }, |
| 37 |
BuildFailed { sha: GitSha, version: Version, elapsed_s: u64 }, |
| 38 |
GateStart { |
| 39 |
run_id: GateRunId, |
| 40 |
tier: TierId, |
| 41 |
version: Version, |
| 42 |
gate: GateKind, |
| 43 |
}, |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
GateLogChunk { |
| 52 |
run_id: GateRunId, |
| 53 |
seq: u32, |
| 54 |
text: String, |
| 55 |
}, |
| 56 |
|
| 57 |
|
| 58 |
GateDone { |
| 59 |
run_id: GateRunId, |
| 60 |
tier: TierId, |
| 61 |
version: Version, |
| 62 |
gate: GateKind, |
| 63 |
outcome: GateOutcome, |
| 64 |
}, |
| 65 |
DeployStart { tier: TierId, node: NodeId, version: Version }, |
| 66 |
DeployOk { tier: TierId, node: NodeId, version: Version }, |
| 67 |
DeployFailed { |
| 68 |
tier: TierId, |
| 69 |
node: NodeId, |
| 70 |
version: Version, |
| 71 |
failure: DeployFailureKind, |
| 72 |
}, |
| 73 |
PromoteComplete { tier: TierId, version: Version }, |
| 74 |
Rollback { tier: TierId, from: Version, to: Version }, |
| 75 |
|
| 76 |
|
| 77 |
BackupFetched { source: String, byte_size: i64 }, |
| 78 |
ManualConfirm { tier: TierId, version: Version }, |
| 79 |
} |
| 80 |
|
| 81 |
pub fn channel() -> EventTx { |
| 82 |
broadcast::channel(CAPACITY).0 |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
pub fn emit(tx: &EventTx, event: Event) { |
| 89 |
let envelope = EventEnvelope { at: Utc::now(), event }; |
| 90 |
let _ = tx.send(envelope); |
| 91 |
} |
| 92 |
|
| 93 |
#[cfg(test)] |
| 94 |
mod tests { |
| 95 |
use super::*; |
| 96 |
|
| 97 |
#[test] |
| 98 |
fn emit_with_zero_subscribers_does_not_panic() { |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
let tx = channel(); |
| 104 |
emit(&tx, Event::RebuildRequested { sha: GitSha::parse("abc1234").unwrap() }); |
| 105 |
emit(&tx, Event::BackupFetched { source: "x".into(), byte_size: 1 }); |
| 106 |
} |
| 107 |
|
| 108 |
#[tokio::test] |
| 109 |
async fn emit_reaches_a_subscriber() { |
| 110 |
let tx = channel(); |
| 111 |
let mut rx = tx.subscribe(); |
| 112 |
emit(&tx, Event::PromoteComplete { |
| 113 |
tier: TierId::new("a"), |
| 114 |
version: "0.8.12".parse().unwrap(), |
| 115 |
}); |
| 116 |
let env = rx.recv().await.expect("envelope"); |
| 117 |
match env.event { |
| 118 |
Event::PromoteComplete { tier, version } => { |
| 119 |
assert_eq!(tier.as_str(), "a"); |
| 120 |
assert_eq!(version.to_string(), "0.8.12"); |
| 121 |
} |
| 122 |
_ => panic!("wrong event kind"), |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
#[tokio::test] |
| 127 |
async fn envelope_serializes_with_flat_kind() { |
| 128 |
|
| 129 |
|
| 130 |
let env = EventEnvelope { |
| 131 |
at: Utc::now(), |
| 132 |
event: Event::GateStart { |
| 133 |
run_id: GateRunId(42), |
| 134 |
tier: TierId::new("host"), |
| 135 |
version: "0.8.12".parse().unwrap(), |
| 136 |
gate: GateKind::CargoTest, |
| 137 |
}, |
| 138 |
}; |
| 139 |
let s = serde_json::to_string(&env).unwrap(); |
| 140 |
let v: serde_json::Value = serde_json::from_str(&s).unwrap(); |
| 141 |
assert_eq!(v["kind"], "gate_start"); |
| 142 |
assert_eq!(v["tier"], "host"); |
| 143 |
assert_eq!(v["gate"], "cargo_test"); |
| 144 |
|
| 145 |
assert!(v.get("event").is_none()); |
| 146 |
} |
| 147 |
|
| 148 |
#[tokio::test] |
| 149 |
async fn lagged_subscriber_observes_recv_error_lagged() { |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
let tx = channel(); |
| 154 |
let mut rx = tx.subscribe(); |
| 155 |
for i in 0..(CAPACITY + 10) { |
| 156 |
|
| 157 |
let sha = GitSha::parse(&format!("{i:0>7x}")).unwrap(); |
| 158 |
emit(&tx, Event::RebuildRequested { sha }); |
| 159 |
} |
| 160 |
let err = rx.recv().await.expect_err("expected Lagged"); |
| 161 |
match err { |
| 162 |
tokio::sync::broadcast::error::RecvError::Lagged(n) => assert!(n >= 10), |
| 163 |
other => panic!("unexpected error: {other:?}"), |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|