| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | use std::sync::OnceLock; |
| 8 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 9 | use std::sync::{Mutex, PoisonError}; |
| 10 | |
| 11 | use forge_ci::proto::RestartMode; |
| 12 | use tokio::sync::Notify; |
| 13 | |
| 14 | |
| 15 | #[derive(Default)] |
| 16 | pub struct RestartState { |
| 17 | mode: Mutex<Option<RestartMode>>, |
| 18 | wake: Notify, |
| 19 | |
| 20 | cancelled: AtomicBool, |
| 21 | } |
| 22 | |
| 23 | impl RestartState { |
| 24 | |
| 25 | |
| 26 | pub fn observe(&self, said: Option<RestartMode>) { |
| 27 | let Some(said) = said else { return }; |
| 28 | let mut mode = self.mode.lock().unwrap_or_else(PoisonError::into_inner); |
| 29 | let next = match (*mode, said) { |
| 30 | (Some(RestartMode::Now), _) => RestartMode::Now, |
| 31 | (_, said) => said, |
| 32 | }; |
| 33 | if *mode != Some(next) { |
| 34 | *mode = Some(next); |
| 35 | self.wake.notify_one(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | pub fn pending(&self) -> Option<RestartMode> { |
| 40 | *self.mode.lock().unwrap_or_else(PoisonError::into_inner) |
| 41 | } |
| 42 | |
| 43 | |
| 44 | pub async fn changed(&self) { |
| 45 | self.wake.notified().await; |
| 46 | } |
| 47 | |
| 48 | pub fn mark_cancelled(&self) { |
| 49 | self.cancelled.store(true, Ordering::SeqCst); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | pub fn jobs_cancelled(&self) -> bool { |
| 54 | self.cancelled.load(Ordering::SeqCst) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | |
| 59 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 60 | pub enum Step { |
| 61 | |
| 62 | Proceed, |
| 63 | |
| 64 | CancelJobs, |
| 65 | |
| 66 | WaitForJobs, |
| 67 | |
| 68 | Restart, |
| 69 | } |
| 70 | |
| 71 | |
| 72 | pub fn decide(pending: Option<RestartMode>, running: usize, cancelled: bool) -> Step { |
| 73 | match pending { |
| 74 | None => Step::Proceed, |
| 75 | Some(_) if running == 0 => Step::Restart, |
| 76 | Some(RestartMode::Now) if !cancelled => Step::CancelJobs, |
| 77 | Some(_) => Step::WaitForJobs, |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | pub fn boot_id() -> &'static str { |
| 83 | static ID: OnceLock<String> = OnceLock::new(); |
| 84 | ID.get_or_init(|| { |
| 85 | let nanos = std::time::SystemTime::now() |
| 86 | .duration_since(std::time::UNIX_EPOCH) |
| 87 | .map_or(0, |d| d.as_nanos()); |
| 88 | format!("{:x}-{nanos:x}", std::process::id()) |
| 89 | }) |
| 90 | } |
| 91 | |
| 92 | #[cfg(test)] |
| 93 | mod tests { |
| 94 | use super::*; |
| 95 | |
| 96 | #[test] |
| 97 | fn no_request_means_claim_as_usual() { |
| 98 | assert_eq!(decide(None, 3, false), Step::Proceed); |
| 99 | } |
| 100 | |
| 101 | #[test] |
| 102 | fn drain_waits_for_running_jobs_then_restarts() { |
| 103 | let drain = Some(RestartMode::Drain); |
| 104 | assert_eq!(decide(drain, 2, false), Step::WaitForJobs); |
| 105 | assert_eq!(decide(drain, 1, false), Step::WaitForJobs); |
| 106 | assert_eq!(decide(drain, 0, false), Step::Restart); |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn now_cancels_once_then_waits_then_restarts() { |
| 111 | let now = Some(RestartMode::Now); |
| 112 | assert_eq!(decide(now, 2, false), Step::CancelJobs); |
| 113 | assert_eq!(decide(now, 2, true), Step::WaitForJobs); |
| 114 | assert_eq!(decide(now, 0, true), Step::Restart); |
| 115 | assert_eq!(decide(now, 0, false), Step::Restart); |
| 116 | } |
| 117 | |
| 118 | #[test] |
| 119 | fn a_request_is_latched_and_now_beats_drain() { |
| 120 | let state = RestartState::default(); |
| 121 | assert_eq!(state.pending(), None); |
| 122 | state.observe(None); |
| 123 | assert_eq!(state.pending(), None); |
| 124 | state.observe(Some(RestartMode::Drain)); |
| 125 | assert_eq!(state.pending(), Some(RestartMode::Drain)); |
| 126 | state.observe(None); |
| 127 | assert_eq!(state.pending(), Some(RestartMode::Drain)); |
| 128 | state.observe(Some(RestartMode::Now)); |
| 129 | assert_eq!(state.pending(), Some(RestartMode::Now)); |
| 130 | state.observe(Some(RestartMode::Drain)); |
| 131 | assert_eq!(state.pending(), Some(RestartMode::Now)); |
| 132 | } |
| 133 | |
| 134 | #[test] |
| 135 | fn the_boot_id_is_stable_within_a_process() { |
| 136 | assert_eq!(boot_id(), boot_id()); |
| 137 | assert!(!boot_id().is_empty()); |
| 138 | } |
| 139 | } |