OXYGENHUB
# f4d6ef418a96 oxygenhub / crates / hub / src / db / runners.rs
14.9 KiB · Rust · last changed Runners' facts kept from their claims and shown 24 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 = ? WHERE id = ?").bind(state).bind(id).execute(pool).await?;
247
    Ok(())
248
}
249
250
/// Forget a runner: its credential stops working. Its jobs keep its name.
251
pub async fn remove(pool: &Pool, id: i64) -> sqlx::Result<()> {
252
    sqlx::query("DELETE FROM flow_runners WHERE id = ?").bind(id).execute(pool).await?;
253
    Ok(())
254
}
255
256
/// The key a runner signs receipts with: the one bound, or `key` (named
257
/// `name`) when none is yet.
258
pub async fn bind_key(pool: &Pool, id: i64, key: &str, name: &str) -> sqlx::Result<String> {
259
    sqlx::query("UPDATE flow_runners SET key = ?, key_name = ? WHERE id = ? AND key = ''")
260
        .bind(key)
261
        .bind(name)
262
        .bind(id)
263
        .execute(pool)
264
        .await?;
265
    sqlx::query_scalar("SELECT key FROM flow_runners WHERE id = ?").bind(id).fetch_one(pool).await
266
}
267
268
/// Keys that runners of this hub sign with.
269
pub async fn keys(pool: &Pool) -> sqlx::Result<Vec<String>> {
270
    sqlx::query_scalar("SELECT key FROM flow_runners WHERE key != ''").fetch_all(pool).await
271
}
272
273
/// A one-time registration token, valid for a day, for a runner of `scope`.
274
pub async fn add_registration(pool: &Pool, token_hash: &str, by: &str, scope: &Scope) -> sqlx::Result<()> {
275
    let now = now_ms();
276
    sqlx::query(
277
        "INSERT INTO flow_runner_registrations (token_hash, created_by, created_ms, expires_ms, scope_owner, scope_repo_id) VALUES (?, ?, ?, ?, ?, ?)",
278
    )
279
    .bind(token_hash)
280
    .bind(by)
281
    .bind(now)
282
    .bind(now + 24 * 3_600_000)
283
    .bind(&scope.owner)
284
    .bind(scope.repo_id)
285
    .execute(pool)
286
    .await?;
287
    Ok(())
288
}
289
290
/// The runners that may take a repository's jobs: the site's, its owner's
291
/// and its own (oxygenhub#41).
292
pub async fn serving(pool: &Pool, owner: &str, repo_id: i64) -> sqlx::Result<Vec<Runner>> {
293
    sqlx::query_as(
294
        "SELECT * FROM flow_runners WHERE (scope_owner = '' AND scope_repo_id IS NULL) \
295
         OR (scope_owner = ? AND scope_repo_id IS NULL) OR scope_repo_id = ? ORDER BY name",
296
    )
297
    .bind(owner)
298
    .bind(repo_id)
299
    .fetch_all(pool)
300
    .await
301
}
302
303
/// The runners that serve an owner: the site's, and those scoped to the
304
/// owner or one of its repositories.
305
pub async fn serving_owner(pool: &Pool, owner: &str) -> sqlx::Result<Vec<Runner>> {
306
    sqlx::query_as("SELECT * FROM flow_runners WHERE (scope_owner = '' AND scope_repo_id IS NULL) OR scope_owner = ? ORDER BY name")
307
        .bind(owner)
308
        .fetch_all(pool)
309
        .await
310
}
311
312
/// Change the repositories a runner serves.
313
pub async fn set_scope(pool: &Pool, id: i64, scope: &Scope) -> sqlx::Result<()> {
314
    sqlx::query("UPDATE flow_runners SET scope_owner = ?, scope_repo_id = ? WHERE id = ?")
315
        .bind(&scope.owner)
316
        .bind(scope.repo_id)
317
        .bind(id)
318
        .execute(pool)
319
        .await?;
320
    Ok(())
321
}
322
323
/// Registration tokens not used yet and not expired: (created by, created, expires).
324
pub async fn pending_registrations(pool: &Pool) -> sqlx::Result<Vec<(String, i64, i64)>> {
325
    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")
326
        .bind(now_ms())
327
        .fetch_all(pool)
328
        .await
329
}
330
331
/// Use registration token `token_hash` to register a runner: `None` when
332
/// the token is unknown, used or expired.
333
#[allow(clippy::too_many_arguments)]
334
pub async fn register(
335
    pool: &Pool,
336
    registration: &str,
337
    name: &str,
338
    token_hash: &str,
339
    key: &str,
340
    key_name: &str,
341
    by_default: &str,
342
) -> sqlx::Result<Option<Runner>> {
343
    let mut tx = super::begin_write(pool).await?;
344
    let now = now_ms();
345
    let found: Option<(String, String, Option<i64>)> = sqlx::query_as(
346
        "SELECT created_by, scope_owner, scope_repo_id FROM flow_runner_registrations WHERE token_hash = ? AND used_ms IS NULL AND expires_ms > ?",
347
    )
348
    .bind(registration)
349
    .bind(now)
350
    .fetch_optional(&mut *tx)
351
    .await?;
352
    let Some((who, scope_owner, scope_repo_id)) = found else { return Ok(None) };
353
    let id: i64 = sqlx::query_scalar(
354
        "INSERT INTO flow_runners (name, token_hash, source, key, key_name, created_ms, created_by, scope_owner, scope_repo_id) VALUES (?, ?, 'registered', ?, ?, ?, ?, ?, ?) RETURNING id",
355
    )
356
    .bind(name)
357
    .bind(token_hash)
358
    .bind(key)
359
    .bind(key_name)
360
    .bind(now)
361
    .bind(if who.is_empty() { by_default } else { &who })
362
    .bind(scope_owner)
363
    .bind(scope_repo_id)
364
    .fetch_one(&mut *tx)
365
    .await?;
366
    sqlx::query("UPDATE flow_runner_registrations SET used_ms = ?, runner_id = ? WHERE token_hash = ?")
367
        .bind(now)
368
        .bind(id)
369
        .bind(registration)
370
        .execute(&mut *tx)
371
        .await?;
372
    tx.commit().await?;
373
    get(pool, id).await
374
}
375
376
/// A job a runner ran, for its history.
377
#[derive(Clone, Debug, FromRow)]
378
pub struct RunnerJob {
379
    pub owner: String,
380
    pub repo: String,
381
    pub run_number: i64,
382
    pub job: String,
383
    pub status: String,
384
    pub started_ms: Option<i64>,
385
    pub finished_ms: Option<i64>,
386
    pub receipt_attest: String,
387
}
388
389
/// Jobs runner `id` ran or is running, newest first.
390
pub async fn history(pool: &Pool, id: i64, limit: i64, before: Option<i64>) -> sqlx::Result<Vec<RunnerJob>> {
391
    sqlx::query_as(
392
        "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, \
393
         j.finished_ms AS finished_ms, j.receipt_attest AS receipt_attest \
394
         FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id JOIN repositories p ON p.id = r.repo_id \
395
         WHERE j.runner_id = ? AND (? IS NULL OR j.started_ms < ?) ORDER BY j.started_ms DESC LIMIT ?",
396
    )
397
    .bind(id)
398
    .bind(before)
399
    .bind(before)
400
    .bind(limit)
401
    .fetch_all(pool)
402
    .await
403
}
404
405
/// (jobs run, passed, busy milliseconds) of runner `id`.
406
pub async fn totals(pool: &Pool, id: i64) -> sqlx::Result<(i64, i64, i64)> {
407
    sqlx::query_as(
408
        "SELECT COUNT(*), COALESCE(SUM(status = 'passed'), 0), COALESCE(SUM(finished_ms - started_ms), 0) \
409
         FROM flow_jobs WHERE runner_id = ? AND finished_ms IS NOT NULL",
410
    )
411
    .bind(id)
412
    .fetch_one(pool)
413
    .await
414
}
415
416
/// Jobs running on each runner now, by runner id.
417
pub async fn running(pool: &Pool) -> sqlx::Result<Vec<(i64, i64)>> {
418
    sqlx::query_as("SELECT runner_id, COUNT(*) FROM flow_jobs WHERE status = 'running' AND runner_id IS NOT NULL GROUP BY runner_id")
419
        .fetch_all(pool)
420
        .await
421
}
422
423
pub async fn set_job_runner(pool: &Pool, job_id: i64, runner_id: i64) -> sqlx::Result<()> {
424
    sqlx::query("UPDATE flow_jobs SET runner_id = ? WHERE id = ?").bind(runner_id).bind(job_id).execute(pool).await?;
425
    Ok(())
426
}