b6b7e4cf Andy McCall 1 month ago ↶
1
2
3
4
use super :: { now_ms, Pool} ;
5
use sqlx:: FromRow;
6
4d2e44d7 Andy McCall 1 month ago ↶
7
# [ derive ( Clone, Debug, Default, FromRow ) ]
b6b7e4cf Andy McCall 1 month ago ↶
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 > ,
74be9796 Andy McCall 1 month ago ↶
26
27
pub scope_owner : String,
28
29
pub scope_repo_id : Option< i64 > ,
7f5f6c05 Andy McCall 1 month ago ↶
30
31
pub admin_labels : String,
32
33
pub timeout_minutes : Option< i64 > ,
92a797b2 Andy McCall 24 days ago ↶
34
35
pub fetches_images : bool ,
2fb70d01 Andy McCall 24 days ago ↶
36
37
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,
4d2e44d7 Andy McCall 1 month ago ↶
43
44
pub tiers : String,
45
46
pub images : String,
47
48
pub profile : String,
b6b7e4cf Andy McCall 1 month ago ↶
49
}
50
74be9796 Andy McCall 1 month ago ↶
51
52
# [ derive ( Clone, Debug, Default, PartialEq, Eq ) ]
53
pub struct Scope {
54
55
pub owner : String,
56
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
b6b7e4cf Andy McCall 1 month ago ↶
66
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
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
7f5f6c05 Andy McCall 1 month ago ↶
84
85
b6b7e4cf Andy McCall 1 month ago ↶
86
pub fn label_list ( & self ) -> Vec< & str > {
7f5f6c05 Andy McCall 1 month ago ↶
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
101
pub fn reported_label_list ( & self ) -> Vec< & str > {
b6b7e4cf Andy McCall 1 month ago ↶
102
self .labels.split ( ' ,' ) .filter ( | l | ! l.is_empty ( ) ) .collect ( )
103
}
74be9796 Andy McCall 1 month ago ↶
104
105
pub fn scope ( & self ) -> Scope {
106
Scope { owner: self .scope_owner.clone ( ) , repo_id: self .scope_repo_id }
107
}
b6b7e4cf Andy McCall 1 month ago ↶
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
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
134
7f5f6c05 Andy McCall 1 month ago ↶
135
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
b6b7e4cf Andy McCall 1 month ago ↶
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
}
4d2e44d7 Andy McCall 1 month ago ↶
172
Ok ( ( ) )
173
}
174
2fb70d01 Andy McCall 24 days ago ↶
175
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
191
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
4d2e44d7 Andy McCall 1 month ago ↶
207
208
209
92a797b2 Andy McCall 24 days ago ↶
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< ( ) > {
4d2e44d7 Andy McCall 1 month ago ↶
218
let images = images.map ( | i | serde_json:: to_string( i) .unwrap_or_default ( ) ) .unwrap_or_default ( ) ;
92a797b2 Andy McCall 24 days ago ↶
219
sqlx:: query( " UPDATE flow_runners SET tiers = ?, images = ?, fetches_images = ?, profile = ? WHERE id = ?" )
4d2e44d7 Andy McCall 1 month ago ↶
220
.bind ( tiers.join ( " ," ) )
221
.bind ( images)
92a797b2 Andy McCall 24 days ago ↶
222
.bind ( fetches_images)
4d2e44d7 Andy McCall 1 month ago ↶
223
.bind ( profile)
224
.bind ( id)
225
.execute ( pool)
226
.await? ;
b6b7e4cf Andy McCall 1 month ago ↶
227
Ok ( ( ) )
4d2e44d7 Andy McCall 1 month ago ↶
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
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
}
b6b7e4cf Andy McCall 1 month ago ↶
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
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
257
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
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
74be9796 Andy McCall 1 month ago ↶
273
274
pub async fn add_registration ( pool : & Pool, token_hash : & str , by : & str , scope : & Scope) -> sqlx:: Result< ( ) > {
b6b7e4cf Andy McCall 1 month ago ↶
275
let now = now_ms ( ) ;
74be9796 Andy McCall 1 month ago ↶
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
7974adb0 Andy McCall 1 month ago ↶
290
291
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
304
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
74be9796 Andy McCall 1 month ago ↶
312
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)
b6b7e4cf Andy McCall 1 month ago ↶
318
.execute ( pool)
319
.await? ;
320
Ok ( ( ) )
321
}
322
323
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
332
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 ( ) ;
74be9796 Andy McCall 1 month ago ↶
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 ) } ;
b6b7e4cf Andy McCall 1 month ago ↶
353
let id: i64 = sqlx:: query_scalar(
74be9796 Andy McCall 1 month ago ↶
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" ,
b6b7e4cf Andy McCall 1 month ago ↶
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 } )
74be9796 Andy McCall 1 month ago ↶
362
.bind ( scope_owner)
363
.bind ( scope_repo_id)
b6b7e4cf Andy McCall 1 month ago ↶
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
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
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
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
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
}