Skip to main content

runnerd_core/api/
results.rs

1//! What an owner's action did, as every client says it after the daemon answers: a pause and the runners it
2//! stopped, what forcing one would cancel, a reseed asked for and done, records removed and a resize written.
3//! Clients may ask differently (a button, a key, a flag); the facts they report come from here.
4
5use super::{Paused, RemovedRecords, Resize};
6use crate::config::ByteSize;
7
8/// What forcing a pause does to `jobs` running jobs: `cancels its run on GitHub and removes its runner`, or for
9/// several, `cancels their runs on GitHub and removes their runners`.
10pub fn force_pause_consequence(jobs: usize) -> &'static str {
11    if jobs == 1 {
12        "cancels its run on GitHub and removes its runner"
13    } else {
14        "cancels their runs on GitHub and removes their runners"
15    }
16}
17
18impl Paused {
19    /// The pause of `target`, a repository or `every repository`: `o/app paused: it gets no new runners`, or for a
20    /// forced one, `o/app paused; stopped job 42 on runnerd-jvm-1`.
21    pub fn line(&self, target: &str) -> String {
22        if self.stopped.is_empty() {
23            return format!("{target} paused: it gets no new runners");
24        }
25        let stopped: Vec<String> = self.stopped.iter().map(ToString::to_string).collect();
26        format!("{target} paused; stopped {}", stopped.join(", "))
27    }
28}
29
30/// A reseed of `profile` the daemon accepted: `jvm: reseed requested; it runs once the profile's slots are free`.
31pub fn reseed_requested(profile: &str) -> String {
32    format!("{profile}: reseed requested; it runs once the profile's slots are free")
33}
34
35/// A reseed of `profile` done, as `cache.reseeded` reports it: `jvm: reseeded` or `jvm: reseed failed: …`.
36pub fn reseeded(profile: &str, error: Option<&str>) -> String {
37    match error {
38        Some(error) => format!("{profile}: reseed failed: {error}"),
39        None => format!("{profile}: reseeded"),
40    }
41}
42
43/// The line of a reseed's `output`, as `cache.reseeded` carries it, that says what it did: the one that started the
44/// snapshot or some of its layers over from the image, else its last. Empty for no output.
45pub fn reseed_outcome(output: &str) -> &str {
46    let mut lines = output.trim().lines();
47    lines
48        .clone()
49        .find(|line| line.contains("over from the image"))
50        .or_else(|| lines.next_back())
51        .unwrap_or_default()
52}
53
54impl RemovedRecords {
55    /// What it freed: `no record removed`, `removed 1 record, 3G freed` or `removed 4 records, 9G freed`.
56    pub fn line(&self) -> String {
57        match self.jobs.len() {
58            0 => "no record removed".to_owned(),
59            1 => format!("removed 1 record, {} freed", ByteSize(self.bytes)),
60            count => format!("removed {count} records, {} freed", ByteSize(self.bytes)),
61        }
62    }
63}
64
65impl Resize {
66    /// It written into `profile`: `jvm: memory 6G, 2 slots written; the daemon runs with them`, naming its CPUs
67    /// when it changes them.
68    pub fn written_line(&self, profile: &str) -> String {
69        let cpus = self
70            .cpus
71            .map(|cpus| format!(", {cpus} cpus"))
72            .unwrap_or_default();
73        format!(
74            "{profile}: memory {}, {} slots{cpus} written; the daemon runs with them",
75            ByteSize(self.memory),
76            self.slots
77        )
78    }
79}
80
81#[cfg(test)]
82mod tests;