#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum AlertKind { RunnerOffline, Backup, Disk, CiKilled, SelfUpdate, Replication, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum AlertTopic { RunnerOffline, BackupFailed, BackupVerifyFailed, BackupOverdue, FsckFailed, DiskLow, CiKilled, SelfUpdateRestart, SelfUpdateFailed, Replication, } impl AlertTopic { pub const ALL: [Self; 10] = [ Self::RunnerOffline, Self::BackupFailed, Self::BackupVerifyFailed, Self::BackupOverdue, Self::FsckFailed, Self::DiskLow, Self::CiKilled, Self::SelfUpdateRestart, Self::SelfUpdateFailed, Self::Replication, ]; pub fn kind(self) -> AlertKind { match self { Self::RunnerOffline => AlertKind::RunnerOffline, Self::BackupFailed | Self::BackupVerifyFailed | Self::BackupOverdue | Self::FsckFailed => AlertKind::Backup, Self::DiskLow => AlertKind::Disk, Self::CiKilled => AlertKind::CiKilled, Self::SelfUpdateRestart | Self::SelfUpdateFailed => AlertKind::SelfUpdate, Self::Replication => AlertKind::Replication, } } } string_enum!(AlertTopic { RunnerOffline = "runner_offline", BackupFailed = "backup_failed", BackupVerifyFailed = "backup_verify_failed", BackupOverdue = "backup_overdue", FsckFailed = "fsck_failed", DiskLow = "disk_low", CiKilled = "ci_killed", SelfUpdateRestart = "self_update_restart", SelfUpdateFailed = "self_update_failed", Replication = "replication", }); pub fn is_kill_signature(exit_code: Option, message: Option<&str>) -> bool { if matches!(exit_code, Some(137) | Some(-9)) { return true; } let Some(message) = message else { return false; }; let text = message.to_ascii_lowercase(); if text.contains("sigkill") || text.contains("out of memory") || text.contains("oom-kill") || text.contains("oom kill") || text.contains("oom_kill") || text.contains("oomkill") || text.contains("exit code 137") || text.contains("exit status 137") { return true; } let mut rest = text.as_str(); while let Some(at) = rest.find("signal") { rest = &rest[at + "signal".len()..]; let digits: String = rest .trim_start_matches([':', '=', ' ']) .chars() .take_while(char::is_ascii_digit) .collect(); if digits == "9" { return true; } } text.trim() == "killed" } pub fn free_percent(total: u64, available: u64) -> Option { (u128::from(available) * 100) .checked_div(u128::from(total)) .map(|p| u64::try_from(p).unwrap_or(100)) } pub fn disk_is_low(total: u64, available: u64, min_percent: u64, min_gib: u64) -> bool { let low_percent = free_percent(total, available).is_some_and(|p| p < min_percent); let low_gib = min_gib > 0 && available < min_gib.saturating_mul(1 << 30); low_percent || low_gib } pub fn backup_overdue( now_unix: i64, last_ok_unix: Option, since_unix: i64, interval_hours: u64, ) -> bool { let limit = i64::try_from(interval_hours.saturating_mul(3600).saturating_mul(2)).unwrap_or(i64::MAX); let basis = last_ok_unix.unwrap_or(since_unix); now_unix.saturating_sub(basis) > limit } pub fn runner_is_offline(now_unix: i64, last_seen_unix: Option, minutes: u64) -> bool { let limit = i64::try_from(minutes.saturating_mul(60)).unwrap_or(i64::MAX); last_seen_unix.is_some_and(|seen| now_unix.saturating_sub(seen) > limit) } #[cfg(test)] mod tests { use super::*; #[test] fn kill_signatures_are_recognised_and_other_failures_are_not() { let cases: [(Option, Option<&str>, bool); 15] = [ (Some(137), None, true), (Some(-9), None, true), (None, Some("process exited: signal: 9 (killed)"), true), (None, Some("terminated by SIGKILL"), true), (None, Some("fatal: out of memory"), true), (None, Some("Killed"), true), (None, Some("job died with signal 9"), true), (None, Some("exit status 137"), true), (Some(1), None, false), (Some(0), Some("ok"), false), (None, Some("job was terminated by a signal"), false), (None, Some("signal: 90"), false), (None, Some("signal 15 (terminated)"), false), (None, Some("a broom closet"), false), (None, None, false), ]; for (code, log, want) in cases { assert_eq!(is_kill_signature(code, log), want, "case {code:?} {log:?}"); } } #[test] fn disk_thresholds() { let gib = 1u64 << 30; assert_eq!(free_percent(100 * gib, 5 * gib), Some(5)); assert_eq!(free_percent(0, 0), None); assert!(disk_is_low(100 * gib, 5 * gib, 10, 0)); assert!(!disk_is_low(100 * gib, 10 * gib, 10, 0)); assert!(disk_is_low(1000 * gib, 50 * gib, 1, 60)); assert!(!disk_is_low(1000 * gib, 70 * gib, 1, 60)); } #[test] fn backup_overdue_after_twice_the_interval() { let h = 3600; assert!(!backup_overdue(47 * h, Some(0), 0, 24)); assert!(backup_overdue(49 * h, Some(0), 0, 24)); assert!(backup_overdue(49 * h, None, 0, 24)); assert!(!backup_overdue(49 * h, None, 10 * h, 24)); } #[test] fn runner_offline_uses_the_threshold() { assert!(!runner_is_offline(1000, Some(500), 10)); assert!(runner_is_offline(1000, Some(300), 10)); assert!(!runner_is_offline(1000, None, 10)); } }