Skip to main content

runnerd_core/api/
control.rs

1//! The control endpoints' types: pausing with or without cancelling running jobs, what a refused request names,
2//! the config file and its check, and the doctor's checks. Plain data; the engine and the daemon act on them.
3
4use std::fmt;
5
6use serde::{Deserialize, Serialize};
7
8/// A runner of this daemon that holds a slot, and the job it runs, if it took one.
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub struct RunningJob {
11    /// `owner/name` the runner is registered on.
12    pub repository: String,
13    /// The runner's name, also its container's.
14    pub runner: String,
15    /// The job's ID; `None` while the runner waits for a job or before its log shows which it took.
16    pub job: Option<u64>,
17}
18
19impl fmt::Display for RunningJob {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self.job {
22            Some(job) => write!(f, "job {job} on {}", self.runner),
23            None => write!(f, "{}", self.runner),
24        }
25    }
26}
27
28/// The body of a 409 refused because runners use what the request would change. Has the `message` of an
29/// [`ErrorResponse`](super::ErrorResponse), so a client that reads only that still gets it.
30#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
31pub struct Busy {
32    /// What went wrong, naming the jobs.
33    pub message: String,
34    /// The runners in the way.
35    pub jobs: Vec<RunningJob>,
36}
37
38/// `POST /v1/repositories/{owner}/{name}/pause` and `POST /v1/repositories/pause` query parameters.
39#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
40pub struct PauseQuery {
41    /// Cancel the repository's running jobs and remove their runners. Without it, a pause while the repository
42    /// runs a job is refused with [`Busy`].
43    #[serde(default)]
44    pub force: bool,
45}
46
47/// `POST /v1/repositories/{owner}/{name}/pause` and `POST /v1/repositories/pause`: the repository, or every one,
48/// is paused, and stays paused when the daemon restarts.
49#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
50pub struct Paused {
51    /// The runners a forced pause stopped: those whose runs it cancelled go once they report the job cancelled,
52    /// within 90 s, the others at once. Empty without `force`.
53    pub stopped: Vec<RunningJob>,
54}
55
56/// `GET /v1/config`: the config the daemon runs with. Parse `text` with `config::Config::parse` for its fields.
57#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
58pub struct ConfigFile {
59    /// The file it was read from, and `PUT /v1/config` writes.
60    pub path: String,
61    /// The TOML, as last loaded or written by the daemon.
62    pub text: String,
63}
64
65/// `POST /v1/config/check` and `PUT /v1/config`: what `runnerd check-config` prints for a config. `PUT` refuses
66/// an invalid one with 422 and this body, which has an [`ErrorResponse`](super::ErrorResponse)'s `message`.
67#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
68pub struct ConfigCheck {
69    /// Whether the daemon can run it.
70    pub valid: bool,
71    /// `valid, memory caps sum to …`, or why it is not valid.
72    pub message: String,
73    /// What a valid config should still fix.
74    pub warnings: Vec<String>,
75    /// Bytes every runner and the mirror may use at once, when it parses.
76    pub memory_caps: Option<u64>,
77    /// The `MemoryMax` of the slice it names; `None` when unlimited or unknown.
78    pub memory_max: Option<u64>,
79}
80
81/// `GET /v1/doctor`: what `runnerd doctor` checks, for the config the daemon runs with.
82#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
83pub struct Doctor {
84    /// In the order a new user fixes them.
85    pub checks: Vec<Check>,
86}
87
88/// One thing the host needs to run runners. Its `Display` is the line `runnerd doctor` prints.
89#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
90pub struct Check {
91    /// Such as `Docker` or `sysbox`.
92    pub name: String,
93    /// The outcome.
94    pub status: CheckStatus,
95    /// What was found.
96    pub detail: String,
97    /// What to run or change to pass, for a failed check.
98    pub fix: Option<String>,
99}
100
101/// How a [`Check`] came out.
102#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
103#[serde(rename_all = "kebab-case")]
104pub enum CheckStatus {
105    /// Ready.
106    Pass,
107    /// Runners cannot run until it is fixed.
108    Fail,
109    /// Works, but unsafe.
110    Warn,
111    /// Not checked, because a check it depends on failed or no config was given.
112    Skip,
113    /// For information.
114    Info,
115}
116
117impl fmt::Display for Check {
118    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119        let status = match self.status {
120            CheckStatus::Pass => "ok",
121            CheckStatus::Fail => "FAIL",
122            CheckStatus::Warn => "WARN",
123            CheckStatus::Skip => "skip",
124            CheckStatus::Info => "info",
125        };
126        write!(f, "{status:<5} {}: {}", self.name, self.detail)?;
127        if let Some(fix) = &self.fix {
128            for (i, line) in fix.lines().enumerate() {
129                let label = if i == 0 { "fix:" } else { "" };
130                write!(f, "\n      {label:<4} {line}")?;
131            }
132        }
133        Ok(())
134    }
135}