Skip to main content

runnerd_core/store/
run.rs

1//! The jobs of one workflow run that the history has, which a job action's confirmation names beside its job
2//! (ADR 0134), and the jobs their runner failed that runnerd may still re-run (ADR 0153). Reads only, a bounded
3//! number of jobs; it decides nothing about what GitHub's run holds.
4
5use rusqlite::{Connection, params};
6
7use super::read;
8use crate::api;
9
10/// Other jobs of a run the history has.
11#[derive(Debug, Clone, Default, PartialEq, Eq)]
12pub struct RunJobs {
13    /// Newest attempt first, then by ID.
14    pub jobs: Vec<api::JobSummary>,
15    /// Whether the history has more than `jobs`.
16    pub more: bool,
17}
18
19/// Up to `limit` jobs of `repository`'s run `run` but `except`, newest attempt first, and whether there are more.
20pub(super) fn run_jobs(
21    db: &Connection,
22    repository: &str,
23    run: u64,
24    except: u64,
25    limit: usize,
26) -> rusqlite::Result<RunJobs> {
27    let mut statement = db.prepare_cached(
28        "SELECT id FROM jobs WHERE repository = ?1 AND run_id = ?2 AND id != ?3 \
29         ORDER BY run_attempt DESC, id LIMIT ?4",
30    )?;
31    let ids = statement
32        .query_map(
33            params![
34                repository,
35                run,
36                except,
37                i64::try_from(limit).unwrap_or(i64::MAX).saturating_add(1)
38            ],
39            |row| row.get::<_, u64>(0),
40        )?
41        .collect::<rusqlite::Result<Vec<u64>>>()?;
42    let more = ids.len() > limit;
43    let mut jobs = Vec::new();
44    for id in ids.into_iter().take(limit) {
45        jobs.extend(read::summary(db, id)?);
46    }
47    Ok(RunJobs { jobs, more })
48}
49
50/// A job its runner failed, not re-run by runnerd yet.
51#[derive(Debug, Clone, PartialEq, Eq)]
52pub struct RunnerFault {
53    /// The job.
54    pub job: u64,
55    /// `owner/name`.
56    pub repository: String,
57    /// Its workflow run.
58    pub run: u64,
59    /// The run's attempt it ran in.
60    pub attempt: u32,
61}
62
63/// What claiming an automatic re-run came to (ADR 0153).
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub enum Claim {
66    /// The attempt is this caller's to send.
67    Claimed,
68    /// An earlier send claimed it, in this daemon's life or a past one: it is never sent again.
69    Taken,
70    /// The claim could not be stored: nothing may be sent.
71    Unstored,
72}
73
74/// One attempt of a repository's run, as a claim names it.
75#[derive(Debug)]
76pub(super) struct ClaimKey {
77    /// `owner/name`.
78    pub(super) repository: String,
79    /// The workflow run.
80    pub(super) run: u64,
81    /// Its attempt.
82    pub(super) attempt: u32,
83}
84
85/// How long a claim is kept: far longer than a fault is read back.
86pub(super) const CLAIM_KEPT_MS: u64 = 24 * 60 * 60 * 1000;
87
88/// The most faults a restart reads back: far more than a day has ever had.
89const FAULTS_MAX: u32 = 500;
90
91/// The jobs their runner failed, final at or after `since` ms, whose attempt runnerd neither re-ran nor claimed,
92/// oldest first.
93pub(super) fn runner_faults(db: &Connection, since: u64) -> rusqlite::Result<Vec<RunnerFault>> {
94    db.prepare_cached(
95        "SELECT id, repository, run_id, run_attempt FROM jobs WHERE runner_fault = 1 AND state = 'completed' \
96         AND rerun_at IS NULL AND run_id != 0 AND coalesce(finished_at, queued_at) >= ?1 AND NOT EXISTS \
97         (SELECT 1 FROM rerun_claims c WHERE c.repository = jobs.repository AND c.run_id = jobs.run_id \
98         AND c.attempt = jobs.run_attempt) ORDER BY id LIMIT ?2",
99    )?
100    .query_map(params![since, FAULTS_MAX], |row| {
101        Ok(RunnerFault {
102            job: row.get(0)?,
103            repository: row.get(1)?,
104            run: row.get(2)?,
105            attempt: row.get(3)?,
106        })
107    })?
108    .collect()
109}