OXYGENHUB
◆ v0.3.1 oxygenhub / crates / hub / src / db / flow.rs
23.5 KiB · Rust · last changed Cancel runs from oh, the run page, and newer pushes 1 month ago source blame history raw
42fa9554 Andy McCall ↶ 1
//! OxygenFlow runs, jobs and logs (hub spec §13.2).
2
3
use super::{now_ms, Pool};
4
use sqlx::FromRow;
5
6
#[derive(Clone, Debug, FromRow)]
7
pub struct Run {
8
    pub id: i64,
9
    pub repo_id: i64,
a0d4bbfb Andy McCall ↶ 10
    /// The run's number within its repository, from 1 (migration 0019).
11
    #[sqlx(default)]
12
    pub number: i64,
1edac4b6 Andy McCall ↶ 13
    /// `push`, `manual` or `rerun:N` (migration 0020).
14
    #[sqlx(default)]
15
    pub trigger: String,
42fa9554 Andy McCall ↶ 16
    pub bookmark: String,
17
    pub state: String,
18
    pub heads: String,
19
    pub pipeline_hash: String,
20
    pub status: String,
21
    pub error: String,
22
    pub actor: String,
23
    pub created_ms: i64,
24
    pub finished_ms: Option<i64>,
25
}
26
27
impl Run {
1edac4b6 Andy McCall ↶ 28
    /// How the run page says what started it.
29
    pub fn started_by(&self) -> String {
30
        match self.trigger.as_str() {
31
            "manual" => format!("started by {}", self.actor),
c889911b Andy McCall ↶ 32
            "schedule" => "started on schedule".to_string(),
33
            t if t.starts_with("tag:") => format!("tag {} pushed by {}", &t[4..], self.actor),
34
            t if t.starts_with("rerun:") => format!("re-run of #{} by {}", self.rerun_of().unwrap_or(""), self.actor),
1edac4b6 Andy McCall ↶ 35
            _ => format!("pushed by {}", self.actor),
36
        }
37
    }
38
c889911b Andy McCall ↶ 39
    /// The run a rerun repeats: `N` of `rerun:N` or `rerun:N:EVENT`.
40
    pub fn rerun_of(&self) -> Option<&str> {
41
        let rest = self.trigger.strip_prefix("rerun:")?;
42
        Some(rest.split(':').next().unwrap_or(rest))
43
    }
44
45
    /// The trigger as people read it: `rerun:N`, without the event it repeats.
46
    pub fn trigger_label(&self) -> String {
47
        match self.rerun_of() {
48
            Some(n) => format!("rerun:{n}"),
49
            None => self.trigger.clone(),
50
        }
51
    }
52
42fa9554 Andy McCall ↶ 53
    pub fn head_list(&self) -> Vec<&str> {
54
        self.heads.split('+').filter(|h| !h.is_empty()).collect()
55
    }
56
57
    pub fn live(&self) -> bool {
58
        matches!(self.status.as_str(), "queued" | "running")
59
    }
60
}
61
62
#[derive(Clone, Debug, FromRow)]
63
pub struct Job {
64
    pub id: i64,
65
    pub run_id: i64,
66
    pub name: String,
67
    pub depth: i64,
68
    pub depends: String,
69
    pub spec: String,
70
    pub sandbox: String,
71
    pub runtime: String,
72
    pub status: String,
73
    pub runner: String,
74
    pub token_hash: String,
75
    pub started_ms: Option<i64>,
76
    pub finished_ms: Option<i64>,
77
    pub exit_code: Option<i64>,
78
    pub error: String,
79
    pub receipt: String,
80
    pub receipt_hash: String,
81
    pub log_count: i64,
fd3db644 Andy McCall ↶ 82
    /// Comma-separated ids of other repositories the job token may read.
83
    pub also_read: String,
cc19697f Andy McCall ↶ 84
    /// The receipt attestation kept in the repository (Oxygen spec §14.6).
85
    pub receipt_attest: String,
86
    /// Who signed it: the runner's identity name.
87
    pub receipt_signer: String,
88
    /// Why a signed receipt was not kept.
89
    pub receipt_note: String,
42fa9554 Andy McCall ↶ 90
}
91
92
impl Job {
93
    pub fn depends_on(&self) -> Vec<String> {
94
        serde_json::from_str(&self.depends).unwrap_or_default()
95
    }
96
97
    pub fn done(&self) -> bool {
745d225d Andy McCall ↶ 98
        matches!(self.status.as_str(), "passed" | "failed" | "skipped" | "error" | "cancelled")
42fa9554 Andy McCall ↶ 99
    }
100
}
101
102
#[derive(Clone, Debug, FromRow)]
103
pub struct LogLine {
104
    pub seq: i64,
105
    pub t: i64,
106
    pub stream: String,
107
    pub text: String,
108
}
109
110
/// A job as the trigger creates it.
111
pub struct NewJob {
112
    pub name: String,
113
    pub depth: i64,
114
    pub depends: Vec<String>,
115
    pub spec: String,
116
    pub sandbox: String,
117
    pub runtime: String,
118
}
119
120
pub struct NewRun<'a> {
121
    pub repo_id: i64,
