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
43impl RemovedRecords {
44    /// What it freed: `no record removed`, `removed 1 record, 3G freed` or `removed 4 records, 9G freed`.
45    pub fn line(&self) -> String {
46        match self.jobs.len() {
47            0 => "no record removed".to_owned(),
48            1 => format!("removed 1 record, {} freed", ByteSize(self.bytes)),
49            count => format!("removed {count} records, {} freed", ByteSize(self.bytes)),
50        }
51    }
52}
53
54impl Resize {
55    /// It written into `profile`: `jvm: memory 6G, 2 slots written; the daemon runs with them`, naming its CPUs
56    /// when it changes them.
57    pub fn written_line(&self, profile: &str) -> String {
58        let cpus = self
59            .cpus
60            .map(|cpus| format!(", {cpus} cpus"))
61            .unwrap_or_default();
62        format!(
63            "{profile}: memory {}, {} slots{cpus} written; the daemon runs with them",
64            ByteSize(self.memory),
65            self.slots
66        )
67    }
68}
69
70#[cfg(test)]
71mod tests;