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 25 days ago ↶
34
35
pub fetches_images : bool ,
2fb70d01 Andy McCall 25 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
e9b9619c Andy McCall 19 days ago ↶
74
75
76
pub fn connectivity ( & self ) -> & 'static str {
77
if self .online ( ) {
78
" online"
79
} else {
80
" offline"
81
}
82
}
83
b6b7e4cf Andy McCall 1 month ago ↶
84
85
pub fn status ( & self ) -> & 'static str {
86
match self .state.as_str ( ) {
87
" disabled" => " disabled" ,
88
" draining" => " draining" ,
89
_ if self .online ( ) => " online" ,
90
_ => " offline" ,
91
}
92
}
93
7f5f6c05 Andy McCall 1 month ago ↶
94
95
b6b7e4cf Andy McCall 1 month ago ↶
96
pub fn label_list ( & self ) -> Vec< & str > {
7f5f6c05 Andy McCall 1 month ago ↶
97
let mut out: Vec< & str > = self .labels.split ( ' ,' ) .filter ( | l | ! l.is_empty ( ) ) .collect ( ) ;
98
for l in self .admin_label_list ( ) {
99
if ! out.contains ( & l) {
100
out.push ( l) ;
101
}
102
}
103
out
104
}
105
106
pub fn admin_label_list ( & self ) -> Vec< & str > {
107
self .admin_labels.split ( ' ,' ) .filter ( | l | ! l.is_empty ( ) ) .collect ( )
108
}
109
110
111
pub fn reported_label_list ( & self ) -> Vec< & str > {
b6b7e4cf Andy McCall 1 month ago ↶
112
self .labels.split ( ' ,' ) .filter ( | l | ! l.is_empty ( ) ) .collect ( )
113
}
74be9796 Andy McCall 1 month ago ↶
114
115
pub fn scope ( & self ) -> Scope {
116
Scope { owner: self .scope_owner.clone ( ) , repo_id: self .scope_repo_id }
117
}
b6b7e4cf Andy McCall 1 month ago ↶
118
}
119
120
pub async fn by_token ( pool : & Pool, token_hash : & str ) -> sqlx:: Result< Option< Runner> > {
5d90e0e0 Andy McCall 20 days ago
121
sqlx:: query_as( " SELECT * FROM flow_runners WHERE token_hash = $1" ) .bind ( token_hash) .fetch_optional ( pool) .await
b6b7e4cf Andy McCall 1 month ago ↶
122
}
123
124
pub async fn get ( pool : & Pool, id : i64 ) -> sqlx:: Result< Option< Runner> > {
5d90e0e0 Andy McCall 20 days ago
125
sqlx:: query_as( " SELECT * FROM flow_runners WHERE id = $1" ) .bind ( id) .fetch_optional ( pool) .await
b6b7e4cf Andy McCall 1 month ago ↶
126
}
127
128
pub async fn list ( pool : & Pool) -> sqlx:: Result< Vec< Runner> > {
129
sqlx:: query_as( " SELECT * FROM flow_runners ORDER BY name, id" ) .fetch_all ( pool) .await
130
}
131
132
133
pub async fn configured ( pool : & Pool, token_hash : & str , name : & str ) -> sqlx:: Result< Runner> {
d1bf2d69 Andy McCall 20 days ago ↶
134
sqlx:: query( " INSERT INTO flow_runners (name, token_hash, source, created_ms) VALUES ($1, $2, 'config', $3) ON CONFLICT DO NOTHING" )
b6b7e4cf Andy McCall 1 month ago ↶
135
.bind ( name)
136
.bind ( token_hash)
137
.bind ( now_ms ( ) )
138
.execute ( pool)
139
.await? ;
140
Ok ( by_token ( pool, token_hash) .await? .expect ( " just made" ) )
141
}
142
143
144
7f5f6c05 Andy McCall 1 month ago ↶
145
146
pub async fn set_limits ( pool : & Pool, id : i64 , admin_labels : & str , timeout_minutes : Option< i64 > ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
147
sqlx:: query( " UPDATE flow_runners SET admin_labels = $1, timeout_minutes = $2 WHERE id = $3" )
7f5f6c05 Andy McCall 1 month ago ↶
148
.bind ( admin_labels)
149
.bind ( timeout_minutes)
150
.bind ( id)
151
.execute ( pool)
152
.await? ;
153
Ok ( ( ) )
154
}
155
b6b7e4cf Andy McCall 1 month ago ↶
156
pub async fn seen ( pool : & Pool, id : i64 , name : Option< & str > , info : Option< & flow_core:: api:: RunnerInfo> ) -> sqlx:: Result< ( ) > {
157
let now = now_ms ( ) ;
158
match info {
159
Some ( i) => {
160
sqlx:: query(
5d90e0e0 Andy McCall 20 days ago
161
" UPDATE flow_runners SET arch = $1, os = $2, version = $3, labels = $4, max_jobs = $5, memory_mib = $6, cpus = $7, last_seen_ms = $8 WHERE id = $9" ,
b6b7e4cf Andy McCall 1 month ago ↶
162
)
163
.bind ( & i.arch)
164
.bind ( & i.os)
165
.bind ( & i.version)
166
.bind ( i.labels.join ( " ," ) )
167
.bind ( i.max_jobs as i64 )
168
.bind ( i.memory_mib.map ( i64 :: from) )
169
.bind ( i.cpus.map ( i64 :: from) )
170
.bind ( now)
171
.bind ( id)
172
.execute ( pool)
173
.await? ;
174
}
175
None => {
5d90e0e0 Andy McCall 20 days ago
176
sqlx:: query( " UPDATE flow_runners SET last_seen_ms = $1 WHERE id = $2" ) .bind ( now) .bind ( id) .execute ( pool) .await? ;
b6b7e4cf Andy McCall 1 month ago ↶
177
}
178
}
179
if let Some ( n) = name {
5d90e0e0 Andy McCall 20 days ago
180
sqlx:: query( " UPDATE flow_runners SET name = $1 WHERE id = $2 AND source = 'config'" ) .bind ( n) .bind ( id) .execute ( pool) .await? ;
b6b7e4cf Andy McCall 1 month ago ↶
181
}
4d2e44d7 Andy McCall 1 month ago ↶
182
Ok ( ( ) )
183
}
184
2fb70d01 Andy McCall 25 days ago ↶
185
186
pub async fn set_facts ( pool : & Pool, id : i64 , f : & flow_core:: api:: Facts) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
187
sqlx:: query( " UPDATE flow_runners SET free_disk_mib = $1, load_x100 = $2, speed = $3, speed_class = $4, kernel = $5 WHERE id = $6" )
2fb70d01 Andy McCall 25 days ago ↶
188
.bind ( f.free_disk_mib.map ( | v | v.min ( i64 :: MAX as u64 ) as i64 ) )
189
.bind ( f.load_x100.map ( i64 :: from) )
190
.bind ( f.speed.map ( i64 :: from) )
191
.bind ( f.speed_class.map ( i64 :: from) )
192
.bind ( f.kernel.clone ( ) .unwrap_or_default ( ) )
193
.bind ( id)
194
.execute ( pool)
195
.await? ;
196
Ok ( ( ) )
197
}
198
199
impl Runner {
200
201
202
pub fn facts_line ( & self ) -> String {
203
let mut out = Vec :: new( ) ;
204
if let Some ( s) = self .speed {
205
out.push ( format! ( " speed {s} (class {} )" , self .speed_class.unwrap_or ( 0 ) ) ) ;
206
}
207
if let Some ( l) = self .load_x100 {
208
out.push ( format! ( " load {:.2} " , l as f64 / 100. 0 ) ) ;
209
}
210
if let Some ( d) = self .free_disk_mib {
211
out.push ( if d > = 1024 { format! ( " {} GiB free" , d / 1024 ) } else { format! ( " {d} MiB free" ) } ) ;
212
}
213
out.join ( " · " )
214
}
215
}
216
4d2e44d7 Andy McCall 1 month ago ↶
217
218
219
92a797b2 Andy McCall 25 days ago ↶
220
pub async fn set_offer (
221
pool : & Pool,
222
id : i64 ,
223
tiers : & [String],
224
images : Option< & [ String] > ,
225
fetches_images : bool ,
226
profile : & str ,
227
) -> sqlx:: Result< ( ) > {
4d2e44d7 Andy McCall 1 month ago ↶
228
let images = images.map ( | i | serde_json:: to_string( i) .unwrap_or_default ( ) ) .unwrap_or_default ( ) ;
5d90e0e0 Andy McCall 20 days ago
229
sqlx:: query( " UPDATE flow_runners SET tiers = $1, images = $2, fetches_images = $3, profile = $4 WHERE id = $5" )
4d2e44d7 Andy McCall 1 month ago ↶
230
.bind ( tiers.join ( " ," ) )
231
.bind ( images)
92a797b2 Andy McCall 25 days ago ↶
232
.bind ( fetches_images)
4d2e44d7 Andy McCall 1 month ago ↶
233
.bind ( profile)
234
.bind ( id)
235
.execute ( pool)
236
.await? ;
b6b7e4cf Andy McCall 1 month ago ↶
237
Ok ( ( ) )
4d2e44d7 Andy McCall 1 month ago ↶
238
}
239
240
impl Runner {
241
pub fn tier_list ( & self ) -> Vec< & str > {
242
self .tiers.split ( ' ,' ) .filter ( | t | ! t.is_empty ( ) ) .collect ( )
243
}
244
245
246
pub fn image_list ( & self ) -> Option< Vec< String> > {
247
if self .images.is_empty ( ) {
248
None
249
} else {
250
serde_json:: from_str( & self .images) .ok ( )
251
}
252
}
b6b7e4cf Andy McCall 1 month ago ↶
253
}
254
255
pub async fn set_state ( pool : & Pool, id : i64 , state : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
256
sqlx:: query( " UPDATE flow_runners SET state = $1, state_ms = $2 WHERE id = $3" )
4a4400c5 Andy McCall 22 days ago ↶
257
.bind ( state)
258
.bind ( super :: now_ms( ) )
259
.bind ( id)
260
.execute ( pool)
261
.await? ;
b6b7e4cf Andy McCall 1 month ago ↶
262
Ok ( ( ) )
4a4400c5 Andy McCall 22 days ago ↶
263
}
264
265
266
267
pub async fn erroring ( pool : & Pool, id : i64 , n : u32 ) -> sqlx:: Result< Option< String> > {
268
if n = = 0 {
269
return Ok ( None ) ;
270
}
271
let last: Vec< ( String, String) > = sqlx:: query_as(
272
" SELECT j.status, j.error FROM flow_jobs j JOIN flow_runners r ON r.id = j.runner_id
5d90e0e0 Andy McCall 20 days ago
273
WHERE j.runner_id = $1 AND j.finished_ms IS NOT NULL AND j.finished_ms >= r.state_ms
274
ORDER BY j.finished_ms DESC, j.id DESC LIMIT $2" ,
4a4400c5 Andy McCall 22 days ago ↶
275
)
276
.bind ( id)
277
.bind ( n as i64 )
278
.fetch_all ( pool)
279
.await? ;
280
Ok ( ( last.len ( ) = = n as usize & & last.iter ( ) .all ( | ( s , _) | s = = " error" ) ) .then ( | | last[ 0 ] .1. clone ( ) ) )
b6b7e4cf Andy McCall 1 month ago ↶
281
}
282
283
284
pub async fn remove ( pool : & Pool, id : i64 ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
285
sqlx:: query( " DELETE FROM flow_runners WHERE id = $1" ) .bind ( id) .execute ( pool) .await? ;
b6b7e4cf Andy McCall 1 month ago ↶
286
Ok ( ( ) )
287
}
288
289
290
291
pub async fn bind_key ( pool : & Pool, id : i64 , key : & str , name : & str ) -> sqlx:: Result< String> {
5d90e0e0 Andy McCall 20 days ago
292
sqlx:: query( " UPDATE flow_runners SET key = $1, key_name = $2 WHERE id = $3 AND key = ''" )
b6b7e4cf Andy McCall 1 month ago ↶
293
.bind ( key)
294
.bind ( name)
295
.bind ( id)
296
.execute ( pool)
297
.await? ;
5d90e0e0 Andy McCall 20 days ago
298
sqlx:: query_scalar( " SELECT key FROM flow_runners WHERE id = $1" ) .bind ( id) .fetch_one ( pool) .await
b6b7e4cf Andy McCall 1 month ago ↶
299
}
300
301
302
pub async fn keys ( pool : & Pool) -> sqlx:: Result< Vec< String> > {
303
sqlx:: query_scalar( " SELECT key FROM flow_runners WHERE key != ''" ) .fetch_all ( pool) .await
304
}
305
74be9796 Andy McCall 1 month ago ↶
306
307
pub async fn add_registration ( pool : & Pool, token_hash : & str , by : & str , scope : & Scope) -> sqlx:: Result< ( ) > {
b6b7e4cf Andy McCall 1 month ago ↶
308
let now = now_ms ( ) ;
74be9796 Andy McCall 1 month ago ↶
309
sqlx:: query(
5d90e0e0 Andy McCall 20 days ago
310
" INSERT INTO flow_runner_registrations (token_hash, created_by, created_ms, expires_ms, scope_owner, scope_repo_id) VALUES ($1, $2, $3, $4, $5, $6)" ,
74be9796 Andy McCall 1 month ago ↶
311
)
312
.bind ( token_hash)
313
.bind ( by)
314
.bind ( now)
315
.bind ( now + 24 * 3_600_000 )
316
.bind ( & scope.owner)
317
.bind ( scope.repo_id)
318
.execute ( pool)
319
.await? ;
320
Ok ( ( ) )
321
}
322
7974adb0 Andy McCall 1 month ago ↶
323
324
325
pub async fn serving ( pool : & Pool, owner : & str , repo_id : i64 ) -> sqlx:: Result< Vec< Runner> > {
326
sqlx:: query_as(
327
" SELECT * FROM flow_runners WHERE (scope_owner = '' AND scope_repo_id IS NULL) \
5d90e0e0 Andy McCall 20 days ago
328
OR (scope_owner = $1 AND scope_repo_id IS NULL) OR scope_repo_id = $2 ORDER BY name" ,
7974adb0 Andy McCall 1 month ago ↶
329
)
330
.bind ( owner)
331
.bind ( repo_id)
332
.fetch_all ( pool)
333
.await
334
}
335
336
337
338
pub async fn serving_owner ( pool : & Pool, owner : & str ) -> sqlx:: Result< Vec< Runner> > {
5d90e0e0 Andy McCall 20 days ago
339
sqlx:: query_as( " SELECT * FROM flow_runners WHERE (scope_owner = '' AND scope_repo_id IS NULL) OR scope_owner = $1 ORDER BY name" )
7974adb0 Andy McCall 1 month ago ↶
340
.bind ( owner)
341
.fetch_all ( pool)
342
.await
343
}
344
74be9796 Andy McCall 1 month ago ↶
345
346
pub async fn set_scope ( pool : & Pool, id : i64 , scope : & Scope) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
347
sqlx:: query( " UPDATE flow_runners SET scope_owner = $1, scope_repo_id = $2 WHERE id = $3" )
74be9796 Andy McCall 1 month ago ↶
348
.bind ( & scope.owner)
349
.bind ( scope.repo_id)
350
.bind ( id)
b6b7e4cf Andy McCall 1 month ago ↶
351
.execute ( pool)
352
.await? ;
353
Ok ( ( ) )
354
}
355
356
357
pub async fn pending_registrations ( pool : & Pool) -> sqlx:: Result< Vec< ( String, i64 , i64 ) > > {
5d90e0e0 Andy McCall 20 days ago
358
sqlx:: query_as( " SELECT created_by, created_ms, expires_ms FROM flow_runner_registrations WHERE used_ms IS NULL AND expires_ms > $1 ORDER BY created_ms DESC" )
b6b7e4cf Andy McCall 1 month ago ↶
359
.bind ( now_ms ( ) )
360
.fetch_all ( pool)
361
.await
362
}
363
364
365
366
# [ allow ( clippy::too_many_arguments ) ]
367
pub async fn register (
368
pool : & Pool,
369
registration : & str ,
370
name : & str ,
371
token_hash : & str ,
372
key : & str ,
373
key_name : & str ,
374
by_default : & str ,
375
) -> sqlx:: Result< Option< Runner> > {
376
let mut tx = super :: begin_write( pool) .await? ;
377
let now = now_ms ( ) ;
74be9796 Andy McCall 1 month ago ↶
378
let found: Option< ( String, String, Option< i64 > ) > = sqlx:: query_as(
5d90e0e0 Andy McCall 20 days ago
379
" SELECT created_by, scope_owner, scope_repo_id FROM flow_runner_registrations WHERE token_hash = $1 AND used_ms IS NULL AND expires_ms > $2" ,
74be9796 Andy McCall 1 month ago ↶
380
)
381
.bind ( registration)
382
.bind ( now)
383
.fetch_optional ( & mut * tx)
384
.await? ;
385
let Some ( ( who, scope_owner, scope_repo_id) ) = found else { return Ok ( None ) } ;
b6b7e4cf Andy McCall 1 month ago ↶
386
let id: i64 = sqlx:: query_scalar(
5d90e0e0 Andy McCall 20 days ago
387
" INSERT INTO flow_runners (name, token_hash, source, key, key_name, created_ms, created_by, scope_owner, scope_repo_id) VALUES ($1, $2, 'registered', $3, $4, $5, $6, $7, $8) RETURNING id" ,
b6b7e4cf Andy McCall 1 month ago ↶
388
)
389
.bind ( name)
390
.bind ( token_hash)
391
.bind ( key)
392
.bind ( key_name)
393
.bind ( now)
394
.bind ( if who.is_empty ( ) { by_default } else { & who } )
74be9796 Andy McCall 1 month ago ↶
395
.bind ( scope_owner)
396
.bind ( scope_repo_id)
b6b7e4cf Andy McCall 1 month ago ↶
397
.fetch_one ( & mut * tx)
398
.await? ;
5d90e0e0 Andy McCall 20 days ago
399
sqlx:: query( " UPDATE flow_runner_registrations SET used_ms = $1, runner_id = $2 WHERE token_hash = $3" )
b6b7e4cf Andy McCall 1 month ago ↶
400
.bind ( now)
401
.bind ( id)
402
.bind ( registration)
403
.execute ( & mut * tx)
404
.await? ;
405
tx.commit ( ) .await? ;
406
get ( pool, id) .await
407
}
408
409
410
# [ derive ( Clone, Debug, FromRow ) ]
411
pub struct RunnerJob {
412
pub owner : String,
413
pub repo : String,
414
pub run_number : i64 ,
415
pub job : String,
416
pub status : String,
417
pub started_ms : Option< i64 > ,
418
pub finished_ms : Option< i64 > ,
419
pub receipt_attest : String,
420
}
421
422
423
pub async fn history ( pool : & Pool, id : i64 , limit : i64 , before : Option< i64 > ) -> sqlx:: Result< Vec< RunnerJob> > {
424
sqlx:: query_as(
425
" 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, \
426
j.finished_ms AS finished_ms, j.receipt_attest AS receipt_attest \
427
FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id JOIN repositories p ON p.id = r.repo_id \
5d90e0e0 Andy McCall 20 days ago
428
WHERE j.runner_id = $1 AND ($2 IS NULL OR j.started_ms < $3) ORDER BY j.started_ms DESC LIMIT $4" ,
b6b7e4cf Andy McCall 1 month ago ↶
429
)
430
.bind ( id)
431
.bind ( before)
432
.bind ( before)
433
.bind ( limit)
434
.fetch_all ( pool)
435
.await
436
}
437
438
439
pub async fn totals ( pool : & Pool, id : i64 ) -> sqlx:: Result< ( i64 , i64 , i64 ) > {
440
sqlx:: query_as(
3b1bc27f Andy McCall 20 days ago
441
" SELECT COUNT(*), COALESCE(SUM(CASE WHEN status = 'passed' THEN 1 ELSE 0 END), 0), CAST(COALESCE(SUM(finished_ms - started_ms), 0) AS BIGINT) \
5d90e0e0 Andy McCall 20 days ago
442
FROM flow_jobs WHERE runner_id = $1 AND finished_ms IS NOT NULL" ,
b6b7e4cf Andy McCall 1 month ago ↶
443
)
444
.bind ( id)
445
.fetch_one ( pool)
446
.await
447
}
448
449
450
pub async fn running ( pool : & Pool) -> sqlx:: Result< Vec< ( i64 , i64 ) > > {
451
sqlx:: query_as( " SELECT runner_id, COUNT(*) FROM flow_jobs WHERE status = 'running' AND runner_id IS NOT NULL GROUP BY runner_id" )
452
.fetch_all ( pool)
453
.await
454
}
455
456
pub async fn set_job_runner ( pool : & Pool, job_id : i64 , runner_id : i64 ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
457
sqlx:: query( " UPDATE flow_jobs SET runner_id = $1 WHERE id = $2" ) .bind ( runner_id) .bind ( job_id) .execute ( pool) .await? ;
b6b7e4cf Andy McCall 1 month ago ↶
458
Ok ( ( ) )
459
}