use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, MutexGuard};
use vitrify_core::model::UtcTime;
use crate::gate;
use crate::out::J;
use crate::run::events::{EventLog, RunEvent};
#[derive(Clone)]
pub struct GateBoard {
inner: Arc<Mutex<Board>>,
}
struct Board {
path: Option<PathBuf>,
events: EventLog,
lane: Option<String>,
step: &'static str,
since: UtcTime,
batch: Vec<String>,
running: Vec<(String, String)>,
count: Option<(usize, HashSet<String>)>,
}
impl Board {
fn begin(&mut self, name: &'static str) {
self.step = name;
self.since = UtcTime::now();
self.running.clear();
self.count = None;
if name == "idle" {
return;
}
let mut detail = name.to_string();
if !self.batch.is_empty() {
detail = format!("{detail} · {}", self.batch.join(", "));
}
let units = self.batch.iter().map(|u| J::s(u.clone())).collect();
let mut e = RunEvent::new(UtcTime::now(), "gate", None, None, detail)
.with("batch", J::Arr(units))
.with("step", J::s(name));
if let Some(lane) = &self.lane {
e = e.with("lane", J::s(lane.clone()));
}
self.events.write(&e);
}
fn to_json(&self) -> J {
let (done, total) = match &self.count {
Some((total, done)) => (J::Int(done.len() as i64), J::Int(*total as i64)),
None => (J::Null, J::Null),
};
let running = self
.running
.iter()
.map(|(cmd, since)| {
J::Obj(vec![
("command".to_string(), J::s(cmd.clone())),
("since".to_string(), J::s(since.clone())),
])
})
.collect();
J::Obj(vec![
("step".to_string(), J::s(self.step)),
("since".to_string(), J::s(self.since.format())),
(
"batch".to_string(),
J::Arr(self.batch.iter().map(|u| J::s(u.clone())).collect()),
),
("running".to_string(), J::Arr(running)),
("done".to_string(), done),
("total".to_string(), total),
])
}
fn write(&self) {
let Some(path) = &self.path else { return };
let tmp = path.with_file_name("gate.json.tmp");
if std::fs::write(&tmp, self.to_json().dumps_indent2()).is_ok() {
let _ = std::fs::rename(&tmp, path);
}
}
}
impl GateBoard {
fn new(path: Option<PathBuf>) -> GateBoard {
GateBoard {
inner: Arc::new(Mutex::new(Board {
path,
events: EventLog::off(),
lane: None,
step: "idle",
since: UtcTime::now(),
batch: Vec::new(),
running: Vec::new(),
count: None,
})),
}
}
pub fn off() -> GateBoard {
GateBoard::new(None)
}
pub fn to(path: PathBuf) -> GateBoard {
GateBoard::new(Some(path))
}
pub fn with_events(self, events: EventLog) -> GateBoard {
self.lock().events = events;
self
}
pub fn for_lane(self, slug: &str) -> GateBoard {
self.lock().lane = Some(slug.to_string());
self
}
fn lock(&self) -> MutexGuard<'_, Board> {
self.inner.lock().unwrap_or_else(|p| p.into_inner())
}
fn change(&self, f: impl FnOnce(&mut Board)) {
let mut board = self.lock();
f(&mut board);
board.write();
}
pub fn batch(&self, units: Vec<String>) {
self.change(|b| b.batch = units);
}
pub fn step(&self, name: &'static str) {
self.change(|b| b.begin(name));
}
pub fn rows(&self, total: usize) {
self.change(|b| b.count = Some((total, HashSet::new())));
}
pub fn idle(&self) {
self.change(|b| {
b.batch.clear();
b.begin("idle");
});
}
}
impl gate::Watch for GateBoard {
fn started(&self, cmd: &str) {
self.change(|b| b.running.push((cmd.to_string(), UtcTime::now().format())));
}
fn ended(&self, cmd: &str) {
self.change(|b| {
if let Some(at) = b.running.iter().position(|(c, _)| c == cmd) {
b.running.remove(at);
}
if let Some((total, done)) = &mut b.count
&& done.len() < *total
{
done.insert(cmd.to_string());
}
});
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::gate::Watch;
fn read(path: &std::path::Path) -> serde_json::Value {
serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap()
}
fn running(v: &serde_json::Value) -> Vec<String> {
v["running"]
.as_array()
.unwrap()
.iter()
.map(|r| r["command"].as_str().unwrap().to_string())
.collect()
}
#[test]
fn the_file_follows_each_change() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("gate.json");
let board = GateBoard::to(path.clone());
board.batch(vec!["U-002".into(), "U-005".into()]);
board.step("rows");
let v = read(&path);
assert_eq!(v["step"], "rows");
assert_eq!(v["batch"], serde_json::json!(["U-002", "U-005"]));
assert_eq!(v["running"], serde_json::json!([]));
assert!(v["done"].is_null() && v["total"].is_null());
assert!(UtcTime::parse(v["since"].as_str().unwrap()).is_some());
board.rows(2);
board.started("a");
board.started("b");
let v = read(&path);
assert_eq!(
(v["done"].as_i64(), v["total"].as_i64()),
(Some(0), Some(2))
);
assert_eq!(running(&v), ["a", "b"]);
assert!(UtcTime::parse(v["running"][0]["since"].as_str().unwrap()).is_some());
board.ended("a");
board.started("a"); board.ended("a");
let v = read(&path);
assert_eq!(v["done"], 1);
assert_eq!(running(&v), ["b"]);
board.ended("b");
board.started("make test");
board.ended("make test");
let v = read(&path);
assert_eq!(
(v["done"].as_i64(), v["total"].as_i64()),
(Some(2), Some(2))
);
board.step("lint");
let v = read(&path);
assert_eq!(v["running"], serde_json::json!([]));
assert!(v["done"].is_null() && v["total"].is_null());
assert_eq!(v["batch"], serde_json::json!(["U-002", "U-005"]));
board.idle();
let v = read(&path);
assert_eq!(v["step"], "idle");
assert_eq!(v["batch"], serde_json::json!([]));
let text = std::fs::read_to_string(&path).unwrap();
let at = |k: &str| text.find(&format!("\"{k}\"")).unwrap();
assert!(at("step") < at("since") && at("since") < at("batch"));
assert!(at("batch") < at("running") && at("running") < at("done"));
assert!(at("done") < at("total"));
assert!(!dir.path().join("gate.json.tmp").exists());
}
#[test]
fn commands_started_at_once_from_many_threads_are_all_running() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("gate.json");
let board = GateBoard::to(path.clone());
board.step("rows");
std::thread::scope(|s| {
for t in 0..8 {
let board = board.clone();
s.spawn(move || {
for k in 0..10 {
board.started(&format!("cmd {t}-{k}"));
}
});
}
});
let mut got = running(&read(&path));
got.sort();
let mut want: Vec<String> = (0..8)
.flat_map(|t| (0..10).map(move |k| format!("cmd {t}-{k}")))
.collect();
want.sort();
assert_eq!(got, want);
}
#[test]
fn each_step_but_idle_is_a_gate_event() {
let dir = tempfile::tempdir().unwrap();
let board =
GateBoard::to(dir.path().join("gate.json")).with_events(EventLog::open(dir.path()));
board.batch(vec!["U-002".into(), "U-005".into()]);
board.step("rows");
board.idle();
board.step("head-check");
let text = std::fs::read_to_string(dir.path().join("events.jsonl")).unwrap();
let events: Vec<RunEvent> = text
.lines()
.map(|l| RunEvent::from_json(&serde_json::from_str(l).unwrap()).unwrap())
.collect();
assert_eq!(events.len(), 2);
assert_eq!(events[0].kind, "gate");
assert_eq!(events[0].detail, "rows · U-002, U-005");
assert_eq!(events[0].field("step"), Some(&J::s("rows")));
assert_eq!(
events[0].field("batch"),
Some(&J::Arr(vec![J::s("U-002"), J::s("U-005")]))
);
assert_eq!(events[1].detail, "head-check");
assert_eq!(events[1].field("batch"), Some(&J::Arr(vec![])));
}
}