OXYGENHUB
◆ v0.5.0 oxygenhub / crates / hub / src / db / runners.rs
15.7 KiB · Rust · last changed Alerts: a failed run tells its pusher and watchers; an erroring runner is drained 21 days ago source blame history raw
1
//! OxygenFlow runners (hub spec §13.2): who may claim jobs, what they are,
2
//! whether they are taking work, and what they have run.
3
4
use super::{now_ms, Pool};
5
use sqlx::FromRow;
6
7
#[derive(Clone, Debug, Default, FromRow)]
8
pub struct Runner {
9
    pub id: i64,
10
    pub name: String,
11
    pub token_hash: String,
12
    pub source: String,
13
    pub key: String,
14
    pub key_name: String,
15
    pub arch: String,
16
    pub os: String,
17
    pub version: String,
18
    pub labels: String,
19
    pub max_jobs: i64,
20
    pub memory_mib: Option<i64>,
21
    pub cpus: Option<i64>,
22
    pub state: String,
23
    pub created_ms: i64,
24
    pub created_by: String,
25
    pub last_seen_ms: Option<i64>,
26
    /// The owner whose repositories it serves; empty for the whole site.
27
    pub scope_owner: String,
28
    /// The one repository it serves, when it serves one.
29
    pub scope_repo_id: Option<i64>,
30
    /// Labels an administrator gave it, beside those it reports (#98).
31
    pub admin_labels: String,
32
    /// A time limit for its jobs in minutes, below the site's, when set.
33
    pub timeout_minutes: Option<i64>,
34
    /// It fetches images it lacks from the hub (oxygenflow#39).
35
    pub fetches_images: bool,
36
    /// What it measured at its last claim (oxygenflow#40): free disk (MiB),
37
    /// the 1-minute load ×100, its speed score and class, its kernel.
38
    pub free_disk_mib: Option<i64>,
39
    pub load_x100: Option<i64>,
40
    pub speed: Option<i64>,
41
    pub speed_class: Option<i64>,
42
    pub kernel: String,
43
    /// The tiers it offered at its last claim, comma-separated.
44
    pub tiers: String,
45
    /// The images it held then (a JSON array); empty when it did not say.
46
    pub images: String,
47
    /// BLAKE3 of what it offers; its declines hold until this changes.
48
    pub profile: String,
49
}
50
51
/// Which repositories' jobs a runner may take (oxygenhub#41).
52
#[derive(Clone, Debug, Default, PartialEq, Eq)]
53
pub struct Scope {
54
    /// Empty: any owner.
55
    pub owner: String,
56
    /// `None`: any of the owner's repositories.
57
    pub repo_id: Option<i64>,
58
}
59
60
impl Scope {
61
    pub fn is_site(&self) -> bool {
62
        self.owner.is_empty() && self.repo_id.is_none()
63
    }
64
}
65
66
/// A runner that has not polled for this long is shown offline.
67
pub const ONLINE_MS: i64 = 90_000;
68
69
impl Runner {
70
    pub fn online(&self) -> bool {
71
        self.last_seen_ms.is_some_and(|t| now_ms() - t < ONLINE_MS)
72
    }
73
74
    /// `online`, `offline`, `draining` or `disabled`.
75
    pub fn status(&self) -> &'static str {
76
        match self.state.as_str() {
77
            "disabled" => "disabled",
78
            "draining" => "draining",
79
            _ if self.online() => "online",
80
            _ => "offline",
81
        }
82
    }
83
84
    /// Every label it carries: what it reports, and what an administrator
85
    /// gave it.
86
    pub fn label_list(&self) -> Vec<&str> {
87
        let mut out: Vec<&str> = self.labels.split(',').filter(|l| !l.is_empty()).collect();
88
        for l in self.admin_label_list() {
89
            if !out.contains(&l) {
90
                out.push(l);
91
            }
92
        }
93
        out
94
    }
95
96
    pub fn admin_label_list(&self) -> Vec<&str> {
97
        self.admin_labels.split(',').filter(|l| !l.is_empty()).collect()
98
    }
99
100
    /// The labels it reported itself, as the admin page distinguishes them.
