OXYGENHUB
# 7f5f6c056d8f oxygenhub / crates / hub / src / db / runners.rs
11.9 KiB · Rust · last changed A runner's own labels and time limit, from its admin page 1 month 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, 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
}
35
36
/// Which repositories' jobs a runner may take (oxygenhub#41).
37
#[derive(Clone, Debug, Default, PartialEq, Eq)]
38
pub struct Scope {
39
    /// Empty: any owner.
40
    pub owner: String,
41
    /// `None`: any of the owner's repositories.
42
    pub repo_id: Option<i64>,
43
}
44
45
impl Scope {
46
    pub fn is_site(&self) -> bool {
47
        self.owner.is_empty() && self.repo_id.is_none()
48
    }
49
}
50
51
/// A runner that has not polled for this long is shown offline.
52
pub const ONLINE_MS: i64 = 90_000;
53
54
impl Runner {
55
    pub fn online(&self) -> bool {
56
        self.last_seen_ms.is_some_and(|t| now_ms() - t < ONLINE_MS)
57
    }
58
59
    /// `online`, `offline`, `draining` or `disabled`.
60
    pub fn status(&self) -> &'static str {
61
        match self.state.as_str() {
62
            "disabled" => "disabled",
63
            "draining" => "draining",
64
            _ if self.online() => "online",
65
            _ => "offline",
66
        }
67
    }
68
69
    /// Every label it carries: what it reports, and what an administrator
70
    /// gave it.
71
    pub fn label_list(&self) -> Vec<&str> {
72
        let mut out: Vec<&str> = self.labels.split(',').filter(|l| !l.is_empty()).collect();
73
        for l in self.admin_label_list() {
74
            if !out.contains(&l) {
75
                out.push(l);
76
            }
77
        }
78
        out
79
    }
80
81
    pub fn admin_label_list(&self) -> Vec<&str> {
82
        self.admin_labels.split(',').filter(|l| !l.is_empty()).collect()
83
    }
84
85
    /// The labels it reported itself, as the admin page distinguishes them.
86
    pub fn reported_label_list(&self) -> Vec<&str> {
87
        self.labels.split(',').filter(|l| !l.is_empty()).collect()
88
    }
89
90
    pub fn scope(&self) -> Scope {
91
        Scope { owner: self.scope_owner.clone(), repo_id: self.scope_repo_id }
92
    }
