OXYGENHUB
# 56a786aa42f1 oxygenhub / crates / hub / src / db / flow.rs
11.0 KiB · Rust · last changed OxygenFlow claims list the state's pinned Oxygen subrepos that the pusher can read, and the job token may read those repositories too 1 month ago source blame history raw
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,
10
    pub bookmark: String,
11
    pub state: String,
12
    pub heads: String,
13
    pub pipeline_hash: String,
14
    pub status: String,
15
    pub error: String,
16
    pub actor: String,
17
    pub created_ms: i64,
18
    pub finished_ms: Option<i64>,
19
}
20
21
impl Run {
22
    pub fn head_list(&self) -> Vec<&str> {
23
        self.heads.split('+').filter(|h| !h.is_empty()).collect()
24
    }
25
26
    pub fn live(&self) -> bool {
27
        matches!(self.status.as_str(), "queued" | "running")
28
    }
29
}
30
31
#[derive(Clone, Debug, FromRow)]
32
pub struct Job {
33
    pub id: i64,
34
    pub run_id: i64,
35
    pub name: String,
36
    pub depth: i64,
37
    pub depends: String,
38
    pub spec: String,
39
    pub sandbox: String,
40
    pub runtime: String,
41
    pub status: String,
42
    pub runner: String,
43
    pub token_hash: String,
44
    pub started_ms: Option<i64>,
45
    pub finished_ms: Option<i64>,
46
    pub exit_code: Option<i64>,
47
    pub error: String,
48
    pub receipt: String,
49
    pub receipt_hash: String,
50
    pub log_count: i64,
51
    /// Comma-separated ids of other repositories the job token may read.
52
    pub also_read: String,
53
}
54
55
impl Job {
56
    pub fn depends_on(&self) -> Vec<String> {
57
        serde_json::from_str(&self.depends).unwrap_or_default()
58
    }
59
60
    pub fn done(&self) -> bool {
61
        matches!(self.status.as_str(), "passed" | "failed" | "skipped" | "error")
62
    }
63
}
64
65
#[derive(Clone, Debug, FromRow)]
66
pub struct LogLine {
67
    pub seq: i64,
68
    pub t: i64,
69
    pub stream: String,
70
    pub text: String,
71
}
72
73
/// A job as the trigger creates it.
74
pub struct NewJob {
75
    pub name: String,
76
    pub depth: i64,
77
    pub depends: Vec<String>,
78
    pub spec: String,
79
    pub sandbox: String,
80
    pub runtime: String,
81
}
82
83
pub struct NewRun<'a> {
84
    pub repo_id: i64,
85
    pub bookmark: &'a str,
86
    pub state: &'a str,
87
    pub heads: &'a str,
88
    pub pipeline_hash: &'a str,
89
    pub actor: &'a str,
90
    /// Problems when the pipeline is invalid: the run is recorded in error.
91
    pub error: Option<&'a str>,
