runnerd_core/api/jobs.rs
1//! Job history as clients read it: one record per GitHub job a runner of this daemon ran, its timeline, steps,
2//! metrics and log size; the queries that list them; sample series for graphs; per-repository aggregates; cache
3//! sizes. Times are seconds since the Unix epoch, sizes are bytes, CPU is in millicores (1000 = one core busy).
4
5use serde::{Deserialize, Serialize};
6
7/// Where a job is in its life, as this daemon saw it.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(rename_all = "kebab-case")]
10pub enum JobState {
11 /// Waiting for a runner.
12 Queued,
13 /// A runner of this daemon took it.
14 Running,
15 /// GitHub reports it completed; `conclusion` says how.
16 Completed,
17 /// Its runner's container is gone and GitHub did not report the job completed in time.
18 Lost,
19 /// A runner of another machine took it, or it was cancelled before any runner did.
20 Elsewhere,
21}
22
23impl JobState {
24 /// Whether the job can still change.
25 pub fn is_final(self) -> bool {
26 matches!(
27 self,
28 JobState::Completed | JobState::Lost | JobState::Elsewhere
29 )
30 }
31}
32
33/// A job without its steps: what lists and events carry.
34#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
35pub struct JobSummary {
36 /// GitHub's job ID, also its check run ID.
37 pub id: u64,
38 /// `owner/name` on GitHub.
39 pub repository: String,
40 /// The workflow run it belongs to.
41 pub run_id: u64,
42 /// 1 for the first attempt of the run, higher for re-runs.
43 pub run_attempt: u32,
44 /// The workflow's name.
45 pub workflow: Option<String>,
46 /// The job's name in the workflow.
47 pub name: String,
48 /// The branch the run is for.
49 pub branch: Option<String>,
50 /// The job's page on GitHub.
51 pub url: Option<String>,
52 /// The workflow's `runs-on` labels.
53 pub labels: Vec<String>,
54 /// Where the job is.
55 pub state: JobState,
56 /// GitHub's conclusion once completed: `success`, `failure`, `cancelled`, `skipped`, `timed_out`, ….
57 pub conclusion: Option<String>,
58 /// The runner that ran it.
59 pub runner: Option<JobRunner>,
60 /// When each phase started.
61 pub timeline: Timeline,
62 /// CPU, memory and network use while it ran.
63 pub metrics: JobMetrics,
64 /// How much of its log is stored.
65 pub log: LogSummary,
66}
67
68/// `GET /v1/jobs/{id}`: a job with its steps.
69#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
70pub struct Job {
71 /// Everything but the steps.
72 #[serde(flatten)]
73 pub summary: JobSummary,
74 /// The steps as GitHub numbers them, including `Set up job` and `Complete job`.
75 pub steps: Vec<Step>,
76 /// Its record, for a failed job of a profile that records failures.
77 #[serde(default)]
78 pub record: Option<super::RecordNote>,
79 /// How its runner was admitted; `None` for one started before runnerd recorded it.
80 #[serde(default)]
81 pub admission: Option<super::Admission>,
82}
83
84/// The runner a job ran on.
85#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
86pub struct JobRunner {
87 /// The runner's name, also its container's.
88 pub name: String,
89 /// The profile it ran in.
90 pub profile: String,
91 /// The slot it ran in, 1-based.
92 pub slot: usize,
93 /// Its container's memory cap in bytes, which a reload since may have changed in the profile. `None` in records
94 /// from before runnerd kept it.
95 pub memory_cap: Option<u64>,
96 /// Its container's CPU limit in millicores, the unit of the CPU use in `Sample` and `JobMetrics`. `None` in
97 /// records from before runnerd kept it, or when Docker reports no limit.
98 pub cpu_cap: Option<u64>,
99}
100
101/// A job's phases, in the order they happen. A runner's container starts before GitHub gives it a job.
102#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
103pub struct Timeline {
104 /// GitHub queued the job.
105 pub queued_at: u64,
106 /// The container of the runner that took it started.
107 pub container_started_at: Option<u64>,
108 /// The runner started the job.
109 pub started_at: Option<u64>,
110 /// The job finished, by GitHub's clock; or when its container was found gone.
111 pub finished_at: Option<u64>,
112 /// The container and the runner's registration were removed.
113 pub cleaned_at: Option<u64>,
114}
115
116/// What a job used, over the samples taken while it ran (every few seconds). `None` until a sample exists.
117#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
118pub struct JobMetrics {
119 /// CPU time, in milliseconds.
120 pub cpu_time: Option<u64>,
121 /// The highest CPU use of a sample interval, in millicores.
122 pub cpu_peak: Option<u64>,
123 /// CPU time over wall time, in millicores.
124 pub cpu_average: Option<u64>,
125 /// The highest memory use without page cache.
126 pub memory_peak: Option<u64>,
127 /// The average memory use without page cache.
128 pub memory_average: Option<u64>,
129 /// Bytes the container received.
130 pub network_received: Option<u64>,
131 /// Bytes the container sent.
132 pub network_sent: Option<u64>,
133 /// Of the bytes received, those the Docker Hub mirror served.
134 pub mirror_bytes: u64,
135}
136
137/// A job's stored log.
138#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
139pub struct LogSummary {
140 /// Lines stored.
141 pub lines: u64,
142 /// Bytes stored.
143 pub bytes: u64,
144 /// Set once the log reached `history.log_max` and later lines were dropped.
145 pub truncated: bool,
146}
147
148/// One step of a job, as GitHub reports it.
149#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
150pub struct Step {
151 /// GitHub's step number; numbers may skip.
152 pub number: u32,
153 /// The name GitHub shows, such as `Set up job` or `Run docker run --rm hello-world`.
154 pub name: String,
155 /// `pending`, `queued`, `in_progress` or `completed`.
156 pub status: String,
157 /// Once completed: `success`, `failure`, `skipped`, `cancelled`, ….
158 pub conclusion: Option<String>,
159 /// When it started.
160 pub started_at: Option<u64>,
161 /// When it completed.
162 pub completed_at: Option<u64>,
163}
164
165/// `GET /v1/jobs` query parameters. Jobs come newest first (by ID, which grows with time).
166#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
167pub struct JobQuery {
168 /// Only this `owner/name`.
169 pub repository: Option<String>,
170 /// Only jobs in this state.
171 pub state: Option<JobState>,
172 /// Only jobs with this conclusion, such as `failure`.
173 pub conclusion: Option<String>,
174 /// Only jobs of the workflow with this name, such as `CI`.
175 pub workflow: Option<String>,
176 /// Only jobs with this name in their workflow, such as `build`.
177 pub name: Option<String>,
178 /// Only jobs queued at or after this time.
179 pub since: Option<u64>,
180 /// Only jobs queued before this time.
181 pub until: Option<u64>,
182 /// Only jobs with a smaller ID: the `next` of the previous page.
183 pub before: Option<u64>,
184 /// At most this many jobs; default 50, at most 500.
185 pub limit: Option<u32>,
186}
187
188/// `GET /v1/jobs`: one page of jobs.
189#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
190pub struct JobPage {
191 /// Newest first.
192 pub jobs: Vec<JobSummary>,
193 /// Pass as `before` for the next page; `None` on the last page.
194 pub next: Option<u64>,
195}
196
197/// `GET /v1/jobs/{id}/logs` query parameters. The response is `text/plain`, one log line per line.
198#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
199pub struct LogQuery {
200 /// Start at this line, 0-based.
201 pub from: Option<u64>,
202 /// Keep the response open and send lines as they are stored, until the job finishes.
203 #[serde(default)]
204 pub follow: bool,
205}
206
207/// One sample of a container's use.
208#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
209pub struct Sample {
210 /// When it was taken.
211 pub at: u64,
212 /// CPU use since the previous sample, in millicores.
213 pub cpu: u64,
214 /// Memory use without page cache.
215 pub memory: u64,
216 /// Bytes received since the container started.
217 pub network_received: u64,
218 /// Bytes sent since the container started.
219 pub network_sent: u64,
220}
221
222/// `GET /v1/jobs/{id}/samples`: what the job's container used over time, from its start to its removal. Samples
223/// older than `history.samples` are deleted; the job's metrics stay.
224#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
225pub struct SampleSeries {
226 /// The job.
227 pub job: u64,
228 /// Seconds between samples.
229 pub interval: u64,
230 /// Oldest first.
231 pub samples: Vec<Sample>,
232}
233
234/// One sample of the whole slice runners run in.
235#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
236pub struct HostSample {
237 /// When it was taken.
238 pub at: u64,
239 /// CPU use of the slice since the previous sample, in millicores.
240 pub cpu: u64,
241 /// Memory use of the slice less its inactive page cache, as `docker stats` counts it: a conservative number,
242 /// since active page cache still counts, which the high-water mark of measured admission compares to
243 /// `MemoryMax`.
244 pub memory: u64,
245}
246
247/// `GET /v1/host/samples` query parameters.
248#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
249pub struct HostSampleQuery {
250 /// Only samples taken at or after this time; default the last hour, at most the last 24 hours.
251 pub since: Option<u64>,
252}
253
254/// `GET /v1/host/samples`: the slice's use over time, oldest first.
255#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
256pub struct HostSamples {
257 /// Seconds between samples.
258 pub interval: u64,
259 /// Oldest first.
260 pub samples: Vec<HostSample>,
261}
262
263/// How `GET /v1/stats` groups jobs in time.
264#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
265#[serde(rename_all = "kebab-case")]
266pub enum Bucket {
267 /// By UTC hour.
268 Hour,
269 /// By UTC day.
270 #[default]
271 Day,
272}
273
274/// `GET /v1/activity` query parameters.
275#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
276pub struct ActivityQuery {
277 /// The start of the window; default 24 hours ago. It ends now.
278 pub since: Option<u64>,
279}
280
281/// `GET /v1/activity`: every job runnerd's runners started or finished in a window, by when that happened rather
282/// than when the job was queued, so a job that waited or ran long counts in the window it started or ended in.
283#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
284pub struct Activity {
285 /// The start of the window.
286 pub since: u64,
287 /// When it was read: the end of the window.
288 pub until: u64,
289 /// Each job that started in the window, oldest start first.
290 pub started: Vec<Started>,
291 /// The jobs that finished in the window: completed, or lost with their runner.
292 pub finished: u64,
293 /// Of those, the ones that failed, timed out or were lost, as `GET /v1/stats` counts failures.
294 pub failed: u64,
295}
296
297impl Activity {
298 /// The 95th percentile of the waits of the jobs that started, by nearest rank as sizing's are; `None` when none
299 /// started.
300 pub fn wait_p95(&self) -> Option<u64> {
301 let mut waits: Vec<u64> = self.started.iter().map(|start| start.waited).collect();
302 waits.sort_unstable();
303 let rank = (waits.len() * 95).div_ceil(100);
304 waits.get(rank.checked_sub(1)?).copied()
305 }
306
307 /// The longest wait of the jobs that started in each of the 24 hours before `until`, oldest hour first; 0 for
308 /// an hour without a start. Counted back from `until`, so the hours are the day the other figures count.
309 pub fn wait_hours(&self) -> [u64; 24] {
310 const HOUR: u64 = 3600;
311 let since = self.until.saturating_sub(24 * HOUR);
312 let mut hours = [0u64; 24];
313 for start in self
314 .started
315 .iter()
316 .filter(|start| (since..=self.until).contains(&start.at))
317 {
318 // A start exactly 24 hours ago is in the oldest hour.
319 let back =
320 usize::try_from((self.until - start.at) / HOUR).map_or(23, |back| back.min(23));
321 hours[23 - back] = hours[23 - back].max(start.waited);
322 }
323 hours
324 }
325}
326
327/// A job that started: when, and how long it had waited since it was queued.
328#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
329pub struct Started {
330 /// When it started.
331 pub at: u64,
332 /// Seconds it waited.
333 pub waited: u64,
334}
335
336/// `GET /v1/stats` query parameters.
337#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
338pub struct StatsQuery {
339 /// Only this `owner/name`.
340 pub repository: Option<String>,
341 /// Only jobs queued at or after this time; default 30 days ago. At most 744 buckets before `until` (or now)
342 /// are counted: 31 days of hours, two years of days.
343 pub since: Option<u64>,
344 /// Only jobs queued before this time.
345 pub until: Option<u64>,
346 /// The bucket size; default a day.
347 #[serde(default)]
348 pub bucket: Bucket,
349}
350
351/// `GET /v1/stats`: finished jobs per repository and time bucket, for trends. Sums, so clients can average.
352#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
353pub struct Stats {
354 /// The bucket size.
355 pub bucket: Bucket,
356 /// By bucket start, then repository.
357 pub rows: Vec<StatsRow>,
358}
359
360/// The finished jobs of one repository in one bucket.
361#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
362pub struct StatsRow {
363 /// The bucket's start.
364 pub start: u64,
365 /// `owner/name`.
366 pub repository: String,
367 /// Jobs completed or lost.
368 pub jobs: u64,
369 /// Of those, concluded `success`.
370 pub succeeded: u64,
371 /// Of those, concluded `failure` or `timed_out`, or lost.
372 pub failed: u64,
373 /// Of those, concluded `cancelled`.
374 pub cancelled: u64,
375 /// Seconds from queued to started, summed.
376 pub queue_seconds: u64,
377 /// Seconds from started to finished, summed.
378 pub run_seconds: u64,
379 /// CPU time, in milliseconds, summed.
380 pub cpu_time: u64,
381 /// The highest memory peak of a job.
382 pub memory_peak: u64,
383 /// Bytes received, summed.
384 pub network_received: u64,
385 /// Bytes from the mirror, summed.
386 pub mirror_bytes: u64,
387}
388
389/// What a cache volume holds.
390#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
391#[serde(rename_all = "kebab-case", tag = "kind")]
392pub enum CacheKind {
393 /// A profile's read-only snapshot.
394 Snapshot {
395 /// The profile.
396 profile: String,
397 },
398 /// A slot's layer changes and Docker Engine data.
399 Slot {
400 /// The profile.
401 profile: String,
402 /// 1-based.
403 slot: usize,
404 },
405 /// The pnpm store all slots share.
406 Pnpm,
407 /// The Maven repository all slots share.
408 Maven,
409 /// The Docker Hub mirror's storage.
410 Mirror,
411}
412
413/// One cache volume and its size.
414#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
415pub struct CacheVolume {
416 /// The Docker volume.
417 pub name: String,
418 /// What it holds.
419 #[serde(flatten)]
420 pub kind: CacheKind,
421 /// Its size at `measured_at`.
422 pub bytes: u64,
423 /// The size above which runnerd empties it, or for a snapshot starts it over at its next reseed, if it has one.
424 pub limit: Option<u64>,
425}
426
427/// `GET /v1/caches`: every cache volume of the configured profiles, as last measured. Docker measures volumes
428/// slowly, so runnerd measures them at start and whenever no runner runs after jobs ended.
429#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
430pub struct Caches {
431 /// When the sizes were measured; `None` before the first measurement.
432 pub measured_at: Option<u64>,
433 /// By name.
434 pub volumes: Vec<CacheVolume>,
435}
436
437/// `GET /v1/caches/history` query parameters.
438#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
439pub struct CacheHistoryQuery {
440 /// Only measurements at or after this time; default 30 days ago.
441 pub since: Option<u64>,
442}
443
444/// One volume's size at one measurement.
445#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
446pub struct CacheSize {
447 /// When it was measured.
448 pub at: u64,
449 /// The Docker volume.
450 pub name: String,
451 /// Its size.
452 pub bytes: u64,
453}
454
455/// `GET /v1/caches/history`: every measurement, oldest first.
456#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
457pub struct CacheHistory {
458 /// Oldest first.
459 pub sizes: Vec<CacheSize>,
460}