101
    pub fn reported_label_list(&self) -> Vec<&str> {
102
        self.labels.split(',').filter(|l| !l.is_empty()).collect()
103
    }
104
105
    pub fn scope(&self) -> Scope {
106
        Scope { owner: self.scope_owner.clone(), repo_id: self.scope_repo_id }
107
    }
108
}
109
110
pub async fn by_token(pool: &Pool, token_hash: &str) -> sqlx::Result<Option<Runner>> {
111
    sqlx::query_as("SELECT * FROM flow_runners WHERE token_hash = ?").bind(token_hash).fetch_optional(pool).await
112
}
113
114
pub async fn get(pool: &Pool, id: i64) -> sqlx::Result<Option<Runner>> {
115
    sqlx::query_as("SELECT * FROM flow_runners WHERE id = ?").bind(id).fetch_optional(pool).await
116
}
117
118
pub async fn list(pool: &Pool) -> sqlx::Result<Vec<Runner>> {
119
    sqlx::query_as("SELECT * FROM flow_runners ORDER BY name, id").fetch_all(pool).await
120
}
121
122
/// The row of a runner configured with a hub.toml token, made on first sight.
123
pub async fn configured(pool: &Pool, token_hash: &str, name: &str) -> sqlx::Result<Runner> {
124
    sqlx::query("INSERT OR IGNORE INTO flow_runners (name, token_hash, source, created_ms) VALUES (?, ?, 'config', ?)")
125
        .bind(name)
126
        .bind(token_hash)
127
        .bind(now_ms())
128
        .execute(pool)
129
        .await?;
130
    Ok(by_token(pool, token_hash).await?.expect("just made"))
131
}
132
133
/// What a claim says about the runner, and that it was seen now. A
134
/// configured runner takes the name it claims with.
135
/// The administrator's settings for a runner (#98).
136
pub async fn set_limits(pool: &Pool, id: i64, admin_labels: &str, timeout_minutes: Option<i64>) -> sqlx::Result<()> {
137
    sqlx::query("UPDATE flow_runners SET admin_labels = ?, timeout_minutes = ? WHERE id = ?")
138
        .bind(admin_labels)
139
        .bind(timeout_minutes)
140
        .bind(id)
141
        .execute(pool)
142
        .await?;
143
    Ok(())
144
}
145
146
pub async fn seen(pool: &Pool, id: i64, name: Option<&str>, info: Option<&flow_core::api::RunnerInfo>) -> sqlx::Result<()> {
147
    let now = now_ms();
148
    match info {
149
        Some(i) => {
150
            sqlx::query(
151
                "UPDATE flow_runners SET arch = ?, os = ?, version = ?, labels = ?, max_jobs = ?, memory_mib = ?, cpus = ?, last_seen_ms = ? WHERE id = ?",
152
            )
153
            .bind(&i.arch)
154
            .bind(&i.os)
155
            .bind(&i.version)
156
            .bind(i.labels.join(","))
157
            .bind(i.max_jobs as i64)
158
            .bind(i.memory_mib.map(i64::from))
159
            .bind(i.cpus.map(i64::from))
160
            .bind(now)
161
            .bind(id)
162
            .execute(pool)
163
            .await?;
164
        }
165
        None => {
166
            sqlx::query("UPDATE flow_runners SET last_seen_ms = ? WHERE id = ?").bind(now).bind(id).execute(pool).await?;
167
        }
168
    }
169
    if let Some(n) = name {
170
        sqlx::query("UPDATE flow_runners SET name = ? WHERE id = ? AND source = 'config'").bind(n).bind(id).execute(pool).await?;
171
    }
172
    Ok(())
173
}
174
175
/// Keep what a runner measured about itself at a claim (oxygenflow#40).
176
pub async fn set_facts(pool: &Pool, id: i64, f: &flow_core::api::Facts) -> sqlx::Result<()> {
177
    sqlx::query("UPDATE flow_runners SET free_disk_mib = ?, load_x100 = ?, speed = ?, speed_class = ?, kernel = ? WHERE id = ?")
178
        .bind(f.free_disk_mib.map(|v| v.min(i64::MAX as u64) as i64))
179
        .bind(f.load_x100.map(i64::from))
180
        .bind(f.speed.map(i64::from))
181
        .bind(f.speed_class.map(i64::from))
182
        .bind(f.kernel.clone().unwrap_or_default())
183
        .bind(id)
184
        .execute(pool)
185
        .await?;
186
    Ok(())
187
}
188
189
impl Runner {
190
    /// Its facts as words for the Runners page: "speed 100 (class 3) · load
191
    /// 1.12 · 41 GiB free"; empty when it has sent none.
192
    pub fn facts_line(&self) -> String {
193
        let mut out = Vec::new();
194
        if let Some(s) = self.speed {
195
            out.push(format!("speed {s} (class {})", self.speed_class.unwrap_or(0)));
196
        }
197
        if let Some(l) = self.load_x100 {
198
            out.push(format!("load {:.2}", l as f64 / 100.0));
199
        }
200
        if let Some(d) = self.free_disk_mib {
201
            out.push(if d >= 1024 { format!("{} GiB free", d / 1024) } else { format!("{d} MiB free") });
202
        }
203
        out.join(" · ")
204
    }
205
}
206
207
/// What a runner offers at a claim (OxygenFlow spec §11.1): its tiers and
208
/// images, and the profile hash over those, its architecture, labels and
209
/// capacity.
210
pub async fn set_offer(
211
    pool: &Pool,
212
    id: i64,
213
    tiers: &[String],
214
    images: Option<&[String]>,
215
    fetches_images: bool,
216
    profile: &str,
217
) -> sqlx::Result<()> {
218
    let images = images.map(|i| serde_json::to_string(i).unwrap_or_default()).unwrap_or_default();
219
    sqlx::query("UPDATE flow_runners SET tiers = ?, images = ?, fetches_images = ?, profile = ? WHERE id = ?")
220
        .bind(tiers.join(","))
221
        .bind(images)
222
        .bind(fetches_images)
223
        .bind(profile)
224
        .bind(id)
225
        .execute(pool)
226
        .await?;
227
    Ok(())
228
}
229
230
impl Runner {
231
    pub fn tier_list(&self) -> Vec<&str> {
232
        self.tiers.split(',').filter(|t| !t.is_empty()).collect()
233
    }
234
235
    /// The images it holds, `None` when it does not say.
236
    pub fn image_list(&self) -> Option<Vec<String>> {
237
        if self.images.is_empty() {
238
            None
239
        } else {
240
            serde_json::from_str(&self.images).ok()
241
        }
242
    }
243
}
244
245
pub async fn set_state(pool: &Pool, id: i64, state: &str) -> sqlx::Result<()> {
246
    sqlx::query("UPDATE flow_runners SET state = ?, state_ms = ? WHERE id = ?")
247
        .bind(state)
248
        .bind(super::now_ms())
249
        .bind(id)
250
        .execute(pool)
251
        .await?;
252
    Ok(())
253
}
254
255
/// Whether runner `id`'s last `n` jobs since its state last changed all
256
/// ended in an infrastructure error (oxygenflow#30), with the last one's.
257
pub async fn erroring(pool: &Pool, id: i64, n: u32) -> sqlx::Result<Option<String>> {
258
    if n == 0 {
259
        return Ok(None);
260
    }
261
    let last: Vec<(String, String)> = sqlx::query_as(
262
        "SELECT j.status, j.error FROM flow_jobs j JOIN flow_runners r ON r.id = j.runner_id
263
         WHERE j.runner_id = ? AND j.finished_ms IS NOT NULL AND j.finished_ms >= r.state_ms
264
         ORDER BY j.finished_ms DESC, j.id DESC LIMIT ?",
265
    )
266
    .bind(id)
267
    .bind(n as i64)
268
    .fetch_all(pool)
269
    .await?;
270
    Ok((last.len() == n as usize && last.iter().all(|(s, _)| s == "error")).then(|| last[0].1.clone()))
271
}
272
273
/// Forget a runner: its credential stops working. Its jobs keep its name.
274
pub async fn remove(pool: &Pool, id: i64) -> sqlx::Result<()> {
275
    sqlx::query("DELETE FROM flow_runners WHERE id = ?").bind(id).execute(pool).await?;
276
    Ok(())
277
}
278
279
/// The key a runner signs receipts with: the one bound, or `key` (named
280
/// `name`) when none is yet.
281
pub async fn bind_key(pool: &Pool, id: i64, key: &str, name: &str) -> sqlx::Result<String> {
282
    sqlx::query("UPDATE flow_runners SET key = ?, key_name = ? WHERE id = ? AND key = ''")
283
        .bind(key)
284
        .bind(name)
285
        .bind(id)
286
        .execute(pool)
287
        .await?;
288
    sqlx::query_scalar("SELECT key FROM flow_runners WHERE id = ?").bind(id).fetch_one(pool).await
289
}
290
291
/// Keys that runners of this hub sign with.
292
pub async fn keys(pool: &Pool) -> sqlx::Result<Vec<String>> {
293
    sqlx::query_scalar("SELECT key FROM flow_runners WHERE key != ''").fetch_all(pool).await
294
}
295
296
/// A one-time registration token, valid for a day, for a runner of `scope`.
297
pub async fn add_registration(pool: &Pool, token_hash: &str, by: &str, scope: &Scope) -> sqlx::Result<()> {
298
    let now = now_ms();
299
    sqlx::query(
300
        "INSERT INTO flow_runner_registrations (token_hash, created_by, created_ms, expires_ms, scope_owner, scope_repo_id) VALUES (?, ?, ?, ?, ?, ?)",
301
    )
302
    .bind(token_hash)
303
    .bind(by)
304
    .bind(now)
305
    .bind(now + 24 * 3_600_000)
306
    .bind(&scope.owner)
307
    .bind(scope.repo_id)
308
    .execute(pool)
309
    .await?;
310
    Ok(())
311
}
312
313
/// The runners that may take a repository's jobs: the site's, its owner's
314
/// and its own (oxygenhub#41).
315
pub async fn serving(pool: &Pool, owner: &str, repo_id: i64) -> sqlx::Result<Vec<Runner>> {
316
    sqlx::query_as(
317
        "SELECT * FROM flow_runners WHERE (scope_owner = '' AND scope_repo_id IS NULL) \
318
         OR (scope_owner = ? AND scope_repo_id IS NULL) OR scope_repo_id = ? ORDER BY name",
319
    )
320
    .bind(owner)
321
    .bind(repo_id)
322
    .fetch_all(pool)
323
    .await
324
}
325
326
/// The runners that serve an owner: the site's, and those scoped to the
327
/// owner or one of its repositories.
328
pub async fn serving_owner(pool: &Pool, owner: &str) -> sqlx::Result<Vec<Runner>> {
329
    sqlx::query_as("SELECT * FROM flow_runners WHERE (scope_owner = '' AND scope_repo_id IS NULL) OR scope_owner = ? ORDER BY name")
330
        .bind(owner)
331
        .fetch_all(pool)
332
        .await
333
}
334
335
/// Change the repositories a runner serves.
336
pub async fn set_scope(pool: &Pool, id: i64, scope: &Scope) -> sqlx::Result<()> {
337
    sqlx::query("UPDATE flow_runners SET scope_owner = ?, scope_repo_id = ? WHERE id = ?")
338
        .bind(&scope.owner)
339
        .bind(scope.repo_id)
340
        .bind(id)
341
        .execute(pool)
342
        .await?;
343
    Ok(())
344
}
345
346
/// Registration tokens not used yet and not expired: (created by, created, expires).
347
pub async fn pending_registrations(pool: &Pool) -> sqlx::Result<Vec<(String, i64, i64)>> {
348
    sqlx::query_as("SELECT created_by, created_ms, expires_ms FROM flow_runner_registrations WHERE used_ms IS NULL AND expires_ms > ? ORDER BY created_ms DESC")
349
        .bind(now_ms())
350
        .fetch_all(pool)
351
        .await
352
}
353
354
/// Use registration token `token_hash` to register a runner: `None` when
355
/// the token is unknown, used or expired.
356
#[allow(clippy::too_many_arguments)]
357
pub async fn register(
358
    pool: &Pool,
359
    registration: &str,
360
    name: &str,
361
    token_hash: &str,
362
    key: &str,
363
    key_name: &str,
364
    by_default: &str,
365
) -> sqlx::Result<Option<Runner>> {
366
    let mut tx = super::begin_write(pool).await?;
367
    let now = now_ms();
368
    let found: Option<(String, String, Option<i64>)> = sqlx::query_as(
369
        "SELECT created_by, scope_owner, scope_repo_id FROM flow_runner_registrations WHERE token_hash = ? AND used_ms IS NULL AND expires_ms > ?",
370
    )
371
    .bind(registration)
372
    .bind(now)
373
    .fetch_optional(&mut *tx)
374
    .await?;
375
    let Some((who, scope_owner, scope_repo_id)) = found else { return Ok(None) };
376
    let id: i64 = sqlx::query_scalar(
377
        "INSERT INTO flow_runners (name, token_hash, source, key, key_name, created_ms, created_by, scope_owner, scope_repo_id) VALUES (?, ?, 'registered', ?, ?, ?, ?, ?, ?) RETURNING id",
378
    )
379
    .bind(name)
380
    .bind(token_hash)
381
    .bind(key)
382
    .bind(key_name)
383
    .bind(now)
384
    .bind(if who.is_empty() { by_default } else { &who })
385
    .bind(scope_owner)
386
    .bind(scope_repo_id)
387
    .fetch_one(&mut *tx)
388
    .await?;
389
    sqlx::query("UPDATE flow_runner_registrations SET used_ms = ?, runner_id = ? WHERE token_hash = ?")
390
        .bind(now)
391
        .bind(id)
392
        .bind(registration)
393
        .execute(&mut *tx)
394
        .await?;
395
    tx.commit().await?;
396
    get(pool, id).await
397
}
398
399
/// A job a runner ran, for its history.
400
#[derive(Clone, Debug, FromRow)]
401
pub struct RunnerJob {
402
    pub owner: String,
403
    pub repo: String,
404
    pub run_number: i64,
405
    pub job: String,
406
    pub status: String,
407
    pub started_ms: Option<i64>,
408
    pub finished_ms: Option<i64>,
409
    pub receipt_attest: String,
410
}
411
412
/// Jobs runner `id` ran or is running, newest first.
413
pub async fn history(pool: &Pool, id: i64, limit: i64, before: Option<i64>) -> sqlx::Result<Vec<RunnerJob>> {
414
    sqlx::query_as(
415
        "SELECT p.owner AS owner, p.name AS repo, r.number AS run_number, j.name AS job, j.status AS status, j.started_ms AS started_ms, \
416
         j.finished_ms AS finished_ms, j.receipt_attest AS receipt_attest \
417
         FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id JOIN repositories p ON p.id = r.repo_id \
418
         WHERE j.runner_id = ? AND (? IS NULL OR j.started_ms < ?) ORDER BY j.started_ms DESC LIMIT ?",
419
    )
420
    .bind(id)
421
    .bind(before)
422
    .bind(before)
423
    .bind(limit)
424
    .fetch_all(pool)
425
    .await
426
}
427
428
/// (jobs run, passed, busy milliseconds) of runner `id`.
429
pub async fn totals(pool: &Pool, id: i64) -> sqlx::Result<(i64, i64, i64)> {
430
    sqlx::query_as(
431
        "SELECT COUNT(*), COALESCE(SUM(status = 'passed'), 0), COALESCE(SUM(finished_ms - started_ms), 0) \
432
         FROM flow_jobs WHERE runner_id = ? AND finished_ms IS NOT NULL",
433
    )
434
    .bind(id)
435
    .fetch_one(pool)
436
    .await
437
}
438
439
/// Jobs running on each runner now, by runner id.
440
pub async fn running(pool: &Pool) -> sqlx::Result<Vec<(i64, i64)>> {
441
    sqlx::query_as("SELECT runner_id, COUNT(*) FROM flow_jobs WHERE status = 'running' AND runner_id IS NOT NULL GROUP BY runner_id")
442
        .fetch_all(pool)
443
        .await
444
}
445
446
pub async fn set_job_runner(pool: &Pool, job_id: i64, runner_id: i64) -> sqlx::Result<()> {
447
    sqlx::query("UPDATE flow_jobs SET runner_id = ? WHERE id = ?").bind(runner_id).bind(job_id).execute(pool).await?;
448
    Ok(())
449
}