Skip to main content

runnerd_core/
api.rs

1//! The daemon's JSON API, version 1: the request and response types every client deserializes, and the
2//! principal a transport authenticates. Plain data only: no I/O, no Docker or GitHub types, no secrets but a
3//! [`Pairing`]'s one-time link, which its `Debug` leaves out.
4//!
5//! Routes, all under the authenticated principal:
6//!
7//! | Route | Body |
8//! |---|---|
9//! | `GET /v1/status` | [`Status`] |
10//! | `POST /v1/repositories/{owner}/{name}/pause?`[`PauseQuery`] | [`Paused`]; 409 [`Busy`] while it runs a job |
11//! | `POST /v1/repositories/{owner}/{name}/resume` | none, 204 |
12//! | `POST /v1/repositories/pause?`[`PauseQuery`] | [`Paused`]; 409 [`Busy`] while any runs a job, pausing none |
13//! | `POST /v1/repositories/resume` | none, 204: every repository resumed |
14//! | `POST /v1/profiles/{name}/reseed` | none, 202; 409 while it reseeds; `cache.reseeded` when done |
15//! | `POST /v1/caches/{name}/clean` | none, 204; 409 [`Busy`] while a runner uses it |
16//! | `GET /v1/config` | [`ConfigFile`] |
17//! | `POST /v1/config/check` | TOML in, [`ConfigCheck`] |
18//! | `PUT /v1/config` | TOML in, [`ConfigCheck`]; 422 [`ConfigCheck`] when invalid |
19//! | `GET /v1/doctor` | [`Doctor`] |
20//! | `POST /v1/shutdown` | [`ShutdownRequest`] in, 202 [`ShutdownAnswer`] |
21//! | `GET /v1/dashboard` | [`Dashboard`] |
22//! | `POST /v1/dashboard/sign-out` | none, 204 |
23//! | `POST /v1/dashboard/pairings?`[`PairingQuery`] | [`Pairings`]; 409 when the dashboard is off or has no `[web] origins` |
24//! | `GET /v1/jobs?`[`JobQuery`] | [`JobPage`] |
25//! | `GET /v1/jobs/{id}` | [`Job`] |
26//! | `GET /v1/jobs/{id}/samples` | [`SampleSeries`] |
27//! | `GET /v1/jobs/{id}/logs?`[`LogQuery`] | `text/plain`, one line per log line |
28//! | `GET /v1/host/samples?`[`HostSampleQuery`] | [`HostSamples`] |
29//! | `GET /v1/stats?`[`StatsQuery`] | [`Stats`] |
30//! | `GET /v1/activity?`[`ActivityQuery`] | [`Activity`] |
31//! | `GET /v1/sizing?`[`SizingQuery`] | [`Sizing`] |
32//! | `GET /v1/insights?`[`InsightsQuery`] | [`Insights`] |
33//! | `GET /v1/digest` | [`Digest`] |
34//! | `GET /v1/savings` | [`Savings`] |
35//! | `POST /v1/notify/test?`[`NotifyTestQuery`] | [`NotifyTest`]; 404 without `[notify]` targets or with no such one |
36//! | `GET /v1/caches` | [`Caches`] |
37//! | `GET /v1/caches/history?`[`CacheHistoryQuery`] | [`CacheHistory`] |
38//! | `GET /v1/events?`[`EventQuery`] | `text/event-stream` of [`EventEnvelope`] |
39//! | `GET /v1/records` | [`Records`] |
40//! | `DELETE /v1/records/{job}` | [`RemovedRecords`]; 409 while a shell is open in it |
41//! | `DELETE /v1/records?`[`RemoveRecordsQuery`] | [`RemovedRecords`]; records with an open shell stay |
42//! | `POST /v1/records/{job}/shell?`[`ShellQuery`] | `Upgrade:` [`SHELL_PROTOCOL`], 101, then the shell's terminal; 409 when the budget is full |
43//!
44//! Errors carry an [`ErrorResponse`].
45
46use serde::{Deserialize, Serialize};
47
48mod admission;
49mod budget;
50mod control;
51mod events;
52mod insights;
53mod jobs;
54mod records;
55mod results;
56mod savings;
57mod sizing;
58mod slots;
59mod stats;
60mod storage;
61mod tone;
62mod units;
63
64pub use admission::{Admission, AdmissionPolicy, Basis, CpuAdmission, JobPeak, cores, cpus};
65pub use budget::{
66    BudgetParts, ClosedKind, ClosedPart, FreePart, HeldKind, HeldPart, Reading, SlotPart,
67};
68pub use control::{
69    Busy, Check, CheckStatus, ConfigCheck, ConfigFile, Doctor, PauseQuery, Paused, ReplaceQuery,
70    RunningJob,
71};
72pub use events::{Event, EventEnvelope, EventQuery, RunnerFailure};
73pub use insights::{
74    AutoNote, Delivery, Digest, INSIGHT_DAYS, INSIGHT_DAYS_MAX, Insight, InsightKind, Insights,
75    InsightsQuery, LongWait, Notification, NotifyEvent, NotifyTest, NotifyTestQuery, Priority,
76    RepositoryWaits, RunLink, Severity, WaitSpread, WeekWaits,
77};
78pub use jobs::{
79    Activity, ActivityQuery, Bucket, BuildCacheCounts, BuildCacheStats, CacheHistory,
80    CacheHistoryQuery, CacheKind, CacheSize, CacheVolume, Caches, ChartLine, ChartLineKind,
81    ChartLines, Holder, HostMemory, HostSample, HostSampleQuery, HostSamples, Job, JobMetrics,
82    JobPage, JobQuery, JobRunner, JobState, JobSummary, JobTitle, Limit, LimitSource, Limits,
83    LogQuery, LogSummary, Milestone, Outcome, OutcomeKind, Phase, PhaseKind, Pressure,
84    RunComparison, Sample, SampleSeries, SampleUsage, Started, Stats, StatsQuery, StatsRow, Step,
85    Timeline, job_title,
86};
87pub use records::{
88    Record, RecordLimits, RecordNote, RecordRemoval, Records, RemoveRecordsQuery, RemovedRecords,
89    SHELL_PROTOCOL, ShellQuery,
90};
91pub use results::{force_pause_consequence, reseed_requested, reseeded};
92pub use savings::{
93    Period, PricedRun, PricedRunner, Pricing, PricingHistory, SELF_HOSTED_RATE, SavedWindow,
94    Savings, Sku, Usd, Visibility, billed_minutes, reads_since,
95};
96pub use sizing::{
97    Advice, AutoPlan, GRANULE, JobSizing, MemoryBound, MemoryHold, Need, PAGE_CACHE_ROOM, Peaks,
98    ProfilePlan, ProfileSizing, Resize, RevertCause, SAMPLE_INTERVAL, SLOT_DISK, Sizing,
99    SizingChange, SizingHold, SizingQuery, SizingRefused, SizingRevert, Spread, Starts, need,
100    period, sampled_note,
101};
102pub use slots::{Health, SlotState};
103pub use stats::StatsSummary;
104pub use storage::{
105    ACTIONS_CACHE, BUILD_CACHE_TOOLS, BuildCacheActivity, CacheLimit, RECORDS_NEAR_FULL,
106    RecordState, actions_cache_place, records_near_full,
107};
108pub use tone::{Tone, Toned};
109#[cfg(feature = "host")]
110pub(crate) use units::days_from_civil;
111pub use units::{ago, cores_used, cpu_time, duration, in_cores};
112
113/// Who sent a request, as the transport authenticated it. Handlers get it from one place, so a new transport only
114/// has to produce one.
115#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
116#[serde(rename_all = "kebab-case", tag = "kind")]
117pub enum Principal {
118    /// The user the daemon runs as, on the local Unix socket.
119    LocalOwner {
120        /// The Unix user ID the socket's peer runs as.
121        uid: u32,
122    },
123}
124
125/// `GET /v1/status`: what the daemon serves and runs right now.
126#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
127pub struct Status {
128    /// The daemon's version, from its `Cargo.toml`.
129    pub version: String,
130    /// Set once the daemon stops polling to shut down. [`Status::stopping_line`] says how.
131    pub shutting_down: bool,
132    /// How it stops, once it does; absent from a daemon before it said.
133    #[serde(default, skip_serializing_if = "Option::is_none")]
134    pub stopping: Option<Stopping>,
135    /// The memory budget and what runners use of it.
136    pub budget: Budget,
137    /// Every configured profile with its slots.
138    pub profiles: Vec<ProfileStatus>,
139    /// Every configured repository.
140    pub repositories: Vec<RepositoryStatus>,
141    /// Queued jobs the repositories' runners can take and none holds yet, at the last poll, oldest first. No event
142    /// carries the list: `job.queued` announces a job, `job.started` and `job.finished` its leaving the queue.
143    /// A repository whose last listing failed, as while GitHub rate-limits runnerd, or that is no longer polled
144    /// because runnerd is shutting down, shows no jobs here, while the history keeps its jobs queued.
145    pub queued: Vec<QueuedJob>,
146}
147
148/// The memory budget runners and the mirror share, in bytes.
149#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
150pub struct Budget {
151    /// The systemd slice runner containers run in.
152    pub slice: String,
153    /// The slice's `MemoryMax`; `None` when it is unlimited.
154    pub memory_max: Option<u64>,
155    /// Memory the running containers reserve: the runners' caps, or under measured admission the larger of each
156    /// runner's predicted peak and its last sampled memory, and the caps of the seed containers, shells and the
157    /// mirror.
158    pub memory_in_use: u64,
159    /// What a new runner must fit in beside them.
160    pub admission: AdmissionPolicy,
161    /// Under measured admission, whether the slice is at the high-water mark, so no runner starts.
162    pub held: bool,
163    /// The mirror's part of `memory_in_use`: its cap while it is enabled or still running, else 0.
164    pub mirror_memory: u64,
165    /// The seed containers' part of `memory_in_use`, one per profile that reseeds, by profile name.
166    pub seed_memory: Vec<SeedMemory>,
167    /// The shells' part of `memory_in_use`, one per shell open in a record, by job.
168    pub shell_memory: Vec<ShellMemory>,
169    /// What the host's memory leaves runners beside its other programs, from a recent reading of it; absent
170    /// without one.
171    #[serde(default, skip_serializing_if = "Option::is_none")]
172    pub host: Option<HostBudget>,
173    /// The CPU budget, under `[host] cpu_overcommit`. Absent without one.
174    #[serde(default, skip_serializing_if = "Option::is_none")]
175    pub cpu: Option<CpuBudget>,
176    /// The CPUs the slice may use, in millicores: its `AllowedCPUs` or the host's, less under a lower `CPUQuota`,
177    /// whether or not a CPU budget admits runners. The runners' CPU limits are quotas that may add up past it.
178    /// Absent when it could not be read.
179    #[serde(default, skip_serializing_if = "Option::is_none")]
180    pub slice_cpus: Option<u64>,
181}
182
183/// What the host's memory leaves the runners (ADR 0055). New runners fit in the lower of `MemoryMax` and
184/// `limit`; running ones are never stopped for it.
185#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
186pub struct HostBudget {
187    /// The reading it comes from.
188    pub memory: HostMemory,
189    /// What the slice may hold now: what its runners hold without page cache plus the host's `MemAvailable`, less
190    /// a margin kept free for the host's page cache; with a ceiling, plus what the host can still grow by
191    /// (`HostMemory::growable`, ADR 0084).
192    pub limit: u64,
193    /// What that leaves new runners beside what the running ones reserve, in bytes.
194    pub spare: u64,
195    /// Whether the host's tasks stall for memory (`full` pressure), so no runner starts until they stop.
196    pub stalled: bool,
197}
198
199/// Hundredths of a percent as a percentage: `1.5%`, `12%`.
200pub fn percent(hundredths: u32) -> String {
201    let whole = hundredths / 100;
202    match hundredths % 100 / 10 {
203        0 => format!("{whole}%"),
204        tenth => format!("{whole}.{tenth}%"),
205    }
206}
207
208/// The CPU budget runners share, in millicores.
209#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
210pub struct CpuBudget {
211    /// The CPUs the slice may use: its `AllowedCPUs` or the host's, less under a lower `CPUQuota`.
212    pub cpus: u64,
213    /// Those times `cpu_overcommit`: what the runners' reservations must fit.
214    pub budget: u64,
215    /// What the runners reserve: each its predicted average CPU under measured admission, else its `cpus`.
216    pub in_use: u64,
217    /// Whether a queued job waits for CPU that memory and a slot have room for.
218    pub held: bool,
219}
220
221impl CpuBudget {
222    /// The budget's line as clients show it beside memory, such as `CPU 4.5 of 32 CPUs reserved` or, while a job
223    /// waits for it, `CPU 30 of 32 CPUs reserved; a job waits for CPU`.
224    pub fn line(&self) -> String {
225        let mut line = format!(
226            "CPU {} of {} CPUs reserved",
227            cores(self.in_use),
228            cores(self.budget)
229        );
230        if self.held {
231            line.push_str("; a job waits for CPU");
232        }
233        line
234    }
235}
236
237impl Budget {
238    /// How runners are admitted, as clients add it to the budget's line: nothing under caps, else `by predicted
239    /// peaks`, and why nothing starts while the slice is at the high-water mark.
240    pub fn admission_note(&self) -> Option<&'static str> {
241        match (self.admission, self.held) {
242            (AdmissionPolicy::Caps, _) => None,
243            (AdmissionPolicy::Measured, false) => Some("by predicted peaks"),
244            (AdmissionPolicy::Measured, true) => Some(
245                "by predicted peaks; the slice is past 90% of its MemoryMax, so no runner starts",
246            ),
247        }
248    }
249}
250
251/// A shell container's memory cap: its record's profile's limit when the shell opened.
252#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
253pub struct ShellMemory {
254    /// The job whose record the shell runs.
255    pub job: u64,
256    /// Its cap in bytes.
257    pub memory: u64,
258}
259
260/// A seed container's memory cap: its profile's limit when the reseed started, or, for a requested reseed whose
261/// profile drained, the room it holds until it starts.
262#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
263pub struct SeedMemory {
264    /// The profile it reseeds.
265    pub profile: String,
266    /// Its cap in bytes.
267    pub memory: u64,
268}
269
270/// A runner profile and what runs in its slots.
271#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
272pub struct ProfileStatus {
273    /// The name under `[profiles]` in `runnerd.toml`.
274    pub name: String,
275    /// The image its runners run, such as `runnerd-runner:base`.
276    pub image: String,
277    /// CPUs per slot.
278    pub cpus: u32,
279    /// Bytes per slot.
280    pub memory: u64,
281    /// Set while slot 1's layers become the profile's snapshot.
282    pub reseeding: bool,
283    /// Index 0 is slot 1.
284    pub slots: Vec<SlotStatus>,
285}
286
287/// One slot of a profile: room for one job.
288#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
289pub struct SlotStatus {
290    /// 1-based.
291    pub slot: usize,
292    /// `None` when the slot is free.
293    pub runner: Option<RunnerStatus>,
294}
295
296/// A runner registered for one job, in its container.
297#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
298pub struct RunnerStatus {
299    /// `owner/name` the runner is registered on.
300    pub repository: String,
301    /// The runner's name, also its container's.
302    pub name: String,
303    /// The runner's ID on GitHub.
304    pub runner_id: u64,
305    /// The Docker container ID.
306    pub container: String,
307    /// Whether it runs a job, as GitHub or its log reports it: its log names the job before GitHub lists it busy.
308    pub busy: bool,
309    /// Seconds since the Unix epoch.
310    pub started_at: u64,
311    /// What it takes of the budget: its cap under caps; under measured admission the larger of its predicted peak
312    /// and its last sampled memory.
313    pub reserved: u64,
314    /// A warm runner: idle, and no queued job of its repository waits for it; it takes the next one GitHub hands
315    /// out.
316    pub warm: bool,
317}
318
319/// A configured repository and its queue.
320#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
321pub struct RepositoryStatus {
322    /// `owner/name` on GitHub.
323    pub name: String,
324    /// Its pools in config order, one per `[[repositories]]` entry: a job runs in the one whose labels match it
325    /// (ADR 0058).
326    pub pools: Vec<PoolStatus>,
327    /// Paused repositories get no new runners; their running jobs finish.
328    pub paused: bool,
329    /// Its jobs in `Status::queued`: queued ones none of its runners took yet, those no pool serves too.
330    pub queued_jobs: usize,
331}
332
333/// One of a repository's pools: its runners in one profile.
334#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
335pub struct PoolStatus {
336    /// The profile its jobs run in.
337    pub profile: String,
338    /// The labels its runners register with.
339    pub labels: Vec<String>,
340    /// Its jobs in `Status::queued`: those its labels match.
341    pub queued_jobs: usize,
342}
343
344/// A queued job runners of this daemon can take, as GitHub listed it at the last poll.
345#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
346pub struct QueuedJob {
347    /// GitHub's job ID.
348    pub id: u64,
349    /// The repository whose runners can take it, `owner/name`.
350    pub repository: String,
351    /// The profile of its repository's pool whose labels match it; `None` when no pool's do.
352    #[serde(default, skip_serializing_if = "Option::is_none")]
353    pub profile: Option<String>,
354    /// The workflow run it belongs to.
355    pub run_id: u64,
356    /// 1 for the first attempt of the run, higher for re-runs.
357    pub run_attempt: u32,
358    /// The workflow's name.
359    pub workflow: Option<String>,
360    /// The job's name in the workflow.
361    pub name: String,
362    /// The branch the run is for.
363    pub branch: Option<String>,
364    /// The job's page on GitHub.
365    pub url: Option<String>,
366    /// When GitHub queued it, in seconds since the Unix epoch; 0 if GitHub did not say.
367    pub queued_at: u64,
368    /// Why no runner has it yet, as the last plan saw it. `None` before the daemon has planned with it, and while
369    /// it shuts down.
370    #[serde(default, skip_serializing_if = "Option::is_none")]
371    pub waiting: Option<Waiting>,
372}
373
374/// The slot of an idle runner of `job`'s repository, in its pool's profile when known, such as `jvm 2`.
375fn starting_for(job: &QueuedJob, status: &Status) -> Option<String> {
376    status
377        .profiles
378        .iter()
379        .filter(|profile| job.profile.as_ref().is_none_or(|own| *own == profile.name))
380        .find_map(|profile| {
381            profile
382                .slots
383                .iter()
384                .find(|slot| {
385                    slot.runner
386                        .as_ref()
387                        .is_some_and(|runner| runner.repository == job.repository && !runner.busy)
388                })
389                .map(|slot| format!("{}\u{a0}{}", profile.name, slot.slot))
390        })
391}
392
393impl QueuedJob {
394    /// Where it runs, as every client names it: its repository, and its pool's profile when the repository has
395    /// several pools in `status`, such as `halyard/ledger · light`.
396    pub fn place(&self, status: &Status) -> String {
397        let pools = status
398            .repositories
399            .iter()
400            .find(|repository| repository.name == self.repository)
401            .map_or(0, |repository| repository.pools.len());
402        match self.profile.as_deref().filter(|_| pools > 1) {
403            Some(profile) => format!("{}\u{a0}· {profile}", self.repository),
404            None => self.repository.clone(),
405        }
406    }
407}
408
409impl RepositoryStatus {
410    /// The profile of its first pool, which every client colours it by.
411    pub fn first_profile(&self) -> &str {
412        self.pools.first().map_or("", |pool| pool.profile.as_str())
413    }
414
415    /// Its pools' profiles, as every client lists them on one line: `jvm` or `jvm + light`.
416    pub fn profiles(&self) -> String {
417        self.pools
418            .iter()
419            .map(|pool| pool.profile.as_str())
420            .collect::<Vec<_>>()
421            .join(" + ")
422    }
423
424    /// The profile of its pool whose labels match a job asking for `labels`, as the daemon picks it.
425    pub fn pool_for(&self, labels: &[String]) -> Option<String> {
426        self.pools
427            .iter()
428            .find(|pool| crate::config::labels_match(labels, &pool.labels))
429            .map(|pool| pool.profile.clone())
430    }
431}
432
433/// What failed, that holds a repository's runners back for a while.
434#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
435#[serde(rename_all = "kebab-case")]
436pub enum HoldCause {
437    /// Its runners ended without taking a job.
438    NoJob,
439    /// Its runners ended without a job after waiting for one, twice in a row.
440    IdleExits,
441    /// GitHub did not register its runner.
442    Register,
443    /// Docker did not start its runner's container.
444    Start,
445}
446
447/// Why a queued job has no runner of its own yet. Jobs are served oldest first; each takes the first of these that
448/// holds for it.
449#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
450#[serde(rename_all = "kebab-case", tag = "kind")]
451pub enum Waiting {
452    /// Its repository is paused.
453    Paused,
454    /// Runners of its repository failed, so none starts before `until` (seconds since the Unix epoch).
455    Backoff {
456        /// When runners may start again.
457        until: u64,
458        /// What failed.
459        cause: HoldCause,
460    },
461    /// A runner started for it and waits for GitHub to hand it the job: a runner takes any job its labels match,
462    /// so not necessarily this one.
463    RunnerStarting,
464    /// Slot 1 of its profile is becoming the profile's snapshot, so none of the profile's slots starts.
465    Reseeding {
466        /// The profile.
467        profile: String,
468    },
469    /// Its profile's image is being pulled, so none of the profile's slots starts.
470    PullingImage {
471        /// The profile.
472        profile: String,
473    },
474    /// Every slot of its profile holds a runner.
475    NoSlot {
476        /// The profile.
477        profile: String,
478    },
479    /// What its runner would reserve does not fit the memory budget left. Under measured admission, `free` is 0
480    /// while the slice is at the high-water mark (`Budget::held`).
481    Memory {
482        /// What its runner would reserve, in bytes.
483        needs: u64,
484        /// What the budget has left, in bytes.
485        free: u64,
486    },
487    /// What its runner would reserve does not fit what the host's memory has to spare beside its other programs
488    /// (`HostBudget::spare`), though it fits the slice's budget. `free` is 0 while the host's tasks stall for memory
489    /// (`HostBudget::stalled`).
490    HostMemory {
491        /// What its runner would reserve, in bytes.
492        needs: u64,
493        /// What the host has to spare, in bytes.
494        free: u64,
495    },
496    /// What its runner would reserve does not fit the CPU budget left, in millicores.
497    Cpu {
498        /// What its runner would reserve.
499        needs: u64,
500        /// What the CPU budget has left.
501        free: u64,
502    },
503    /// An older job has waited long enough for CPU that no younger one takes CPU before it.
504    BehindCpu {
505        /// The older job's ID.
506        job: u64,
507    },
508    /// An older job is about to have a runner start for it, which takes the slot, memory or CPU this one would
509    /// otherwise start with: what holds it is that start, not what runs now.
510    BehindStart {
511        /// The older job's ID.
512        job: u64,
513    },
514    /// No pool of its repository has every label it asks for, so no runner of this daemon can take it (ADR 0058).
515    NoPool {
516        /// The labels it asks for.
517        labels: Vec<String>,
518    },
519    /// A reason from a newer daemon than this client knows, so a reason added later does not keep the client from
520    /// reading the status.
521    #[serde(other)]
522    Unknown,
523}
524
525/// What a [`Waiting`] reason waits on, which clients draw it by.
526#[derive(Debug, Clone, Copy, PartialEq, Eq)]
527pub enum WaitKind {
528    /// Something the owner can act on: a pause, runners that failed to take a job, or labels no pool serves.
529    Act,
530    /// Something that clears by itself shortly: a runner starting, a reseed, an image pull, an older job's start.
531    Soon,
532    /// Room: a slot, memory or CPU that a running job holds.
533    Full,
534    /// A reason this client does not know (`Waiting::Unknown`), drawn neutral.
535    Other,
536}
537
538impl Waiting {
539    /// What it waits on.
540    pub fn kind(&self) -> WaitKind {
541        match self {
542            Waiting::Paused | Waiting::Backoff { .. } | Waiting::NoPool { .. } => WaitKind::Act,
543            Waiting::Unknown => WaitKind::Other,
544            Waiting::RunnerStarting
545            | Waiting::Reseeding { .. }
546            | Waiting::PullingImage { .. }
547            | Waiting::BehindStart { .. } => WaitKind::Soon,
548            Waiting::NoSlot { .. }
549            | Waiting::Memory { .. }
550            | Waiting::HostMemory { .. }
551            | Waiting::Cpu { .. }
552            | Waiting::BehindCpu { .. } => WaitKind::Full,
553        }
554    }
555
556    /// Why `job` waits, in the owner's words with the numbers that hold it, as every client says it: such as
557    /// `All 2 node slots are busy` or `Needs 9G of memory; 2.5G of the budget is free`. `status` is the one `job`
558    /// is queued in, which names the slot of a starting runner and the older job one waits behind.
559    pub fn reason(&self, job: &QueuedJob, status: &Status, now: u64) -> String {
560        let size = |bytes: u64| crate::config::ByteSize(bytes).to_string();
561        let older_name = |id: u64| {
562            status
563                .queued
564                .iter()
565                .find(|candidate| candidate.id == id)
566                .map_or_else(|| "an older job".to_owned(), |older| older.name.clone())
567        };
568        match self {
569            Waiting::Paused => format!("{} is paused", job.repository),
570            Waiting::Unknown => "Waits for a reason this client does not know yet".to_owned(),
571            Waiting::NoPool { labels } if labels.is_empty() => {
572                "It asks for no labels, so no profile serves it".to_owned()
573            }
574            Waiting::NoPool { labels } => format!(
575                "No profile serves the {} {}",
576                if labels.len() == 1 { "label" } else { "labels" },
577                labels.join(", ")
578            ),
579            Waiting::Backoff { until, cause } => format!(
580                "{}; retrying in {}",
581                match cause {
582                    HoldCause::NoJob => "Runners failed to take a job",
583                    HoldCause::IdleExits => "Runners ended without a job after waiting for one",
584                    HoldCause::Register => "Runners cannot register with GitHub",
585                    HoldCause::Start => "Runners cannot start",
586                },
587                duration(until.saturating_sub(now))
588            ),
589            Waiting::RunnerStarting => match starting_for(job, status) {
590                Some(slot) => format!("A runner is starting for it in {slot}"),
591                None => "A runner is starting for it".to_owned(),
592            },
593            Waiting::Reseeding { profile } => format!("{profile} is rebuilding its snapshot"),
594            Waiting::PullingImage { profile } => format!("Pulling {profile}'s image"),
595            Waiting::NoSlot { profile } => {
596                let slots = status
597                    .profiles
598                    .iter()
599                    .find(|candidate| &candidate.name == profile)
600                    .map_or(0, |profile| profile.slots.len());
601                if slots == 1 {
602                    format!("The {profile} slot is busy")
603                } else {
604                    format!("All {slots} {profile} slots are busy")
605                }
606            }
607            Waiting::Memory { .. } if status.budget.held => {
608                "The slice is past 90% of its MemoryMax, so nothing starts".to_owned()
609            }
610            Waiting::Memory { needs, free } => format!(
611                "Needs {} of memory; {} of the budget is free",
612                size(*needs),
613                size(*free)
614            ),
615            Waiting::HostMemory { needs, free } => {
616                let host = status.budget.host.as_ref();
617                let reason = if host.is_some_and(|host| host.stalled) {
618                    "The host is short of memory: its programs stall waiting for it, so nothing starts"
619                        .to_owned()
620                } else {
621                    format!(
622                        "The host is short of memory: needs {}; the host has {} to spare",
623                        size(*needs),
624                        size(*free)
625                    )
626                };
627                match host {
628                    Some(host) => format!("{reason}; {}", host.memory.outside_line()),
629                    None => reason,
630                }
631            }
632            Waiting::Cpu { needs, free } => {
633                format!(
634                    "Needs {}; {} are free",
635                    cpus(*needs),
636                    admission::free_cores(*free)
637                )
638            }
639            Waiting::BehindCpu { job: older } => {
640                format!("Behind {}, which waits for CPU", older_name(*older))
641            }
642            Waiting::BehindStart { job: older } => {
643                format!(
644                    "Next after {}, whose runner starts first",
645                    older_name(*older)
646                )
647            }
648        }
649    }
650}
651
652/// `GET /v1/dashboard`: the dashboard's `[web]` settings, as the daemon read them when it started.
653#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
654pub struct Dashboard {
655    /// `[web] open`: the one of `[web] origins` that `runnerctl web` opens the dashboard on; the loopback address
656    /// when none.
657    #[serde(default, skip_serializing_if = "Option::is_none")]
658    pub open: Option<String>,
659}
660
661/// `POST /v1/dashboard/pairings`: which of `[web] origins` to make a link for.
662#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
663pub struct PairingQuery {
664    /// One of `[web] origins`; each of them when none.
665    #[serde(default, skip_serializing_if = "Option::is_none")]
666    pub origin: Option<String>,
667}
668
669/// `POST /v1/dashboard/pairings`: fresh one-time links that sign a device in to the dashboard.
670#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
671pub struct Pairings {
672    /// One per origin asked for.
673    pub links: Vec<Pairing>,
674}
675
676/// A link that signs one device in to the dashboard on `origin`, and nowhere else, within `lifetime`.
677#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
678pub struct Pairing {
679    /// One of `[web] origins`.
680    pub origin: String,
681    /// The link with its one-time code: a credential, which `Debug` leaves out.
682    pub url: String,
683    /// Seconds it works for from when it was made.
684    pub lifetime: u64,
685}
686
687impl std::fmt::Debug for Pairing {
688    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
689        formatter
690            .debug_struct("Pairing")
691            .field("origin", &self.origin)
692            .field("lifetime", &self.lifetime)
693            .finish_non_exhaustive()
694    }
695}
696
697/// `POST /v1/shutdown`.
698#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
699pub struct ShutdownRequest {
700    /// Kill running jobs and delete their runners instead of waiting for them.
701    #[serde(default)]
702    pub force: bool,
703}
704
705/// What `POST /v1/shutdown` answers: how the daemon stops now, which a stop asked for earlier may have made more
706/// urgent than the request.
707#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
708pub struct ShutdownAnswer {
709    /// How it stops.
710    pub stopping: Stopping,
711}
712
713/// How a stopping daemon treats running jobs (ADR 0091).
714#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
715#[serde(rename_all = "kebab-case")]
716pub enum Stopping {
717    /// `runnerd stop`: it waits for running jobs, then removes every runner.
718    WaitsForJobs,
719    /// A signal: it removes idle runners and leaves running jobs to the next start.
720    LeavesJobs,
721    /// `runnerd stop --force` or a second signal: it kills running jobs.
722    KillsJobs,
723    /// A kind of stop from a newer daemon than this client knows, so one added later does not keep the client from
724    /// reading the status.
725    #[serde(other)]
726    Unknown,
727}
728
729impl Stopping {
730    /// What it does, as every client says it: `shutting down: waits for running jobs`.
731    pub fn line(self) -> &'static str {
732        match self {
733            Stopping::WaitsForJobs => "shutting down: waits for running jobs",
734            Stopping::LeavesJobs => "shutting down: leaves running jobs to the next start",
735            Stopping::KillsJobs => "shutting down: kills running jobs",
736            Stopping::Unknown => "shutting down",
737        }
738    }
739}
740
741impl Status {
742    /// What a stopping daemon does, as every client says it ([`Stopping::line`]); `shutting down` from a daemon
743    /// that does not say how; `None` while it runs.
744    pub fn stopping_line(&self) -> Option<&'static str> {
745        match (self.stopping, self.shutting_down) {
746            (Some(stopping), _) => Some(stopping.line()),
747            (None, true) => Some("shutting down"),
748            (None, false) => None,
749        }
750    }
751}
752
753/// The body of every error response.
754#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
755pub struct ErrorResponse {
756    /// What went wrong, for people to read.
757    pub message: String,
758}