122
    pub bookmark: &'a str,
123
    pub state: &'a str,
124
    pub heads: &'a str,
125
    pub pipeline_hash: &'a str,
126
    pub actor: &'a str,
1edac4b6 Andy McCall ↶ 127
    /// `push`, `manual` or `rerun:N`.
128
    pub trigger: &'a str,
42fa9554 Andy McCall ↶ 129
    /// Problems when the pipeline is invalid: the run is recorded in error.
130
    pub error: Option<&'a str>,
131
}
132
133
pub async fn create_run(pool: &Pool, r: NewRun<'_>, jobs: &[NewJob]) -> sqlx::Result<i64> {
941abdd6 Andy McCall ↶ 134
    let mut tx = super::begin_write(pool).await?;
42fa9554 Andy McCall ↶ 135
    let now = now_ms();
136
    let status = if r.error.is_some() { "error" } else { "queued" };
137
    let id: i64 = sqlx::query_scalar(
1edac4b6 Andy McCall ↶ 138
        "INSERT INTO flow_runs (repo_id, number, bookmark, state, heads, pipeline_hash, status, error, actor, trigger, created_ms, finished_ms) \
139
         VALUES (?, (SELECT COALESCE(MAX(number), 0) + 1 FROM flow_runs WHERE repo_id = ?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id",
42fa9554 Andy McCall ↶ 140
    )
141
    .bind(r.repo_id)
a0d4bbfb Andy McCall ↶ 142
    .bind(r.repo_id)
42fa9554 Andy McCall ↶ 143
    .bind(r.bookmark)
144
    .bind(r.state)
145
    .bind(r.heads)
146
    .bind(r.pipeline_hash)
147
    .bind(status)
148
    .bind(r.error.unwrap_or(""))
149
    .bind(r.actor)
1edac4b6 Andy McCall ↶ 150
    .bind(r.trigger)
42fa9554 Andy McCall ↶ 151
    .bind(now)
152
    .bind(r.error.map(|_| now))
153
    .fetch_one(&mut *tx)
154
    .await?;
155
    for j in jobs {
156
        sqlx::query(
157
            "INSERT INTO flow_jobs (run_id, name, depth, depends, spec, sandbox, runtime, status) VALUES (?, ?, ?, ?, ?, ?, ?, 'queued')",
158
        )
159
        .bind(id)
160
        .bind(&j.name)
161
        .bind(j.depth)
162
        .bind(serde_json::to_string(&j.depends).unwrap_or_else(|_| "[]".into()))
163
        .bind(&j.spec)
164
        .bind(&j.sandbox)
165
        .bind(&j.runtime)
166
        .execute(&mut *tx)
167
        .await?;
168
    }
169
    tx.commit().await?;
170
    Ok(id)
171
}
172
c4c87f48 Andy McCall ↶ 173
/// The labels a compiled job and its sandbox ask a runner for (`runs-on`).
174
fn labels_in(spec: &str, sandbox: &str) -> Vec<String> {
175
    let mut out: Vec<String> = [spec, sandbox]
176
        .iter()
177
        .filter_map(|j| serde_json::from_str::<serde_json::Value>(j).ok())
178
        .flat_map(|v| v["runs_on"].as_array().cloned().unwrap_or_default())
179
        .filter_map(|l| l.as_str().map(String::from))
180
        .collect();
181
    out.sort();
182
    out.dedup();
183
    out
184
}
185
186
impl Job {
187
    /// The runner labels this job needs.
188
    pub fn needs(&self) -> Vec<String> {
189
        labels_in(&self.spec, &self.sandbox)
190
    }
191
}
192
bf3d0754 Andy McCall ↶ 193
/// A job waiting for or held by a runner, with where it comes from.
194
#[derive(Clone, Debug, FromRow)]
195
pub struct QueueEntry {
196
    pub job_id: i64,
197
    pub run_id: i64,
198
    pub repo_id: i64,
199
    pub owner: String,
200
    pub name: String,
201
    pub visibility: String,
202
    pub run_number: i64,
203
    pub job: String,
204
    pub status: String,
b66e70b7 Andy McCall ↶ 205
    /// The runner holding it, when running.
206
    pub runner: String,
c4c87f48 Andy McCall ↶ 207
    pub spec: String,
208
    pub sandbox: String,
209
}
210
211
impl QueueEntry {
212
    /// The runner labels the job needs.
213
    pub fn needs(&self) -> Vec<String> {
214
        labels_in(&self.spec, &self.sandbox)
215
    }
bf3d0754 Andy McCall ↶ 216
}
217
218
/// Every job of a live run that is running or queued: running ones first,
219
/// then queued ones in the order runners claim them (hub spec §13.2).
220
pub async fn queue(pool: &Pool) -> sqlx::Result<Vec<QueueEntry>> {
221
    sqlx::query_as(
c4c87f48 Andy McCall ↶ 222
        "SELECT j.id AS job_id, j.run_id, r.repo_id, p.owner, p.name, p.visibility, r.number AS run_number, j.name AS job, j.status, j.runner, j.spec, j.sandbox \
bf3d0754 Andy McCall ↶ 223
         FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id JOIN repositories p ON p.id = r.repo_id \
224
         WHERE j.status IN ('queued', 'running') AND r.status IN ('queued', 'running') \
225
         ORDER BY CASE j.status WHEN 'running' THEN 0 ELSE 1 END, j.run_id, j.depth, j.id",
226
    )
227
    .fetch_all(pool)
228
    .await
229
}
230
90326c96 Andy McCall ↶ 231
/// A repository's runs, newest first, numbered below `before`.
232
pub async fn runs_by_number(pool: &Pool, repo_id: i64, limit: i64, before: Option<i64>) -> sqlx::Result<Vec<Run>> {
233
    sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND number < ? ORDER BY number DESC LIMIT ?")
234
        .bind(repo_id)
235
        .bind(before.unwrap_or(i64::MAX))
236
        .bind(limit)
237
        .fetch_all(pool)
238
        .await
239
}
240
d7ce41ac Andy McCall ↶ 241
/// The latest run on this bookmark of this repository, for what a push
242
/// changed since (a job's scope).
745d225d Andy McCall ↶ 243
/// The run a push to `bookmark` is scoped against: the newest that was not
244
/// cancelled, nor a push run still going (a new push supersedes it), so
245
/// that what a cancelled run would have checked is checked by the next.
d7ce41ac Andy McCall ↶ 246
pub async fn latest_on(pool: &Pool, repo_id: i64, bookmark: &str) -> sqlx::Result<Option<Run>> {
745d225d Andy McCall ↶ 247
    sqlx::query_as(
248
        "SELECT * FROM flow_runs WHERE repo_id = ? AND bookmark = ? AND heads != '' AND status != 'cancelled'
249
         AND NOT (trigger = 'push' AND status IN ('queued', 'running')) ORDER BY id DESC LIMIT 1",
250
    )
251
    .bind(repo_id)
252
    .bind(bookmark)
253
    .fetch_optional(pool)
254
    .await
d7ce41ac Andy McCall ↶ 255
}
256
42fa9554 Andy McCall ↶ 257
pub async fn runs(pool: &Pool, repo_id: i64, limit: i64, before: Option<i64>) -> sqlx::Result<Vec<Run>> {
258
    sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND id < ? ORDER BY id DESC LIMIT ?")
259
        .bind(repo_id)
260
        .bind(before.unwrap_or(i64::MAX))
261
        .bind(limit)
262
        .fetch_all(pool)
263
        .await
264
}
265
a0d4bbfb Andy McCall ↶ 266
/// A repository's run by its number there.
267
pub async fn run(pool: &Pool, repo_id: i64, number: i64) -> sqlx::Result<Option<Run>> {
268
    sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND number = ?").bind(repo_id).bind(number).fetch_optional(pool).await
42fa9554 Andy McCall ↶ 269
}
270
271
pub async fn run_by_id(pool: &Pool, id: i64) -> sqlx::Result<Option<Run>> {
272
    sqlx::query_as("SELECT * FROM flow_runs WHERE id = ?").bind(id).fetch_optional(pool).await
273
}
274
275
pub async fn jobs(pool: &Pool, run_id: i64) -> sqlx::Result<Vec<Job>> {
276
    sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = ? ORDER BY depth, name").bind(run_id).fetch_all(pool).await
277
}
278
279
pub async fn job(pool: &Pool, id: i64) -> sqlx::Result<Option<Job>> {
280
    sqlx::query_as("SELECT * FROM flow_jobs WHERE id = ?").bind(id).fetch_optional(pool).await
281
}
282
283
/// The running job a job token belongs to, while it may still be used.
284
pub async fn job_by_token(pool: &Pool, token_hash: &str, max_age_ms: i64) -> sqlx::Result<Option<Job>> {
285
    sqlx::query_as("SELECT * FROM flow_jobs WHERE token_hash = ? AND status = 'running' AND started_ms >= ?")
286
        .bind(token_hash)
287
        .bind(now_ms() - max_age_ms)
288
        .fetch_optional(pool)
289
        .await
290
}
291
292
/// Take the oldest queued job whose dependencies all passed and whose tier
293
/// the runner offers, and mark it running for `runner` with a token.
fd3db644 Andy McCall ↶ 294
/// Let a job's token also read these repositories (its pinned subrepos).
295
pub async fn set_also_read(pool: &Pool, job_id: i64, repo_ids: &[i64]) -> sqlx::Result<()> {
296
    let list: Vec<String> = repo_ids.iter().map(|i| i.to_string()).collect();
297
    sqlx::query("UPDATE flow_jobs SET also_read = ? WHERE id = ?").bind(list.join(",")).bind(job_id).execute(pool).await?;
298
    Ok(())
299
}
300
301
impl Job {
302
    pub fn also_read_ids(&self) -> Vec<i64> {
303
        self.also_read.split(',').filter_map(|x| x.trim().parse().ok()).collect()
304
    }
305
}
306
b0612e7d Andy McCall ↶ 307
/// What a runner can take on: free memory (MiB) and vCPUs, `None` for no
308
/// limit, and the rootfs images it holds, `None` when it does not say.
309
#[derive(Clone, Debug, Default)]
76bc5a05 Andy McCall ↶ 310
pub struct Room {
311
    pub memory_mib: Option<u32>,
312
    pub cpus: Option<u32>,
b0612e7d Andy McCall ↶ 313
    pub images: Option<std::collections::HashSet<String>>,
76bc5a05 Andy McCall ↶ 314
}
315
316
impl Room {
b0612e7d Andy McCall ↶ 317
    /// Whether a job's sandbox (its stored JSON) fits: its memory and vCPUs,
318
    /// and its rootfs image among the runner's (oxygenhub#73).
76bc5a05 Andy McCall ↶ 319
    pub fn fits(&self, sandbox_json: &str) -> bool {
320
        let v: serde_json::Value = serde_json::from_str(sandbox_json).unwrap_or_default();
321
        let need = |k: &str| v[k].as_u64().unwrap_or(0);
b0612e7d Andy McCall ↶ 322
        let rootfs = v["rootfs"].as_str().unwrap_or("");
323
        self.memory_mib.is_none_or(|m| need("memory_mib") <= m as u64)
324
            && self.cpus.is_none_or(|c| need("cpus") <= c as u64)
325
            && (rootfs.is_empty() || self.images.as_ref().is_none_or(|have| have.contains(rootfs)))
76bc5a05 Andy McCall ↶ 326
    }
327
}
328
42fa9554 Andy McCall ↶ 329
pub async fn claim(pool: &Pool, runtimes: &[String], runner: &str, token_hash: &str) -> sqlx::Result<Option<Job>> {
c4c87f48 Andy McCall ↶ 330
    claim_within(pool, runtimes, runner, token_hash, Room::default(), &Default::default(), &[]).await
76bc5a05 Andy McCall ↶ 331
}
332
333
/// The oldest runnable job that fits in `room`.
74be9796 Andy McCall ↶ 334
/// The first queued job a runner with these tiers, this room and this
335
/// scope may take, now running under `runner`. A scoped runner never sees
336
/// another scope's jobs (oxygenhub#41).
337
pub async fn claim_within(
338
    pool: &Pool,
339
    runtimes: &[String],
340
    runner: &str,
341
    token_hash: &str,
342
    room: Room,
343
    scope: &super::runners::Scope,
c4c87f48 Andy McCall ↶ 344
    labels: &[String],
74be9796 Andy McCall ↶ 345
) -> sqlx::Result<Option<Job>> {
941abdd6 Andy McCall ↶ 346
    let mut tx = super::begin_write(pool).await?;
42fa9554 Andy McCall ↶ 347
    let queued: Vec<Job> = sqlx::query_as(
74be9796 Andy McCall ↶ 348
        "SELECT j.* FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id JOIN repositories rep ON rep.id = r.repo_id \
349
         WHERE j.status = 'queued' AND r.status IN ('queued', 'running') \
350
         AND (? = '' OR rep.owner = ?) AND (? IS NULL OR r.repo_id = ?) \
351
         ORDER BY j.run_id, j.depth, j.id LIMIT 200",
42fa9554 Andy McCall ↶ 352
    )
74be9796 Andy McCall ↶ 353
    .bind(&scope.owner)
354
    .bind(&scope.owner)
355
    .bind(scope.repo_id)
356
    .bind(scope.repo_id)
42fa9554 Andy McCall ↶ 357
    .fetch_all(&mut *tx)
358
    .await?;
359
    for j in queued {
76bc5a05 Andy McCall ↶ 360
        if !runtimes.iter().any(|r| r == &j.runtime) || !room.fits(&j.sandbox) {
c4c87f48 Andy McCall ↶ 361
            continue;
362
        }
363
        // Every label the job and its sandbox ask for (`runs-on`).
364
        if !j.needs().iter().all(|l| labels.contains(l)) {
42fa9554 Andy McCall ↶ 365
            continue;
366
        }
367
        let deps = j.depends_on();
368
        if !deps.is_empty() {
369
            let mut passed = 0i64;
370
            for d in &deps {
371
                let st: Option<String> = sqlx::query_scalar("SELECT status FROM flow_jobs WHERE run_id = ? AND name = ?")
372
                    .bind(j.run_id)
373
                    .bind(d)
374
                    .fetch_optional(&mut *tx)
375
                    .await?;
376
                if st.as_deref() == Some("passed") {
377
                    passed += 1;
378
                }
379
            }
380
            if passed != deps.len() as i64 {
381
                continue;
382
            }
383
        }
384
        let now = now_ms();
385
        let done = sqlx::query(
386
            "UPDATE flow_jobs SET status = 'running', runner = ?, token_hash = ?, started_ms = ? WHERE id = ? AND status = 'queued'",
387
        )
388
        .bind(runner)
389
        .bind(token_hash)
390
        .bind(now)
391
        .bind(j.id)
392
        .execute(&mut *tx)
393
        .await?;
394
        if done.rows_affected() == 1 {
395
            sqlx::query("UPDATE flow_runs SET status = 'running' WHERE id = ? AND status = 'queued'")
396
                .bind(j.run_id)
397
                .execute(&mut *tx)
398
                .await?;
399
            tx.commit().await?;
400
            return job(pool, j.id).await;
401
        }
402
    }
403
    tx.commit().await?;
404
    Ok(None)
405
}
406
407
/// Append records; returns how many were kept (the rest exceed the cap).
408
pub async fn append_log(pool: &Pool, job_id: i64, records: &[flow_core::api::Record], cap: i64) -> sqlx::Result<usize> {
941abdd6 Andy McCall ↶ 409
    let mut tx = super::begin_write(pool).await?;
42fa9554 Andy McCall ↶ 410
    let count: i64 = sqlx::query_scalar("SELECT log_count FROM flow_jobs WHERE id = ?").bind(job_id).fetch_one(&mut *tx).await?;
411
    let room = (cap - count).max(0) as usize;
412
    let keep = &records[..records.len().min(room)];
413
    for (i, r) in keep.iter().enumerate() {
414
        sqlx::query("INSERT INTO flow_logs (job_id, seq, t, stream, text) VALUES (?, ?, ?, ?, ?)")
415
            .bind(job_id)
416
            .bind(count + i as i64 + 1)
417
            .bind(r.t as i64)
418
            .bind(&r.stream)
419
            .bind(&r.text)
420
            .execute(&mut *tx)
421
            .await?;
422
    }
423
    sqlx::query("UPDATE flow_jobs SET log_count = log_count + ? WHERE id = ?")
424
        .bind(keep.len() as i64)
425
        .bind(job_id)
426
        .execute(&mut *tx)
427
        .await?;
428
    tx.commit().await?;
429
    Ok(keep.len())
430
}
431
432
pub async fn log(pool: &Pool, job_id: i64, after: i64, limit: i64) -> sqlx::Result<Vec<LogLine>> {
433
    sqlx::query_as("SELECT seq, t, stream, text FROM flow_logs WHERE job_id = ? AND seq > ? ORDER BY seq LIMIT ?")
434
        .bind(job_id)
435
        .bind(after)
436
        .bind(limit)
437
        .fetch_all(pool)
438
        .await
439
}
440
070a7b64 Andy McCall ↶ 441
/// Jobs running since before `ms` (the oldest first).
5dead1f4 Andy McCall ↶ 442
/// Jobs still running at `now` past their own time limit (`default_s` for
443
/// one claimed without) plus `grace_ms`.
444
pub async fn running_past_limit(pool: &Pool, now: i64, default_s: u64, grace_ms: i64) -> sqlx::Result<Vec<Job>> {
445
    sqlx::query_as(
446
        "SELECT * FROM flow_jobs WHERE status = 'running' \
447
         AND COALESCE(started_ms, 0) + (CASE WHEN timeout_s > 0 THEN timeout_s ELSE ? END) * 1000 + ? < ? ORDER BY started_ms",
448
    )
449
    .bind(default_s as i64)
450
    .bind(grace_ms)
451
    .bind(now)
452
    .fetch_all(pool)
453
    .await
454
}
455
456
/// Record the limit a claimed job runs under.
457
pub async fn set_job_timeout(pool: &Pool, job_id: i64, timeout_s: u64) -> sqlx::Result<()> {
458
    sqlx::query("UPDATE flow_jobs SET timeout_s = ? WHERE id = ?").bind(timeout_s as i64).bind(job_id).execute(pool).await?;
459
    Ok(())
460
}
461
070a7b64 Andy McCall ↶ 462
pub async fn running_since_before(pool: &Pool, ms: i64) -> sqlx::Result<Vec<Job>> {
463
    sqlx::query_as("SELECT * FROM flow_jobs WHERE status = 'running' AND COALESCE(started_ms, 0) < ? ORDER BY started_ms")
464
        .bind(ms)
465
        .fetch_all(pool)
466
        .await
467
}
468
eef677c4 Andy McCall ↶ 469
/// The jobs running on the runner with this id.
470
pub async fn running_on(pool: &Pool, runner_id: i64) -> sqlx::Result<Vec<Job>> {
471
    sqlx::query_as("SELECT * FROM flow_jobs WHERE status = 'running' AND runner_id = ? ORDER BY id").bind(runner_id).fetch_all(pool).await
472
}
473
42fa9554 Andy McCall ↶ 474
pub struct Outcome<'a> {
475
    pub status: &'a str,
476
    pub exit_code: Option<i64>,
477
    pub error: &'a str,
478
    pub receipt: &'a str,
479
    pub receipt_hash: &'a str,
480
}
481
482
/// Record a job's end, skip the jobs that can no longer run, and settle the
483
/// run. Returns the jobs whose status changed (for their checks).
484
pub async fn finish(pool: &Pool, job_id: i64, o: Outcome<'_>) -> sqlx::Result<Vec<Job>> {
941abdd6 Andy McCall ↶ 485
    let mut tx = super::begin_write(pool).await?;
42fa9554 Andy McCall ↶ 486
    let now = now_ms();
487
    let j: Job = sqlx::query_as("SELECT * FROM flow_jobs WHERE id = ?").bind(job_id).fetch_one(&mut *tx).await?;
488
    sqlx::query("UPDATE flow_jobs SET status = ?, exit_code = ?, error = ?, receipt = ?, receipt_hash = ?, finished_ms = ?, token_hash = '' WHERE id = ?")
489
        .bind(o.status)
490
        .bind(o.exit_code)
491
        .bind(o.error)
492
        .bind(o.receipt)
493
        .bind(o.receipt_hash)
494
        .bind(now)
495
        .bind(job_id)
496
        .execute(&mut *tx)
497
        .await?;
498
    let mut changed = vec![job_id];
499
    // Dependents of anything that did not pass are skipped, transitively.
500
    loop {
501
        let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = ?").bind(j.run_id).fetch_all(&mut *tx).await?;
502
        let blocked: Vec<i64> = all
503
            .iter()
504
            .filter(|x| x.status == "queued")
505
            .filter(|x| {
506
                x.depends_on()
507
                    .iter()
508
                    .any(|d| all.iter().any(|y| &y.name == d && matches!(y.status.as_str(), "failed" | "skipped" | "error")))
509
            })
510
            .map(|x| x.id)
511
            .collect();
512
        if blocked.is_empty() {
513
            break;
514
        }
515
        for id in blocked {
516
            sqlx::query("UPDATE flow_jobs SET status = 'skipped', finished_ms = ? WHERE id = ?")
517
                .bind(now)
518
                .bind(id)
519
                .execute(&mut *tx)
520
                .await?;
521
            changed.push(id);
522
        }
523
    }
524
    let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = ?").bind(j.run_id).fetch_all(&mut *tx).await?;
525
    if all.iter().all(Job::done) {
526
        let status = if all.iter().all(|x| x.status == "passed") { "passed" } else { "failed" };
527
        sqlx::query("UPDATE flow_runs SET status = ?, finished_ms = ? WHERE id = ?")
528
            .bind(status)
529
            .bind(now)
530
            .bind(j.run_id)
531
            .execute(&mut *tx)
532
            .await?;
533
    }
534
    tx.commit().await?;
535
    let mut out = Vec::new();
536
    for id in changed {
745d225d Andy McCall ↶ 537
        if let Some(x) = job(pool, id).await? {
538
            out.push(x);
539
        }
540
    }
541
    Ok(out)
542
}
543
544
/// Cancel a run (oxygenflow#32): its jobs not yet done end as cancelled,
545
/// and so does the run. A runner learns of it when the hub next refuses
546
/// its log lines, and stops the job. Returns the jobs whose status changed.
547
pub async fn cancel_run(pool: &Pool, run_id: i64, why: &str) -> sqlx::Result<Vec<Job>> {
548
    let now = now_ms();
549
    let mut tx = pool.begin().await?;
550
    let ids: Vec<(i64,)> = sqlx::query_as("SELECT id FROM flow_jobs WHERE run_id = ? AND status IN ('queued', 'running')")
551
        .bind(run_id)
552
        .fetch_all(&mut *tx)
553
        .await?;
554
    sqlx::query(
555
        "UPDATE flow_jobs SET status = 'cancelled', error = ?, finished_ms = ?, token_hash = ''
556
         WHERE run_id = ? AND status IN ('queued', 'running')",
557
    )
558
    .bind(why)
559
    .bind(now)
560
    .bind(run_id)
561
    .execute(&mut *tx)
562
    .await?;
563
    sqlx::query("UPDATE flow_runs SET status = 'cancelled', finished_ms = ? WHERE id = ? AND status IN ('queued', 'running')")
564
        .bind(now)
565
        .bind(run_id)
566
        .execute(&mut *tx)
567
        .await?;
568
    tx.commit().await?;
569
    let mut out = Vec::new();
570
    for (id,) in ids {
42fa9554 Andy McCall ↶ 571
        if let Some(x) = job(pool, id).await? {
572
            out.push(x);
573
        }
574
    }
575
    Ok(out)
745d225d Andy McCall ↶ 576
}
577
578
/// The push runs of `bookmark` still going, older than run `number`.
579
pub async fn live_push_runs_before(pool: &Pool, repo_id: i64, bookmark: &str, number: i64) -> sqlx::Result<Vec<Run>> {
580
    sqlx::query_as(
581
        "SELECT * FROM flow_runs WHERE repo_id = ? AND bookmark = ? AND number < ? AND trigger = 'push'
582
         AND status IN ('queued', 'running') ORDER BY id",
583
    )
584
    .bind(repo_id)
585
    .bind(bookmark)
586
    .bind(number)
587
    .fetch_all(pool)
588
    .await
42fa9554 Andy McCall ↶ 589
}
736f6813 Andy McCall ↶ 590
591
/// The newest run of exactly this state (heads joined with `+`, sorted).
592
pub async fn latest_for_heads(pool: &Pool, repo_id: i64, heads: &str) -> sqlx::Result<Option<Run>> {
593
    sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND heads = ? ORDER BY id DESC LIMIT 1")
594
        .bind(repo_id)
595
        .bind(heads)
596
        .fetch_optional(pool)
597
        .await
cc19697f Andy McCall ↶ 598
}
599
600
/// Record what became of job `job_id`'s signed receipt.
601
pub async fn set_receipt(pool: &Pool, job_id: i64, attest: &str, signer: &str, note: &str) -> sqlx::Result<()> {
602
    sqlx::query("UPDATE flow_jobs SET receipt_attest = ?, receipt_signer = ?, receipt_note = ? WHERE id = ?")
603
        .bind(attest)
604
        .bind(signer)
605
        .bind(note)
606
        .bind(job_id)
607
        .execute(pool)
608
        .await?;
609
    Ok(())
736f6813 Andy McCall ↶ 610
}
a3c8288b Andy McCall ↶ 611
612
/// A job's artifact, kept as a blob of the run's repository.
613
#[derive(Clone, Debug, FromRow)]
614
pub struct Artifact {
615
    pub job_id: i64,
616
    pub name: String,
617
    pub path: String,
618
    pub blob: String,
619
    pub size: i64,
620
    /// BLAKE3 of the bytes.
621
    pub content: String,
622
    pub created_ms: i64,
623
}
624
625
pub async fn add_artifact(pool: &Pool, a: &Artifact) -> sqlx::Result<()> {
626
    sqlx::query("INSERT OR REPLACE INTO flow_artifacts (job_id, name, path, blob, size, content, created_ms) VALUES (?, ?, ?, ?, ?, ?, ?)")
627
        .bind(a.job_id)
628
        .bind(&a.name)
629
        .bind(&a.path)
630
        .bind(&a.blob)
631
        .bind(a.size)
632
        .bind(&a.content)
633
        .bind(a.created_ms)
634
        .execute(pool)
635
        .await?;
636
    Ok(())
637
}
638
639
/// Job `job_id`'s artifacts, by name.
640
pub async fn artifacts(pool: &Pool, job_id: i64) -> sqlx::Result<Vec<Artifact>> {
641
    sqlx::query_as("SELECT * FROM flow_artifacts WHERE job_id = ? ORDER BY name").bind(job_id).fetch_all(pool).await
642
}
10708ab8 Andy McCall ↶ 643
644
/// Where an artifact with given content came from: the run and job that
645
/// built it, and the job's signed receipt when one was kept.
646
#[derive(Clone, Debug, FromRow)]
647
pub struct Provenance {
648
    pub run_number: i64,
649
    pub job: String,
650
    pub name: String,
651
    pub receipt_attest: String,
652
    pub receipt_signer: String,
653
}
654
655
/// The build of the bytes whose BLAKE3 is `content` in repository
656
/// `repo_id`: a passed job, one with a kept signed receipt first, newest
657
/// run first.
658
pub async fn provenance(pool: &Pool, repo_id: i64, content: &str) -> sqlx::Result<Option<Provenance>> {
659
    sqlx::query_as(
660
        "SELECT r.number AS run_number, j.name AS job, a.name AS name, j.receipt_attest, j.receipt_signer \
661
         FROM flow_artifacts a JOIN flow_jobs j ON j.id = a.job_id JOIN flow_runs r ON r.id = j.run_id \
662
         WHERE r.repo_id = ? AND a.content = ? AND j.status = 'passed' \
663
         ORDER BY (j.receipt_attest != '') DESC, r.number DESC LIMIT 1",
664
    )
665
    .bind(repo_id)
666
    .bind(content)
667
    .fetch_optional(pool)
668
    .await
669
}