93
}
94
95
pub async fn by_token(pool: &Pool, token_hash: &str) -> sqlx::Result<Option<Runner>> {
96
    sqlx::query_as("SELECT * FROM flow_runners WHERE token_hash = ?").bind(token_hash).fetch_optional(pool).await
97
}
98
99
pub async fn get(pool: &Pool, id: i64) -> sqlx::Result<Option<Runner>> {
100
    sqlx::query_as("SELECT * FROM flow_runners WHERE id = ?").bind(id).fetch_optional(pool).await
101
}
102
103
pub async fn list(pool: &Pool) -> sqlx::Result<Vec<Runner>> {
104
    sqlx::query_as("SELECT * FROM flow_runners ORDER BY name, id").fetch_all(pool).await
105
}
106
107
/// The row of a runner configured with a hub.toml token, made on first sight.
108
pub async fn configured(pool: &Pool, token_hash: &str, name: &str) -> sqlx::Result<Runner> {
109
    sqlx::query("INSERT OR IGNORE INTO flow_runners (name, token_hash, source, created_ms) VALUES (?, ?, 'config', ?)")
110
        .bind(name)
111
        .bind(token_hash)
112
        .bind(now_ms())
113
        .execute(pool)
114
        .await?;
115
    Ok(by_token(pool, token_hash).await?.expect("just made"))
116
}
117
118
/// What a claim says about the runner, and that it was seen now. A
119
/// configured runner takes the name it claims with.
120
/// The administrator's settings for a runner (#98).
121
pub async fn set_limits(pool: &Pool, id: i64, admin_labels: &str, timeout_minutes: Option<i64>) -> sqlx::Result<()> {
122
    sqlx::query("UPDATE flow_runners SET admin_labels = ?, timeout_minutes = ? WHERE id = ?")
123
        .bind(admin_labels)
124
        .bind(timeout_minutes)
125
        .bind(id)
126
        .execute(pool)
127
        .await?;
128
    Ok(())
129
}
130
131
pub async fn seen(pool: &Pool, id: i64, name: Option<&str>, info: Option<&flow_core::api::RunnerInfo>) -> sqlx::Result<()> {
132
    let now = now_ms();
133
    match info {
134
        Some(i) => {
135
            sqlx::query(
136
                "UPDATE flow_runners SET arch = ?, os = ?, version = ?, labels = ?, max_jobs = ?, memory_mib = ?, cpus = ?, last_seen_ms = ? WHERE id = ?",
137
            )
138
            .bind(&i.arch)
139
            .bind(&i.os)
140
            .bind(&i.version)
141
            .bind(i.labels.join(","))
142
            .bind(i.max_jobs as i64)
143
            .bind(i.memory_mib.map(i64::from))
144
            .bind(i.cpus.map(i64::from))
145
            .bind(now)
146
            .bind(id)
147
            .execute(pool)
148
            .await?;
149
        }
150
        None => {
151
            sqlx::query("UPDATE flow_runners SET last_seen_ms = ? WHERE id = ?").bind(now).bind(id).execute(pool).await?;
152
        }
153
    }
154
    if let Some(n) = name {
155
        sqlx::query("UPDATE flow_runners SET name = ? WHERE id = ? AND source = 'config'").bind(n).bind(id).execute(pool).await?;
156
    }
157
    Ok(())
158
}
159
160
pub async fn set_state(pool: &Pool, id: i64, state: &str) -> sqlx::Result<()> {
161
    sqlx::query("UPDATE flow_runners SET state = ? WHERE id = ?").bind(state).bind(id).execute(pool).await?;
162
    Ok(())
163
}
164
165
/// Forget a runner: its credential stops working. Its jobs keep its name.
166
pub async fn remove(pool: &Pool, id: i64) -> sqlx::Result<()> {
167
    sqlx::query("DELETE FROM flow_runners WHERE id = ?").bind(id).execute(pool).await?;
168
    Ok(())
169
}
170
171
/// The key a runner signs receipts with: the one bound, or `key` (named
172
/// `name`) when none is yet.
173
pub async fn bind_key(pool: &Pool, id: i64, key: &str, name: &str) -> sqlx::Result<String> {
174
    sqlx::query("UPDATE flow_runners SET key = ?, key_name = ? WHERE id = ? AND key = ''")
175
        .bind(key)
176
        .bind(name)
177
        .bind(id)
178
        .execute(pool)
179
        .await?;
180
    sqlx::query_scalar("SELECT key FROM flow_runners WHERE id = ?").bind(id).fetch_one(pool).await
181
}
182
183
/// Keys that runners of this hub sign with.
184
pub async fn keys(pool: &Pool) -> sqlx::Result<Vec<String>> {
185
    sqlx::query_scalar("SELECT key FROM flow_runners WHERE key != ''").fetch_all(pool).await
186
}
187
188
/// A one-time registration token, valid for a day, for a runner of `scope`.
189
pub async fn add_registration(pool: &Pool, token_hash: &str, by: &str, scope: &Scope) -> sqlx::Result<()> {
190
    let now = now_ms();
191
    sqlx::query(
192
        "INSERT INTO flow_runner_registrations (token_hash, created_by, created_ms, expires_ms, scope_owner, scope_repo_id) VALUES (?, ?, ?, ?, ?, ?)",
193
    )
194
    .bind(token_hash)
195
    .bind(by)
196
    .bind(now)
197
    .bind(now + 24 * 3_600_000)
198
    .bind(&scope.owner)
199
    .bind(scope.repo_id)
200
    .execute(pool)
201
    .await?;
202
    Ok(())
203
}
204
205
/// The runners that may take a repository's jobs: the site's, its owner's
206
/// and its own (oxygenhub#41).
207
pub async fn serving(pool: &Pool, owner: &str, repo_id: i64) -> sqlx::Result<Vec<Runner>> {
208
    sqlx::query_as(
209
        "SELECT * FROM flow_runners WHERE (scope_owner = '' AND scope_repo_id IS NULL) \
210
         OR (scope_owner = ? AND scope_repo_id IS NULL) OR scope_repo_id = ? ORDER BY name",
211
    )
212
    .bind(owner)
213
    .bind(repo_id)
214
    .fetch_all(pool)
215
    .await
216
}
217
218
/// The runners that serve an owner: the site's, and those scoped to the
219
/// owner or one of its repositories.
220
pub async fn serving_owner(pool: &Pool, owner: &str) -> sqlx::Result<Vec<Runner>> {
221
    sqlx::query_as("SELECT * FROM flow_runners WHERE (scope_owner = '' AND scope_repo_id IS NULL) OR scope_owner = ? ORDER BY name")
222
        .bind(owner)
223
        .fetch_all(pool)
224
        .await
225
}
226
227
/// Change the repositories a runner serves.
228
pub async fn set_scope(pool: &Pool, id: i64, scope: &Scope) -> sqlx::Result<()> {
229
    sqlx::query("UPDATE flow_runners SET scope_owner = ?, scope_repo_id = ? WHERE id = ?")
230
        .bind(&scope.owner)
231
        .bind(scope.repo_id)
232
        .bind(id)
233
        .execute(pool)
234
        .await?;
235
    Ok(())
236
}
237
238
/// Registration tokens not used yet and not expired: (created by, created, expires).
239
pub async fn pending_registrations(pool: &Pool) -> sqlx::Result<Vec<(String, i64, i64)>> {
240
    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")
241
        .bind(now_ms())
242
        .fetch_all(pool)
243
        .await
244
}
245
246
/// Use registration token `token_hash` to register a runner: `None` when
247
/// the token is unknown, used or expired.
248
#[allow(clippy::too_many_arguments)]
249
pub async fn register(
250
    pool: &Pool,
251
    registration: &str,
252
    name: &str,
253
    token_hash: &str,
254
    key: &str,
255
    key_name: &str,
256
    by_default: &str,
257
) -> sqlx::Result<Option<Runner>> {
258
    let mut tx = super::begin_write(pool).await?;
259
    let now = now_ms();
260
    let found: Option<(String, String, Option<i64>)> = sqlx::query_as(
261
        "SELECT created_by, scope_owner, scope_repo_id FROM flow_runner_registrations WHERE token_hash = ? AND used_ms IS NULL AND expires_ms > ?",
262
    )
263
    .bind(registration)
264
    .bind(now)
265
    .fetch_optional(&mut *tx)
266
    .await?;
267
    let Some((who, scope_owner, scope_repo_id)) = found else { return Ok(None) };
268
    let id: i64 = sqlx::query_scalar(
269
        "INSERT INTO flow_runners (name, token_hash, source, key, key_name, created_ms, created_by, scope_owner, scope_repo_id) VALUES (?, ?, 'registered', ?, ?, ?, ?, ?, ?) RETURNING id",
270
    )
271
    .bind(name)
272
    .bind(token_hash)
273
    .bind(key)
274
    .bind(key_name)
275
    .bind(now)
276
    .bind(if who.is_empty() { by_default } else { &who })
277
    .bind(scope_owner)
278
    .bind(scope_repo_id)
279
    .fetch_one(&mut *tx)
280
    .await?;
281
    sqlx::query("UPDATE flow_runner_registrations SET used_ms = ?, runner_id = ? WHERE token_hash = ?")
282
        .bind(now)
283
        .bind(id)
284
        .bind(registration)
285
        .execute(&mut *tx)
286
        .await?;
287
    tx.commit().await?;
288
    get(pool, id).await
289
}
290
291
/// A job a runner ran, for its history.
292
#[derive(Clone, Debug, FromRow)]
293
pub struct RunnerJob {
294
    pub owner: String,
295
    pub repo: String,
296
    pub run_number: i64,
297
    pub job: String,
298
    pub status: String,
299
    pub started_ms: Option<i64>,
300
    pub finished_ms: Option<i64>,
301
    pub receipt_attest: String,
302
}
303
304
/// Jobs runner `id` ran or is running, newest first.
305
pub async fn history(pool: &Pool, id: i64, limit: i64, before: Option<i64>) -> sqlx::Result<Vec<RunnerJob>> {
306
    sqlx::query_as(
307
        "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, \
308
         j.finished_ms AS finished_ms, j.receipt_attest AS receipt_attest \
309
         FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id JOIN repositories p ON p.id = r.repo_id \
310
         WHERE j.runner_id = ? AND (? IS NULL OR j.started_ms < ?) ORDER BY j.started_ms DESC LIMIT ?",
311
    )
312
    .bind(id)
313
    .bind(before)
314
    .bind(before)
315
    .bind(limit)
316
    .fetch_all(pool)
317
    .await
318
}
319
320
/// (jobs run, passed, busy milliseconds) of runner `id`.
321
pub async fn totals(pool: &Pool, id: i64) -> sqlx::Result<(i64, i64, i64)> {
322
    sqlx::query_as(
323
        "SELECT COUNT(*), COALESCE(SUM(status = 'passed'), 0), COALESCE(SUM(finished_ms - started_ms), 0) \
324
         FROM flow_jobs WHERE runner_id = ? AND finished_ms IS NOT NULL",
325
    )
326
    .bind(id)
327
    .fetch_one(pool)
328
    .await
329
}
330
331
/// Jobs running on each runner now, by runner id.
332
pub async fn running(pool: &Pool) -> sqlx::Result<Vec<(i64, i64)>> {
333
    sqlx::query_as("SELECT runner_id, COUNT(*) FROM flow_jobs WHERE status = 'running' AND runner_id IS NOT NULL GROUP BY runner_id")
334
        .fetch_all(pool)
335
        .await
336
}
337
338
pub async fn set_job_runner(pool: &Pool, job_id: i64, runner_id: i64) -> sqlx::Result<()> {
339
    sqlx::query("UPDATE flow_jobs SET runner_id = ? WHERE id = ?").bind(runner_id).bind(job_id).execute(pool).await?;
340
    Ok(())
341
}