use std::collections::HashMap; use std::sync::{Mutex, MutexGuard, PoisonError}; use tokio::sync::watch; const RUNNERS_KEY: &str = "runners"; const RUNS_ANY_KEY: &str = "*runs"; #[derive(Default)] pub struct CiLive { jobs: Signals, runs: Signals, repos: Signals, runners: Signals, queues: Signals, runner_online: Mutex>, } impl CiLive { pub fn note_runner_online(&self, runner_id: &str, online: bool) { let changed = self .runner_online .lock() .unwrap_or_else(PoisonError::into_inner) .insert(runner_id.to_owned(), online) != Some(online); if changed { self.bump_runners(); } } pub fn runner_recorded_online(&self, runner_id: &str) -> bool { self.runner_online .lock() .unwrap_or_else(PoisonError::into_inner) .get(runner_id) == Some(&true) } pub fn forget_runners_except(&self, keep: &[&str]) { self.runner_online .lock() .unwrap_or_else(PoisonError::into_inner) .retain(|id, _| keep.contains(&id.as_str())); } pub fn subscribe_queue(&self, repo_id: &str) -> watch::Receiver { self.queues.subscribe(repo_id) } pub fn bump_queue(&self, repo_id: &str) { self.queues.bump(repo_id); } pub fn subscribe_job(&self, job_id: &str) -> watch::Receiver { self.jobs.subscribe(job_id) } pub fn subscribe_run(&self, run_id: &str) -> watch::Receiver { self.runs.subscribe(run_id) } pub fn subscribe_any_run(&self) -> watch::Receiver { self.runs.subscribe(RUNS_ANY_KEY) } pub fn subscribe_repo(&self, repo_id: &str) -> watch::Receiver { self.repos.subscribe(repo_id) } pub fn subscribe_runners(&self) -> watch::Receiver { self.runners.subscribe(RUNNERS_KEY) } pub fn bump_runners(&self) { self.runners.bump(RUNNERS_KEY); } pub fn bump_job(&self, job_id: &str) { self.jobs.bump(job_id); } pub fn bump_run(&self, run_id: &str) { self.runs.bump(run_id); self.runs.bump(RUNS_ANY_KEY); } pub fn bump_repo(&self, repo_id: &str) { self.repos.bump(repo_id); self.runs.bump(RUNS_ANY_KEY); } pub fn close_job(&self, job_id: &str) { self.jobs.close(job_id); } } #[derive(Default)] pub struct Signals { counters: Mutex>>, } impl Signals { fn counters(&self) -> MutexGuard<'_, HashMap>> { self.counters.lock().unwrap_or_else(PoisonError::into_inner) } pub fn subscribe(&self, key: &str) -> watch::Receiver { self.counters() .entry(key.to_owned()) .or_insert_with(|| watch::channel(0).0) .subscribe() } pub fn bump(&self, key: &str) { crate::live::next_live_seq(); let mut counters = self.counters(); let Some(tx) = counters.get(key) else { return; }; tx.send_modify(|generation| *generation = generation.wrapping_add(1)); if tx.receiver_count() == 0 { counters.remove(key); } } fn close(&self, key: &str) { crate::live::next_live_seq(); let removed = self.counters().remove(key); if let Some(tx) = removed { tx.send_modify(|generation| *generation = generation.wrapping_add(1)); } } } #[cfg(test)] mod tests { use std::time::Duration; use super::*; #[tokio::test] async fn a_bump_wakes_only_the_subscribers_of_that_job() { let live = CiLive::default(); let mut job = live.subscribe_job("job-1"); let other = live.subscribe_job("job-2"); let run = live.subscribe_run("run-1"); live.bump_job("job-1"); tokio::time::timeout(Duration::from_secs(1), job.changed()) .await .expect("woken") .expect("open"); assert_eq!(*job.borrow_and_update(), 1); assert!(!other.has_changed().expect("open")); assert!(!run.has_changed().expect("open")); } #[tokio::test] async fn a_bump_before_subscribing_is_not_seen_and_a_bump_after_is() { let live = CiLive::default(); let early = live.subscribe_run("run-1"); live.bump_run("run-1"); let mut late = live.subscribe_run("run-1"); assert!(early.has_changed().expect("open")); assert!(!late.has_changed().expect("open")); live.bump_run("run-1"); assert!(late.has_changed().expect("open")); late.changed().await.expect("open"); assert_eq!(*late.borrow_and_update(), 2); } #[test] fn a_bump_without_subscribers_keeps_no_entry() { let live = CiLive::default(); live.bump_job("job-1"); live.bump_run("run-1"); live.bump_repo("repo-1"); assert!(live.jobs.counters().is_empty()); assert!(live.runs.counters().is_empty()); assert!(live.repos.counters().is_empty()); } #[tokio::test] async fn a_repo_bump_wakes_only_the_subscribers_of_that_repo() { let live = CiLive::default(); let mut repo = live.subscribe_repo("repo-1"); let other = live.subscribe_repo("repo-2"); let run = live.subscribe_run("run-1"); live.bump_repo("repo-1"); tokio::time::timeout(Duration::from_secs(1), repo.changed()) .await .expect("woken") .expect("open"); assert_eq!(*repo.borrow_and_update(), 1); assert!(!other.has_changed().expect("open")); assert!(!run.has_changed().expect("open")); } #[tokio::test] async fn a_runner_wakes_the_pages_only_when_its_online_state_changes() { let live = CiLive::default(); let mut runners = live.subscribe_runners(); live.note_runner_online("runner-1", true); assert!(runners.has_changed().expect("open")); runners.borrow_and_update(); live.note_runner_online("runner-1", true); assert!(!runners.has_changed().expect("open")); live.note_runner_online("runner-1", false); assert!(runners.has_changed().expect("open")); assert!(!live.runner_recorded_online("runner-1")); } #[tokio::test] async fn close_wakes_the_subscriber_once_and_then_ends_the_signal() { let live = CiLive::default(); let mut rx = live.subscribe_job("job-1"); live.close_job("job-1"); assert!(live.jobs.counters().is_empty()); rx.changed().await.expect("the last change is delivered"); assert_eq!(*rx.borrow_and_update(), 1); assert!(rx.changed().await.is_err()); } }