92
}
93
94
pub async fn create_run(pool: &Pool, r: NewRun<'_>, jobs: &[NewJob]) -> sqlx::Result<i64> {
95
    let mut tx = pool.begin().await?;
96
    let now = now_ms();
97
    let status = if r.error.is_some() { "error" } else { "queued" };
98
    let id: i64 = sqlx::query_scalar(
99
        "INSERT INTO flow_runs (repo_id, bookmark, state, heads, pipeline_hash, status, error, actor, created_ms, finished_ms) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id",
100
    )
101
    .bind(r.repo_id)
102
    .bind(r.bookmark)
103
    .bind(r.state)
104
    .bind(r.heads)
105
    .bind(r.pipeline_hash)
106
    .bind(status)
107
    .bind(r.error.unwrap_or(""))
108
    .bind(r.actor)
109
    .bind(now)
110
    .bind(r.error.map(|_| now))
111
    .fetch_one(&mut *tx)
112
    .await?;
113
    for j in jobs {
114
        sqlx::query(
115
            "INSERT INTO flow_jobs (run_id, name, depth, depends, spec, sandbox, runtime, status) VALUES (?, ?, ?, ?, ?, ?, ?, 'queued')",
116
        )
117
        .bind(id)
118
        .bind(&j.name)
119
        .bind(j.depth)
120
        .bind(serde_json::to_string(&j.depends).unwrap_or_else(|_| "[]".into()))
121
        .bind(&j.spec)
122
        .bind(&j.sandbox)
123
        .bind(&j.runtime)
124
        .execute(&mut *tx)
125
        .await?;
126
    }
127
    tx.commit().await?;
128
    Ok(id)
129
}
130
131
pub async fn runs(pool: &Pool, repo_id: i64, limit: i64, before: Option<i64>) -> sqlx::Result<Vec<Run>> {
132
    sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND id < ? ORDER BY id DESC LIMIT ?")
133
        .bind(repo_id)
134
        .bind(before.unwrap_or(i64::MAX))
135
        .bind(limit)
136
        .fetch_all(pool)
137
        .await
138
}
139
140
pub async fn run(pool: &Pool, repo_id: i64, id: i64) -> sqlx::Result<Option<Run>> {
141
    sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND id = ?").bind(repo_id).bind(id).fetch_optional(pool).await
142
}
143
144
pub async fn run_by_id(pool: &Pool, id: i64) -> sqlx::Result<Option<Run>> {
145
    sqlx::query_as("SELECT * FROM flow_runs WHERE id = ?").bind(id).fetch_optional(pool).await
146
}
147
148
pub async fn jobs(pool: &Pool, run_id: i64) -> sqlx::Result<Vec<Job>> {
149
    sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = ? ORDER BY depth, name").bind(run_id).fetch_all(pool).await
150
}
151
152
pub async fn job(pool: &Pool, id: i64) -> sqlx::Result<Option<Job>> {
153
    sqlx::query_as("SELECT * FROM flow_jobs WHERE id = ?").bind(id).fetch_optional(pool).await
154
}
155
156
/// The running job a job token belongs to, while it may still be used.
157
pub async fn job_by_token(pool: &Pool, token_hash: &str, max_age_ms: i64) -> sqlx::Result<Option<Job>> {
158
    sqlx::query_as("SELECT * FROM flow_jobs WHERE token_hash = ? AND status = 'running' AND started_ms >= ?")
159
        .bind(token_hash)
160
        .bind(now_ms() - max_age_ms)
161
        .fetch_optional(pool)
162
        .await
163
}
164
165
/// Take the oldest queued job whose dependencies all passed and whose tier
166
/// the runner offers, and mark it running for `runner` with a token.
167
/// Let a job's token also read these repositories (its pinned subrepos).
168
pub async fn set_also_read(pool: &Pool, job_id: i64, repo_ids: &[i64]) -> sqlx::Result<()> {
169
    let list: Vec<String> = repo_ids.iter().map(|i| i.to_string()).collect();
170
    sqlx::query("UPDATE flow_jobs SET also_read = ? WHERE id = ?").bind(list.join(",")).bind(job_id).execute(pool).await?;
171
    Ok(())
172
}
173
174
impl Job {
175
    pub fn also_read_ids(&self) -> Vec<i64> {
176
        self.also_read.split(',').filter_map(|x| x.trim().parse().ok()).collect()
177
    }
178
}
179
180
pub async fn claim(pool: &Pool, runtimes: &[String], runner: &str, token_hash: &str) -> sqlx::Result<Option<Job>> {
181
    let mut tx = pool.begin().await?;
182
    let queued: Vec<Job> = sqlx::query_as(
183
        "SELECT j.* FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id WHERE j.status = 'queued' AND r.status IN ('queued', 'running') ORDER BY j.run_id, j.depth, j.id LIMIT 200",
184
    )
185
    .fetch_all(&mut *tx)
186
    .await?;
187
    for j in queued {
188
        if !runtimes.iter().any(|r| r == &j.runtime) {
189
            continue;
190
        }
191
        let deps = j.depends_on();
192
        if !deps.is_empty() {
193
            let mut passed = 0i64;
194
            for d in &deps {
195
                let st: Option<String> = sqlx::query_scalar("SELECT status FROM flow_jobs WHERE run_id = ? AND name = ?")
196
                    .bind(j.run_id)
197
                    .bind(d)
198
                    .fetch_optional(&mut *tx)
199
                    .await?;
200
                if st.as_deref() == Some("passed") {
201
                    passed += 1;
202
                }
203
            }
204
            if passed != deps.len() as i64 {
205
                continue;
206
            }
207
        }
208
        let now = now_ms();
209
        let done = sqlx::query(
210
            "UPDATE flow_jobs SET status = 'running', runner = ?, token_hash = ?, started_ms = ? WHERE id = ? AND status = 'queued'",
211
        )
212
        .bind(runner)
213
        .bind(token_hash)
214
        .bind(now)
215
        .bind(j.id)
216
        .execute(&mut *tx)
217
        .await?;
218
        if done.rows_affected() == 1 {
219
            sqlx::query("UPDATE flow_runs SET status = 'running' WHERE id = ? AND status = 'queued'")
220
                .bind(j.run_id)
221
                .execute(&mut *tx)
222
                .await?;
223
            tx.commit().await?;
224
            return job(pool, j.id).await;
225
        }
226
    }
227
    tx.commit().await?;
228
    Ok(None)
229
}
230
231
/// Append records; returns how many were kept (the rest exceed the cap).
232
pub async fn append_log(pool: &Pool, job_id: i64, records: &[flow_core::api::Record], cap: i64) -> sqlx::Result<usize> {
233
    let mut tx = pool.begin().await?;
234
    let count: i64 = sqlx::query_scalar("SELECT log_count FROM flow_jobs WHERE id = ?").bind(job_id).fetch_one(&mut *tx).await?;
235
    let room = (cap - count).max(0) as usize;
236
    let keep = &records[..records.len().min(room)];
237
    for (i, r) in keep.iter().enumerate() {
238
        sqlx::query("INSERT INTO flow_logs (job_id, seq, t, stream, text) VALUES (?, ?, ?, ?, ?)")
239
            .bind(job_id)
240
            .bind(count + i as i64 + 1)
241
            .bind(r.t as i64)
242
            .bind(&r.stream)
243
            .bind(&r.text)
244
            .execute(&mut *tx)
245
            .await?;
246
    }
247
    sqlx::query("UPDATE flow_jobs SET log_count = log_count + ? WHERE id = ?")
248
        .bind(keep.len() as i64)
249
        .bind(job_id)
250
        .execute(&mut *tx)
251
        .await?;
252
    tx.commit().await?;
253
    Ok(keep.len())
254
}
255
256
pub async fn log(pool: &Pool, job_id: i64, after: i64, limit: i64) -> sqlx::Result<Vec<LogLine>> {
257
    sqlx::query_as("SELECT seq, t, stream, text FROM flow_logs WHERE job_id = ? AND seq > ? ORDER BY seq LIMIT ?")
258
        .bind(job_id)
259
        .bind(after)
260
        .bind(limit)
261
        .fetch_all(pool)
262
        .await
263
}
264
265
pub struct Outcome<'a> {
266
    pub status: &'a str,
267
    pub exit_code: Option<i64>,
268
    pub error: &'a str,
269
    pub receipt: &'a str,
270
    pub receipt_hash: &'a str,
271
}
272
273
/// Record a job's end, skip the jobs that can no longer run, and settle the
274
/// run. Returns the jobs whose status changed (for their checks).
275
pub async fn finish(pool: &Pool, job_id: i64, o: Outcome<'_>) -> sqlx::Result<Vec<Job>> {
276
    let mut tx = pool.begin().await?;
277
    let now = now_ms();
278
    let j: Job = sqlx::query_as("SELECT * FROM flow_jobs WHERE id = ?").bind(job_id).fetch_one(&mut *tx).await?;
279
    sqlx::query("UPDATE flow_jobs SET status = ?, exit_code = ?, error = ?, receipt = ?, receipt_hash = ?, finished_ms = ?, token_hash = '' WHERE id = ?")
280
        .bind(o.status)
281
        .bind(o.exit_code)
282
        .bind(o.error)
283
        .bind(o.receipt)
284
        .bind(o.receipt_hash)
285
        .bind(now)
286
        .bind(job_id)
287
        .execute(&mut *tx)
288
        .await?;
289
    let mut changed = vec![job_id];
290
    // Dependents of anything that did not pass are skipped, transitively.
291
    loop {
292
        let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = ?").bind(j.run_id).fetch_all(&mut *tx).await?;
293
        let blocked: Vec<i64> = all
294
            .iter()
295
            .filter(|x| x.status == "queued")
296
            .filter(|x| {
297
                x.depends_on()
298
                    .iter()
299
                    .any(|d| all.iter().any(|y| &y.name == d && matches!(y.status.as_str(), "failed" | "skipped" | "error")))
300
            })
301
            .map(|x| x.id)
302
            .collect();
303
        if blocked.is_empty() {
304
            break;
305
        }
306
        for id in blocked {
307
            sqlx::query("UPDATE flow_jobs SET status = 'skipped', finished_ms = ? WHERE id = ?")
308
                .bind(now)
309
                .bind(id)
310
                .execute(&mut *tx)
311
                .await?;
312
            changed.push(id);
313
        }
314
    }
315
    let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = ?").bind(j.run_id).fetch_all(&mut *tx).await?;
316
    if all.iter().all(Job::done) {
317
        let status = if all.iter().all(|x| x.status == "passed") { "passed" } else { "failed" };
318
        sqlx::query("UPDATE flow_runs SET status = ?, finished_ms = ? WHERE id = ?")
319
            .bind(status)
320
            .bind(now)
321
            .bind(j.run_id)
322
            .execute(&mut *tx)
323
            .await?;
324
    }
325
    tx.commit().await?;
326
    let mut out = Vec::new();
327
    for id in changed {
328
        if let Some(x) = job(pool, id).await? {
329
            out.push(x);
330
        }
331
    }
332
    Ok(out)
333
}