| 1 | |
|
| 2 | |
|
| 3 | | use super::{now_ms, Pool}; |
| 4 | | use sqlx::FromRow; |
| 5 | |
|
| 6 | | #[derive(Clone, Debug, FromRow)] |
| 7 | | pub struct Run { |
| 8 | | pub id: i64, |
| 9 | | pub repo_id: i64, |
| 10 | | |
| 11 | | #[sqlx(default)] |
| 12 | | pub number: i64, |
| 13 | | |
| 14 | | |
| 15 | | #[sqlx(default)] |
| 16 | | pub trigger: String, |
| 17 | | pub bookmark: String, |
| 18 | | pub state: String, |
| 19 | | pub heads: String, |
| 20 | | pub pipeline_hash: String, |
| 21 | | pub status: String, |
| 22 | | pub error: String, |
| 23 | | pub actor: String, |
| 24 | | pub created_ms: i64, |
| 25 | | pub finished_ms: Option<i64>, |
| 26 | | |
| 27 | | |
| 28 | | #[sqlx(default)] |
| 29 | | pub priority: String, |
| 30 | | |
| 31 | | #[sqlx(default)] |
| 32 | | pub priority_note: String, |
| 33 | | } |
| 34 | |
|
| 35 | |
|
| 36 | | pub const PRIORITIES: [&str; 4] = ["urgent", "high", "normal", "low"]; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | | macro_rules! level_rank { |
| 42 | | () => { |
| 43 | | "CASE COALESCE(NULLIF(r.priority, ''), NULLIF(rep.flow_priority, ''), \ |
| 44 | | NULLIF((SELECT o.flow_priority FROM organisations o WHERE o.name = rep.owner), ''), \ |
| 45 | | NULLIF((SELECT s.value FROM settings s WHERE s.key = 'flow_priority'), ''), 'normal') \ |
| 46 | | WHEN 'urgent' THEN 0 WHEN 'high' THEN 1 WHEN 'low' THEN 3 ELSE 2 END" |
| 47 | | }; |
| 48 | | } |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | | pub const PRIORITY_RANK: &str = concat!( |
| 56 | | "CASE WHEN r.priority <> '' THEN ", |
| 57 | | level_rank!(), |
| 58 | | " WHEN j.priority = 'low' THEN 3 WHEN j.priority = 'normal' AND (", |
| 59 | | level_rank!(), |
| 60 | | ") < 2 THEN 2 ELSE ", |
| 61 | | level_rank!(), |
| 62 | | " END" |
| 63 | | ); |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | | pub const PRIORITY_NOTE: &str = |
| 68 | | "CASE WHEN r.priority <> '' THEN r.priority_note WHEN j.priority <> '' THEN 'pipeline: ' || j.priority ELSE r.priority_note END"; |
| 69 | |
|
| 70 | |
|
| 71 | 4× | pub async fn expedited_live(pool: &Pool, owner: &str) -> sqlx::Result<i64> { |
| 72 | 4× | sqlx::query_scalar( |
| 73 | 4× | "SELECT COUNT(*) FROM flow_runs r JOIN repositories rep ON rep.id = r.repo_id \ |
| 74 | 4× | WHERE rep.owner = $1 AND r.priority = 'urgent' AND r.status IN ('queued', 'running')", |
| 75 | 4× | ) |
| 76 | 4× | .bind(owner) |
| 77 | 4× | .fetch_one(pool) |
| 78 | 4× | .await |
| 79 | 4× | } |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | 4× | pub async fn expedite(pool: &Pool, run_id: i64, note: &str) -> sqlx::Result<bool> { |
| 84 | 4× | let r = sqlx::query("UPDATE flow_runs SET priority = 'urgent', priority_note = $1 WHERE id = $2 AND status IN ('queued', 'running')") |
| 85 | 4× | .bind(note) |
| 86 | 4× | .bind(run_id) |
| 87 | 4× | .execute(pool) |
| 88 | 4× | .await?; |
| 89 | 4× | Ok(r.rows_affected() > 0) |
| 90 | 4× | } |
| 91 | |
|
| 92 | | impl Run { |
| 93 | | |
| 94 | 23× | pub fn started_by(&self) -> String { |
| 95 | 23× | match self.trigger.as_str() { |
| 96 | 23× | "manual" => format!("started by {}", self.actor), |
| 97 | 21× | "schedule" => "started on schedule".to_string(), |
| 98 | 21× | t if t.starts_with("tag:") => format!("tag {} pushed by {}", &t[4..], self.actor), |
| 99 | 21× | t if t.starts_with("rerun:") => format!("re-run of #{} by {}", self.rerun_of().unwrap_or(""), self.actor), |
| 100 | 20× | _ => format!("pushed by {}", self.actor), |
| 101 | | } |
| 102 | 23× | } |
| 103 | |
|
| 104 | | |
| 105 | 16× | pub fn rerun_of(&self) -> Option<&str> { |
| 106 | 16× | let rest = self.trigger.strip_prefix("rerun:")?; |
| 107 | 2× | Some(rest.split(':').next().unwrap_or(rest)) |
| 108 | 16× | } |
| 109 | |
|
| 110 | | |
| 111 | 15× | pub fn trigger_label(&self) -> String { |
| 112 | 15× | match self.rerun_of() { |
| 113 | 1× | Some(n) => format!("rerun:{n}"), |
| 114 | 14× | None => self.trigger.clone(), |
| 115 | | } |
| 116 | 15× | } |
| 117 | |
|
| 118 | 413× | pub fn head_list(&self) -> Vec<&str> { |
| 119 | 545× | self.heads.split('+').filter(|h| !h.is_empty()).collect() |
| 120 | 413× | } |
| 121 | |
|
| 122 | 179× | pub fn live(&self) -> bool { |
| 123 | 179× | matches!(self.status.as_str(), "queued" | "running") |
| 124 | 179× | } |
| 125 | | } |
| 126 | |
|
| 127 | | #[derive(Clone, Debug, Default, FromRow)] |
| 128 | | pub struct Job { |
| 129 | | pub id: i64, |
| 130 | | pub run_id: i64, |
| 131 | | pub name: String, |
| 132 | | pub depth: i64, |
| 133 | | pub depends: String, |
| 134 | | pub spec: String, |
| 135 | | pub sandbox: String, |
| 136 | | pub runtime: String, |
| 137 | | pub status: String, |
| 138 | | pub runner: String, |
| 139 | | pub token_hash: String, |
| 140 | | pub started_ms: Option<i64>, |
| 141 | | pub finished_ms: Option<i64>, |
| 142 | | pub exit_code: Option<i64>, |
| 143 | | pub error: String, |
| 144 | | pub receipt: String, |
| 145 | | pub receipt_hash: String, |
| 146 | | pub log_count: i64, |
| 147 | | |
| 148 | | pub also_read: String, |
| 149 | | |
| 150 | | pub receipt_attest: String, |
| 151 | | |
| 152 | | pub receipt_signer: String, |
| 153 | | |
| 154 | | pub receipt_note: String, |
| 155 | | |
| 156 | | |
| 157 | | pub waiting: String, |
| 158 | | |
| 159 | | pub peak_disk_mib: Option<i64>, |
| 160 | | pub peak_memory_mib: Option<i64>, |
| 161 | | |
| 162 | | #[sqlx(default)] |
| 163 | | pub phases: String, |
| 164 | | |
| 165 | | pub need_disk_mib: Option<i64>, |
| 166 | | |
| 167 | | pub placed: String, |
| 168 | | |
| 169 | | pub approval: String, |
| 170 | | |
| 171 | | pub approved_by: String, |
| 172 | | pub approved_ms: Option<i64>, |
| 173 | | |
| 174 | | #[sqlx(default)] |
| 175 | | pub approved_via: String, |
| 176 | | |
| 177 | | pub environment: String, |
| 178 | | |
| 179 | | pub approval_seal: String, |
| 180 | | |
| 181 | | |
| 182 | | #[sqlx(default)] |
| 183 | | pub queued_ms: Option<i64>, |
| 184 | | #[sqlx(default)] |
| 185 | | pub ready_ms: Option<i64>, |
| 186 | | |
| 187 | | |
| 188 | | |
| 189 | | #[sqlx(default)] |
| 190 | | pub handed_back_ms: Option<i64>, |
| 191 | | #[sqlx(default)] |
| 192 | | pub handbacks: i64, |
| 193 | | } |
| 194 | |
|
| 195 | | impl Job { |
| 196 | | |
| 197 | | |
| 198 | 54× | pub fn phase_list(&self) -> Vec<(String, u64)> { |
| 199 | 54× | let m: std::collections::BTreeMap<String, u64> = serde_json::from_str(&self.phases).unwrap_or_default(); |
| 200 | 216× | ["image", "prepare", "run", "finish"].iter().filter_map(|p| m.get(*p).map(|ms| (p.to_string(), *ms))).collect() |
| 201 | 54× | } |
| 202 | |
|
| 203 | | |
| 204 | | |
| 205 | 156× | pub fn reused_from(&self) -> Option<i64> { |
| 206 | 156× | self.error.strip_prefix("reused from ")?.rsplit_once("run #")?.1.parse().ok() |
| 207 | 156× | } |
| 208 | |
|
| 209 | | |
| 210 | | |
| 211 | 118× | pub fn reused_repo(&self) -> Option<&str> { |
| 212 | 118× | let rest = self.error.strip_prefix("reused from ")?; |
| 213 | 30× | let (repo, n) = rest.rsplit_once(" run #")?; |
| 214 | 18× | n.parse::<i64>().ok()?; |
| 215 | 18× | Some(repo).filter(|r| !r.is_empty()) |
| 216 | 118× | } |
| 217 | |
|
| 218 | 424× | pub fn depends_on(&self) -> Vec<String> { |
| 219 | 424× | serde_json::from_str(&self.depends).unwrap_or_default() |
| 220 | 424× | } |
| 221 | |
|
| 222 | 252× | pub fn done(&self) -> bool { |
| 223 | 252× | matches!(self.status.as_str(), "passed" | "failed" | "skipped" | "error" | "cancelled") |
| 224 | 252× | } |
| 225 | | } |
| 226 | |
|
| 227 | | #[derive(Clone, Debug, FromRow)] |
| 228 | | pub struct LogLine { |
| 229 | | pub seq: i64, |
| 230 | | pub t: i64, |
| 231 | | pub stream: String, |
| 232 | | pub text: String, |
| 233 | | } |
| 234 | |
|
| 235 | |
|
| 236 | | pub struct NewJob { |
| 237 | | pub name: String, |
| 238 | | pub depth: i64, |
| 239 | | pub depends: Vec<String>, |
| 240 | | pub spec: String, |
| 241 | | pub sandbox: String, |
| 242 | | pub runtime: String, |
| 243 | | |
| 244 | | pub need_disk_mib: Option<i64>, |
| 245 | | |
| 246 | | |
| 247 | | pub reused: Option<Reused>, |
| 248 | | } |
| 249 | |
|
| 250 | |
|
| 251 | | #[derive(Clone, Debug)] |
| 252 | | pub struct Reused { |
| 253 | | |
| 254 | | pub repo: Option<String>, |
| 255 | | |
| 256 | | pub run_number: i64, |
| 257 | | pub receipt: String, |
| 258 | | pub receipt_hash: String, |
| 259 | | pub receipt_attest: String, |
| 260 | | pub receipt_signer: String, |
| 261 | | pub receipt_note: String, |
| 262 | | } |
| 263 | |
|
| 264 | | impl Reused { |
| 265 | | |
| 266 | 17× | pub fn note(&self) -> String { |
| 267 | 17× | match &self.repo { |
| 268 | 8× | Some(r) => format!("reused from {r} run #{}", self.run_number), |
| 269 | 9× | None => format!("reused from run #{}", self.run_number), |
| 270 | | } |
| 271 | 17× | } |
| 272 | | } |
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | 306× | pub async fn passed_on_state( |
| 277 | 306× | pool: &Pool, |
| 278 | 306× | repo_id: i64, |
| 279 | 306× | state: &str, |
| 280 | 306× | name: &str, |
| 281 | 306× | spec: &str, |
| 282 | 306× | sandbox: &str, |
| 283 | 306× | ) -> sqlx::Result<Option<Reused>> { |
| 284 | 306× | passed_in(pool, repo_id, state, name, spec, sandbox, false).await |
| 285 | 306× | } |
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | 312× | async fn passed_in( |
| 291 | 312× | pool: &Pool, |
| 292 | 312× | repo_id: i64, |
| 293 | 312× | state: &str, |
| 294 | 312× | name: &str, |
| 295 | 312× | spec: &str, |
| 296 | 312× | sandbox: &str, |
| 297 | 312× | ran_here: bool, |
| 298 | 312× | ) -> sqlx::Result<Option<Reused>> { |
| 299 | 312× | let row: Option<(i64, String, String, String, String, String)> = sqlx::query_as( |
| 300 | 312× | "SELECT r.number, j.receipt, j.receipt_hash, j.receipt_attest, j.receipt_signer, j.receipt_note \ |
| 301 | 312× | FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id \ |
| 302 | 312× | WHERE r.repo_id = $1 AND r.state = $2 AND j.name = $3 AND j.spec = $4 AND j.sandbox = $5 AND j.status = 'passed' \ |
| 303 | 312× | AND (NOT $6 OR j.error NOT LIKE 'reused from %') \ |
| 304 | 312× | ORDER BY j.id DESC LIMIT 1", |
| 305 | 312× | ) |
| 306 | 312× | .bind(repo_id) |
| 307 | 312× | .bind(state) |
| 308 | 312× | .bind(name) |
| 309 | 312× | .bind(spec) |
| 310 | 312× | .bind(sandbox) |
| 311 | 312× | .bind(ran_here) |
| 312 | 312× | .fetch_optional(pool) |
| 313 | 312× | .await?; |
| 314 | 312× | Ok(row.map(|(run_number, receipt, receipt_hash, receipt_attest, receipt_signer, receipt_note)| Reused { |
| 315 | 15× | repo: None, |
| 316 | 15× | run_number, |
| 317 | 15× | receipt, |
| 318 | 15× | receipt_hash, |
| 319 | 15× | receipt_attest, |
| 320 | 15× | receipt_signer, |
| 321 | 15× | receipt_note, |
| 322 | 15× | })) |
| 323 | 312× | } |
| 324 | |
|
| 325 | |
|
| 326 | | const UPSTREAM_DEPTH: usize = 5; |
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | 6× | pub async fn passed_upstream( |
| 334 | 6× | pool: &Pool, |
| 335 | 6× | repo: &super::repos::RepoRow, |
| 336 | 6× | state: &str, |
| 337 | 6× | name: &str, |
| 338 | 6× | spec: &str, |
| 339 | 6× | sandbox: &str, |
| 340 | 6× | ) -> sqlx::Result<Option<Reused>> { |
| 341 | 6× | let mut parent = repo.forked_from; |
| 342 | 6× | for _ in 0..UPSTREAM_DEPTH { |
| 343 | 6× | let Some(id) = parent else { break }; |
| 344 | 6× | let Some(up) = super::repos::get_by_id(pool, id).await? else { break }; |
| 345 | 6× | parent = up.forked_from; |
| 346 | 6× | if !(up.visibility == "public" || up.owner == repo.owner) { |
| 347 | 0× | continue; |
| 348 | 6× | } |
| 349 | 6× | if let Some(mut r) = passed_in(pool, up.id, state, name, spec, sandbox, true).await? { |
| 350 | 6× | r.repo = Some(format!("{}/{}", up.owner, up.name)); |
| 351 | 6× | return Ok(Some(r)); |
| 352 | 0× | } |
| 353 | | } |
| 354 | 0× | Ok(None) |
| 355 | 6× | } |
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | 291× | pub async fn passed_elsewhere( |
| 363 | 291× | pool: &Pool, |
| 364 | 291× | repo: &super::repos::RepoRow, |
| 365 | 291× | state: &str, |
| 366 | 291× | name: &str, |
| 367 | 291× | spec: &str, |
| 368 | 291× | sandbox: &str, |
| 369 | 291× | ) -> sqlx::Result<Option<Reused>> { |
| 370 | | #[derive(FromRow)] |
| 371 | | struct Pass { |
| 372 | | owner: String, |
| 373 | | name: String, |
| 374 | | number: i64, |
| 375 | | receipt: String, |
| 376 | | receipt_hash: String, |
| 377 | | receipt_attest: String, |
| 378 | | receipt_signer: String, |
| 379 | | receipt_note: String, |
| 380 | | } |
| 381 | 291× | let pass: Option<Pass> = sqlx::query_as( |
| 382 | 291× | "SELECT o.owner, o.name, r.number, j.receipt, j.receipt_hash, j.receipt_attest, j.receipt_signer, j.receipt_note \ |
| 383 | 291× | FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id JOIN repositories o ON o.id = r.repo_id \ |
| 384 | 291× | WHERE r.state = $1 AND j.name = $2 AND j.spec = $3 AND j.sandbox = $4 AND j.status = 'passed' \ |
| 385 | 291× | AND o.id <> $5 AND (o.visibility = 'public' OR o.owner = $6) AND j.error NOT LIKE 'reused from %' \ |
| 386 | 291× | ORDER BY j.id DESC LIMIT 1", |
| 387 | 291× | ) |
| 388 | 291× | .bind(state) |
| 389 | 291× | .bind(name) |
| 390 | 291× | .bind(spec) |
| 391 | 291× | .bind(sandbox) |
| 392 | 291× | .bind(repo.id) |
| 393 | 291× | .bind(&repo.owner) |
| 394 | 291× | .fetch_optional(pool) |
| 395 | 291× | .await?; |
| 396 | 291× | Ok(pass.map(|p| Reused { |
| 397 | 3× | repo: Some(format!("{}/{}", p.owner, p.name)), |
| 398 | 3× | run_number: p.number, |
| 399 | 3× | receipt: p.receipt, |
| 400 | 3× | receipt_hash: p.receipt_hash, |
| 401 | 3× | receipt_attest: p.receipt_attest, |
| 402 | 3× | receipt_signer: p.receipt_signer, |
| 403 | 3× | receipt_note: p.receipt_note, |
| 404 | 3× | })) |
| 405 | 291× | } |
| 406 | |
|
| 407 | |
|
| 408 | | const NEED_FROM_RUNS: i64 = 5; |
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | 375× | pub async fn learned_disk_need(pool: &Pool, repo_id: i64, name: &str) -> sqlx::Result<Option<i64>> { |
| 414 | 375× | let peak: Option<i64> = sqlx::query_scalar( |
| 415 | 375× | "SELECT MAX(peak_disk_mib) FROM (SELECT j.peak_disk_mib FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id \ |
| 416 | 375× | WHERE r.repo_id = $1 AND j.name = $2 AND j.peak_disk_mib IS NOT NULL ORDER BY j.id DESC LIMIT $3) recent", |
| 417 | 375× | ) |
| 418 | 375× | .bind(repo_id) |
| 419 | 375× | .bind(name) |
| 420 | 375× | .bind(NEED_FROM_RUNS) |
| 421 | 375× | .fetch_one(pool) |
| 422 | 375× | .await?; |
| 423 | 375× | Ok(peak.map(|p| p + p / 4 + 64)) |
| 424 | 375× | } |
| 425 | |
|
| 426 | |
|
| 427 | 159× | pub async fn set_placed(pool: &Pool, job_id: i64, placed: &str) -> sqlx::Result<()> { |
| 428 | 159× | sqlx::query("UPDATE flow_jobs SET placed = $1 WHERE id = $2").bind(placed).bind(job_id).execute(pool).await?; |
| 429 | 159× | Ok(()) |
| 430 | 159× | } |
| 431 | |
|
| 432 | |
|
| 433 | 98× | pub async fn set_peaks(pool: &Pool, job_id: i64, disk_mib: Option<u64>, memory_mib: Option<u64>) -> sqlx::Result<()> { |
| 434 | 98× | sqlx::query("UPDATE flow_jobs SET peak_disk_mib = $1, peak_memory_mib = $2 WHERE id = $3") |
| 435 | 98× | .bind(disk_mib.map(|v| v.min(i64::MAX as u64) as i64)) |
| 436 | 98× | .bind(memory_mib.map(|v| v.min(i64::MAX as u64) as i64)) |
| 437 | 98× | .bind(job_id) |
| 438 | 98× | .execute(pool) |
| 439 | 98× | .await?; |
| 440 | 98× | Ok(()) |
| 441 | 98× | } |
| 442 | |
|
| 443 | |
|
| 444 | 98× | pub async fn set_phases(pool: &Pool, job_id: i64, phases: &std::collections::BTreeMap<String, u64>) -> sqlx::Result<()> { |
| 445 | 98× | let json = if phases.is_empty() { String::new() } else { serde_json::to_string(phases).unwrap_or_default() }; |
| 446 | 98× | sqlx::query("UPDATE flow_jobs SET phases = $1 WHERE id = $2").bind(json).bind(job_id).execute(pool).await?; |
| 447 | 98× | Ok(()) |
| 448 | 98× | } |
| 449 | |
|
| 450 | | pub struct NewRun<'a> { |
| 451 | | pub repo_id: i64, |
| 452 | | pub bookmark: &'a str, |
| 453 | | pub state: &'a str, |
| 454 | | pub heads: &'a str, |
| 455 | | pub pipeline_hash: &'a str, |
| 456 | | pub actor: &'a str, |
| 457 | | |
| 458 | | |
| 459 | | pub trigger: &'a str, |
| 460 | | |
| 461 | | pub error: Option<&'a str>, |
| 462 | | } |
| 463 | |
|
| 464 | |
|
| 465 | |
|
| 466 | 1× | pub async fn create_skipped_run(pool: &Pool, r: NewRun<'_>, why: &str) -> sqlx::Result<i64> { |
| 467 | 1× | let mut tx = super::begin_write(pool).await?; |
| 468 | 1× | let now = now_ms(); |
| 469 | 1× | let id: i64 = sqlx::query_scalar( |
| 470 | 1× | "INSERT INTO flow_runs (repo_id, number, bookmark, state, heads, pipeline_hash, status, error, actor, trigger, created_ms, finished_ms) \ |
| 471 | 1× | VALUES ($1, (SELECT COALESCE(MAX(number), 0) + 1 FROM flow_runs WHERE repo_id = $2), $3, $4, $5, $6, 'skipped', $7, $8, $9, $10, $11) RETURNING id", |
| 472 | 1× | ) |
| 473 | 1× | .bind(r.repo_id) |
| 474 | 1× | .bind(r.repo_id) |
| 475 | 1× | .bind(r.bookmark) |
| 476 | 1× | .bind(r.state) |
| 477 | 1× | .bind(r.heads) |
| 478 | 1× | .bind(r.pipeline_hash) |
| 479 | 1× | .bind(why) |
| 480 | 1× | .bind(r.actor) |
| 481 | 1× | .bind(r.trigger) |
| 482 | 1× | .bind(now) |
| 483 | 1× | .bind(now) |
| 484 | 1× | .fetch_one(&mut *tx) |
| 485 | 1× | .await?; |
| 486 | 1× | tx.commit().await?; |
| 487 | 1× | Ok(id) |
| 488 | 1× | } |
| 489 | |
|
| 490 | 145× | pub async fn create_run(pool: &Pool, r: NewRun<'_>, jobs: &[NewJob]) -> sqlx::Result<i64> { |
| 491 | 145× | let mut tx = super::begin_write(pool).await?; |
| 492 | 145× | let now = now_ms(); |
| 493 | 145× | let status = if r.error.is_some() { "error" } else { "queued" }; |
| 494 | 145× | let id: i64 = sqlx::query_scalar( |
| 495 | 145× | "INSERT INTO flow_runs (repo_id, number, bookmark, state, heads, pipeline_hash, status, error, actor, trigger, created_ms, finished_ms) \ |
| 496 | 145× | VALUES ($1, (SELECT COALESCE(MAX(number), 0) + 1 FROM flow_runs WHERE repo_id = $2), $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING id", |
| 497 | | ) |
| 498 | 145× | .bind(r.repo_id) |
| 499 | 145× | .bind(r.repo_id) |
| 500 | 145× | .bind(r.bookmark) |
| 501 | 145× | .bind(r.state) |
| 502 | 145× | .bind(r.heads) |
| 503 | 145× | .bind(r.pipeline_hash) |
| 504 | 145× | .bind(status) |
| 505 | 145× | .bind(r.error.unwrap_or("")) |
| 506 | 145× | .bind(r.actor) |
| 507 | 145× | .bind(r.trigger) |
| 508 | 145× | .bind(now) |
| 509 | 145× | .bind(r.error.map(|_| now)) |
| 510 | 145× | .fetch_one(&mut *tx) |
| 511 | 145× | .await?; |
| 512 | 379× | for j in jobs { |
| 513 | 379× | let job_id: i64 = sqlx::query_scalar( |
| 514 | 379× | "INSERT INTO flow_jobs (run_id, name, depth, depends, spec, sandbox, runtime, status, need_disk_mib, approval, environment, priority, queued_ms) \ |
| 515 | 379× | VALUES ($1, $2, $3, $4, $5, $6, $7, 'queued', $8, $9, $10, $11, $12) RETURNING id", |
| 516 | | ) |
| 517 | 379× | .bind(id) |
| 518 | 379× | .bind(&j.name) |
| 519 | 379× | .bind(j.depth) |
| 520 | 379× | .bind(serde_json::to_string(&j.depends).unwrap_or_else(|_| "[]".into())) |
| 521 | 379× | .bind(&j.spec) |
| 522 | 379× | .bind(&j.sandbox) |
| 523 | 379× | .bind(&j.runtime) |
| 524 | 379× | .bind(j.need_disk_mib) |
| 525 | 379× | .bind(if needs_approval(&j.spec) { "waiting" } else { "" }) |
| 526 | 379× | .bind(environment_of(&j.spec)) |
| 527 | 379× | .bind(priority_of(&j.spec)) |
| 528 | 379× | .bind(now) |
| 529 | 379× | .fetch_one(&mut *tx) |
| 530 | 379× | .await?; |
| 531 | | |
| 532 | 379× | if let Some(r) = &j.reused { |
| 533 | 17× | sqlx::query( |
| 534 | 17× | "UPDATE flow_jobs SET status = 'passed', exit_code = 0, error = $1, receipt = $2, receipt_hash = $3, receipt_attest = $4, \ |
| 535 | 17× | receipt_signer = $5, receipt_note = $6, started_ms = $7, finished_ms = $7 WHERE id = $8", |
| 536 | 17× | ) |
| 537 | 17× | .bind(r.note()) |
| 538 | 17× | .bind(&r.receipt) |
| 539 | 17× | .bind(&r.receipt_hash) |
| 540 | 17× | .bind(&r.receipt_attest) |
| 541 | 17× | .bind(&r.receipt_signer) |
| 542 | 17× | .bind(&r.receipt_note) |
| 543 | 17× | .bind(now) |
| 544 | 17× | .bind(job_id) |
| 545 | 17× | .execute(&mut *tx) |
| 546 | 17× | .await?; |
| 547 | 362× | } |
| 548 | | } |
| 549 | | |
| 550 | 154× | if !jobs.is_empty() && jobs.iter().all(|j| j.reused.is_some()) { |
| 551 | 4× | sqlx::query("UPDATE flow_runs SET status = 'passed', finished_ms = $1 WHERE id = $2").bind(now).bind(id).execute(&mut *tx).await?; |
| 552 | 141× | } |
| 553 | 145× | tx.commit().await?; |
| 554 | 145× | Ok(id) |
| 555 | 145× | } |
| 556 | |
|
| 557 | |
|
| 558 | 379× | fn environment_of(spec: &str) -> String { |
| 559 | 379× | serde_json::from_str::<serde_json::Value>(spec).ok().and_then(|v| v["environment"].as_str().map(String::from)).unwrap_or_default() |
| 560 | 379× | } |
| 561 | |
|
| 562 | |
|
| 563 | |
|
| 564 | 379× | fn priority_of(spec: &str) -> String { |
| 565 | 379× | serde_json::from_str::<serde_json::Value>(spec) |
| 566 | 379× | .ok() |
| 567 | 379× | .and_then(|v| v["priority"].as_str().map(String::from)) |
| 568 | 379× | .filter(|p| p == "low" || p == "normal") |
| 569 | 379× | .unwrap_or_default() |
| 570 | 379× | } |
| 571 | |
|
| 572 | |
|
| 573 | 379× | fn needs_approval(spec: &str) -> bool { |
| 574 | 379× | serde_json::from_str::<serde_json::Value>(spec).is_ok_and(|v| !v["approval"].is_null()) |
| 575 | 379× | } |
| 576 | |
|
| 577 | |
|
| 578 | 211× | fn labels_in(spec: &str, sandbox: &str) -> Vec<String> { |
| 579 | 211× | let mut out: Vec<String> = [spec, sandbox] |
| 580 | 211× | .iter() |
| 581 | 422× | .filter_map(|j| serde_json::from_str::<serde_json::Value>(j).ok()) |
| 582 | 422× | .flat_map(|v| v["runs_on"].as_array().cloned().unwrap_or_default()) |
| 583 | 211× | .filter_map(|l| l.as_str().map(String::from)) |
| 584 | 211× | .collect(); |
| 585 | 211× | out.sort(); |
| 586 | 211× | out.dedup(); |
| 587 | 211× | out |
| 588 | 211× | } |
| 589 | |
|
| 590 | | impl Job { |
| 591 | | |
| 592 | 200× | pub fn needs(&self) -> Vec<String> { |
| 593 | 200× | labels_in(&self.spec, &self.sandbox) |
| 594 | 200× | } |
| 595 | |
|
| 596 | | |
| 597 | 38× | pub fn arch(&self) -> Option<String> { |
| 598 | 38× | arch_in(&self.sandbox) |
| 599 | 38× | } |
| 600 | | } |
| 601 | |
|
| 602 | |
|
| 603 | 48× | fn arch_in(sandbox: &str) -> Option<String> { |
| 604 | 48× | serde_json::from_str::<serde_json::Value>(sandbox).ok()?["arch"].as_str().map(String::from) |
| 605 | 48× | } |
| 606 | |
|
| 607 | |
|
| 608 | | #[derive(Clone, Debug, FromRow)] |
| 609 | | pub struct QueueEntry { |
| 610 | | pub job_id: i64, |
| 611 | | pub run_id: i64, |
| 612 | | pub repo_id: i64, |
| 613 | | pub owner: String, |
| 614 | | pub name: String, |
| 615 | | pub visibility: String, |
| 616 | | pub run_number: i64, |
| 617 | | pub job: String, |
| 618 | | pub status: String, |
| 619 | | |
| 620 | | pub runner: String, |
| 621 | | pub spec: String, |
| 622 | | pub sandbox: String, |
| 623 | | |
| 624 | | pub started_ms: Option<i64>, |
| 625 | | |
| 626 | | |
| 627 | | pub priority_rank: i64, |
| 628 | | pub priority_note: String, |
| 629 | | } |
| 630 | |
|
| 631 | | impl QueueEntry { |
| 632 | 22× | pub fn priority(&self) -> &'static str { |
| 633 | 22× | PRIORITIES.get(self.priority_rank as usize).copied().unwrap_or("normal") |
| 634 | 22× | } |
| 635 | | } |
| 636 | |
|
| 637 | | impl QueueEntry { |
| 638 | | |
| 639 | 11× | pub fn needs(&self) -> Vec<String> { |
| 640 | 11× | labels_in(&self.spec, &self.sandbox) |
| 641 | 11× | } |
| 642 | |
|
| 643 | | |
| 644 | 10× | pub fn arch(&self) -> Option<String> { |
| 645 | 10× | arch_in(&self.sandbox) |
| 646 | 10× | } |
| 647 | | } |
| 648 | |
|
| 649 | |
|
| 650 | |
|
| 651 | 273× | pub async fn queue(pool: &Pool) -> sqlx::Result<Vec<QueueEntry>> { |
| 652 | 273× | sqlx::query_as(&format!( |
| 653 | 273× | "SELECT j.id AS job_id, j.run_id, r.repo_id, rep.owner, rep.name, rep.visibility, r.number AS run_number, j.name AS job, j.status, j.runner, j.spec, j.sandbox, j.started_ms, \ |
| 654 | 273× | CAST({PRIORITY_RANK} AS BIGINT) AS priority_rank, {PRIORITY_NOTE} AS priority_note \ |
| 655 | 273× | FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id JOIN repositories rep ON rep.id = r.repo_id \ |
| 656 | 273× | WHERE j.status IN ('queued', 'running') AND j.approval != 'waiting' AND r.status IN ('queued', 'running') \ |
| 657 | 273× | ORDER BY CASE j.status WHEN 'running' THEN 0 ELSE 1 END, \ |
| 658 | 273× | CASE j.status WHEN 'running' THEN 0 ELSE {PRIORITY_RANK} END, \ |
| 659 | 273× | CASE j.status WHEN 'running' THEN 0 ELSE ROW_NUMBER() OVER (PARTITION BY r.repo_id, j.status ORDER BY j.run_id, j.depth, j.id) \ |
| 660 | 273× | + (SELECT COUNT(*) FROM flow_jobs x JOIN flow_runs xr ON xr.id = x.run_id WHERE x.status = 'running' AND xr.repo_id = r.repo_id) END, \ |
| 661 | 273× | j.run_id, j.depth, j.id" |
| 662 | 273× | )) |
| 663 | 273× | .fetch_all(pool) |
| 664 | 273× | .await |
| 665 | 273× | } |
| 666 | |
|
| 667 | |
|
| 668 | 2× | pub async fn runs_by_number(pool: &Pool, repo_id: i64, limit: i64, before: Option<i64>) -> sqlx::Result<Vec<Run>> { |
| 669 | 2× | sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = $1 AND number < $2 ORDER BY number DESC LIMIT $3") |
| 670 | 2× | .bind(repo_id) |
| 671 | 2× | .bind(before.unwrap_or(i64::MAX)) |
| 672 | 2× | .bind(limit) |
| 673 | 2× | .fetch_all(pool) |
| 674 | 2× | .await |
| 675 | 2× | } |
| 676 | |
|
| 677 | |
|
| 678 | |
|
| 679 | |
|
| 680 | |
|
| 681 | |
|
| 682 | 152× | pub async fn latest_on(pool: &Pool, repo_id: i64, bookmark: &str) -> sqlx::Result<Option<Run>> { |
| 683 | 152× | sqlx::query_as( |
| 684 | 152× | "SELECT * FROM flow_runs WHERE repo_id = $1 AND bookmark = $2 AND heads != '' AND status NOT IN ('cancelled', 'skipped') |
| 685 | 152× | AND NOT (trigger = 'push' AND status IN ('queued', 'running')) ORDER BY id DESC LIMIT 1", |
| 686 | 152× | ) |
| 687 | 152× | .bind(repo_id) |
| 688 | 152× | .bind(bookmark) |
| 689 | 152× | .fetch_optional(pool) |
| 690 | 152× | .await |
| 691 | 152× | } |
| 692 | |
|
| 693 | 295× | pub async fn runs(pool: &Pool, repo_id: i64, limit: i64, before: Option<i64>) -> sqlx::Result<Vec<Run>> { |
| 694 | 295× | sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = $1 AND id < $2 ORDER BY id DESC LIMIT $3") |
| 695 | 295× | .bind(repo_id) |
| 696 | 295× | .bind(before.unwrap_or(i64::MAX)) |
| 697 | 295× | .bind(limit) |
| 698 | 295× | .fetch_all(pool) |
| 699 | 295× | .await |
| 700 | 295× | } |
| 701 | |
|
| 702 | |
|
| 703 | 96× | pub async fn run(pool: &Pool, repo_id: i64, number: i64) -> sqlx::Result<Option<Run>> { |
| 704 | 96× | sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = $1 AND number = $2").bind(repo_id).bind(number).fetch_optional(pool).await |
| 705 | 96× | } |
| 706 | |
|
| 707 | 1005× | pub async fn run_by_id(pool: &Pool, id: i64) -> sqlx::Result<Option<Run>> { |
| 708 | 1005× | sqlx::query_as("SELECT * FROM flow_runs WHERE id = $1").bind(id).fetch_optional(pool).await |
| 709 | 1005× | } |
| 710 | |
|
| 711 | 281× | pub async fn jobs(pool: &Pool, run_id: i64) -> sqlx::Result<Vec<Job>> { |
| 712 | 281× | sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = $1 ORDER BY depth, name").bind(run_id).fetch_all(pool).await |
| 713 | 281× | } |
| 714 | |
|
| 715 | 487× | pub async fn job(pool: &Pool, id: i64) -> sqlx::Result<Option<Job>> { |
| 716 | 487× | sqlx::query_as("SELECT * FROM flow_jobs WHERE id = $1").bind(id).fetch_optional(pool).await |
| 717 | 487× | } |
| 718 | |
|
| 719 | |
|
| 720 | 31× | pub async fn job_by_token(pool: &Pool, token_hash: &str, max_age_ms: i64) -> sqlx::Result<Option<Job>> { |
| 721 | 31× | sqlx::query_as("SELECT * FROM flow_jobs WHERE token_hash = $1 AND status = 'running' AND started_ms >= $2") |
| 722 | 31× | .bind(token_hash) |
| 723 | 31× | .bind(now_ms() - max_age_ms) |
| 724 | 31× | .fetch_optional(pool) |
| 725 | 31× | .await |
| 726 | 31× | } |
| 727 | |
|
| 728 | |
|
| 729 | |
|
| 730 | |
|
| 731 | 28× | pub async fn set_also_read(pool: &Pool, job_id: i64, repo_ids: &[i64]) -> sqlx::Result<()> { |
| 732 | 29× | let list: Vec<String> = repo_ids.iter().map(|i| i.to_string()).collect(); |
| 733 | 28× | sqlx::query("UPDATE flow_jobs SET also_read = $1 WHERE id = $2").bind(list.join(",")).bind(job_id).execute(pool).await?; |
| 734 | 28× | Ok(()) |
| 735 | 28× | } |
| 736 | |
|
| 737 | | impl Job { |
| 738 | 10× | pub fn also_read_ids(&self) -> Vec<i64> { |
| 739 | 15× | self.also_read.split(',').filter_map(|x| x.trim().parse().ok()).collect() |
| 740 | 10× | } |
| 741 | | } |
| 742 | |
|
| 743 | |
|
| 744 | |
|
| 745 | | #[derive(Clone, Debug, Default)] |
| 746 | | pub struct Room { |
| 747 | | pub memory_mib: Option<u32>, |
| 748 | | pub cpus: Option<u32>, |
| 749 | | pub images: Option<std::collections::HashSet<String>>, |
| 750 | | |
| 751 | | pub arch: String, |
| 752 | | |
| 753 | | pub image_archs: std::collections::HashMap<String, String>, |
| 754 | | |
| 755 | | pub declined: std::collections::HashSet<i64>, |
| 756 | | |
| 757 | | |
| 758 | | pub fetchable: std::collections::HashSet<String>, |
| 759 | | |
| 760 | | |
| 761 | | pub free_disk_mib: Option<u64>, |
| 762 | | } |
| 763 | |
|
| 764 | | impl Room { |
| 765 | | |
| 766 | 171× | pub fn disk_fits(&self, need: Option<i64>) -> bool { |
| 767 | 171× | match (need, self.free_disk_mib) { |
| 768 | 2× | (Some(n), Some(free)) => n.max(0) as u64 <= free, |
| 769 | 169× | _ => true, |
| 770 | | } |
| 771 | 171× | } |
| 772 | | } |
| 773 | |
|
| 774 | | impl Room { |
| 775 | | |
| 776 | | |
| 777 | 180× | pub fn fits(&self, sandbox_json: &str) -> bool { |
| 778 | 180× | let v: serde_json::Value = serde_json::from_str(sandbox_json).unwrap_or_default(); |
| 779 | 180× | let need = |k: &str| v[k].as_u64().unwrap_or(0); |
| 780 | 180× | let rootfs = v["rootfs"].as_str().unwrap_or(""); |
| 781 | | |
| 782 | | |
| 783 | | |
| 784 | 180× | let wanted = v["arch"].as_str().map(String::from).or_else(|| self.image_archs.get(rootfs).cloned()); |
| 785 | 180× | let arch_ok = self.arch.is_empty() || wanted.as_deref().is_none_or(|w| w == self.arch); |
| 786 | 180× | arch_ok |
| 787 | 175× | && self.memory_mib.is_none_or(|m| need("memory_mib") <= m as u64) |
| 788 | 175× | && self.cpus.is_none_or(|c| need("cpus") <= c as u64) |
| 789 | 175× | && (rootfs.is_empty() || self.images.as_ref().is_none_or(|have| have.contains(rootfs)) || self.fetchable.contains(rootfs)) |
| 790 | 180× | } |
| 791 | | } |
| 792 | |
|
| 793 | |
|
| 794 | 12× | pub async fn record_image_archs(pool: &Pool, archs: &std::collections::BTreeMap<String, String>) -> sqlx::Result<()> { |
| 795 | 12× | for (hash, arch) in archs.iter().take(500) { |
| 796 | 10× | if hash.len() != 64 || !flow_core::arch_ok(arch) { |
| 797 | 0× | continue; |
| 798 | 10× | } |
| 799 | 10× | sqlx::query("INSERT INTO flow_images (hash, arch, seen_ms) VALUES ($1, $2, $3) ON CONFLICT (hash) DO UPDATE SET arch = excluded.arch, seen_ms = excluded.seen_ms") |
| 800 | 10× | .bind(hash.to_ascii_lowercase()) |
| 801 | 10× | .bind(arch) |
| 802 | 10× | .bind(now_ms()) |
| 803 | 10× | .execute(pool) |
| 804 | 10× | .await?; |
| 805 | | } |
| 806 | 12× | Ok(()) |
| 807 | 12× | } |
| 808 | |
|
| 809 | |
|
| 810 | |
|
| 811 | |
|
| 812 | 200× | pub async fn learn_archs_from_runs(pool: &Pool) -> sqlx::Result<u64> { |
| 813 | 200× | let rootfs = crate::db::json_text("j.sandbox", "rootfs"); |
| 814 | 200× | let r = sqlx::query(&format!( |
| 815 | 200× | "INSERT INTO flow_images (hash, arch, seen_ms) |
| 816 | 200× | SELECT lower({rootfs}), MIN(r.arch), $1 FROM flow_jobs j JOIN flow_runners r ON r.id = j.runner_id |
| 817 | 200× | WHERE j.status = 'passed' AND r.arch != '' AND length({rootfs}) = 64 |
| 818 | 200× | GROUP BY lower({rootfs}) |
| 819 | 200× | ON CONFLICT (hash) DO NOTHING" |
| 820 | 200× | )) |
| 821 | 200× | .bind(now_ms()) |
| 822 | 200× | .execute(pool) |
| 823 | 200× | .await?; |
| 824 | 199× | Ok(r.rows_affected()) |
| 825 | 199× | } |
| 826 | |
|
| 827 | |
|
| 828 | 461× | pub async fn image_archs(pool: &Pool) -> sqlx::Result<std::collections::HashMap<String, String>> { |
| 829 | 461× | let rows: Vec<(String, String)> = sqlx::query_as("SELECT hash, arch FROM flow_images").fetch_all(pool).await?; |
| 830 | 461× | Ok(rows.into_iter().collect()) |
| 831 | 461× | } |
| 832 | |
|
| 833 | 0× | pub async fn claim(pool: &Pool, runtimes: &[String], runner: &str, token_hash: &str) -> sqlx::Result<Option<Job>> { |
| 834 | 0× | claim_within(pool, runtimes, runner, token_hash, Room::default(), &Default::default(), &[]).await |
| 835 | 0× | } |
| 836 | |
|
| 837 | |
|
| 838 | |
|
| 839 | |
|
| 840 | |
|
| 841 | |
|
| 842 | |
|
| 843 | |
|
| 844 | 0× | pub async fn claim_within( |
| 845 | 0× | pool: &Pool, |
| 846 | 0× | runtimes: &[String], |
| 847 | 0× | runner: &str, |
| 848 | 0× | token_hash: &str, |
| 849 | 0× | room: Room, |
| 850 | 0× | scope: &super::runners::Scope, |
| 851 | 0× | labels: &[String], |
| 852 | 0× | ) -> sqlx::Result<Option<Job>> { |
| 853 | 0× | claim_ranked(pool, runtimes, runner, token_hash, room, scope, labels, &|_| false).await |
| 854 | 0× | } |
| 855 | |
|
| 856 | |
|
| 857 | |
|
| 858 | 159× | pub async fn set_runner_version(pool: &Pool, id: i64, version: &str) -> sqlx::Result<()> { |
| 859 | 159× | sqlx::query("UPDATE flow_jobs SET runner_version = $1 WHERE id = $2").bind(version).bind(id).execute(pool).await?; |
| 860 | 159× | Ok(()) |
| 861 | 159× | } |
| 862 | |
|
| 863 | |
|
| 864 | |
|
| 865 | | #[allow(clippy::too_many_arguments)] |
| 866 | 225× | pub async fn claim_ranked( |
| 867 | 225× | pool: &Pool, |
| 868 | 225× | runtimes: &[String], |
| 869 | 225× | runner: &str, |
| 870 | 225× | token_hash: &str, |
| 871 | 225× | room: Room, |
| 872 | 225× | scope: &super::runners::Scope, |
| 873 | 225× | labels: &[String], |
| 874 | 225× | elsewhere: &(dyn Fn(&Job) -> bool + Sync), |
| 875 | 225× | ) -> sqlx::Result<Option<Job>> { |
| 876 | 225× | let mut tx = super::begin_write(pool).await?; |
| 877 | 225× | let queued: Vec<Job> = sqlx::query_as(&format!( |
| 878 | 225× | "SELECT j.* FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id JOIN repositories rep ON rep.id = r.repo_id \ |
| 879 | 225× | WHERE j.status = 'queued' AND j.approval != 'waiting' AND r.status IN ('queued', 'running') \ |
| 880 | 225× | AND ($1 = '' OR rep.owner = $2) AND ($3 IS NULL OR r.repo_id = $4) \ |
| 881 | 225× | AND NOT ($5 = '' AND $6 IS NULL AND rep.flow = 'own') \ |
| 882 | 225× | ORDER BY CASE WHEN j.handed_back_ms IS NULL THEN 1 ELSE 0 END, {PRIORITY_RANK}, \ |
| 883 | 225× | ROW_NUMBER() OVER (PARTITION BY r.repo_id ORDER BY j.run_id, j.depth, j.id) \ |
| 884 | 225× | + (SELECT COUNT(*) FROM flow_jobs x JOIN flow_runs xr ON xr.id = x.run_id WHERE x.status = 'running' AND xr.repo_id = r.repo_id), \ |
| 885 | 225× | j.run_id, j.depth, j.id LIMIT 200" |
| 886 | 225× | )) |
| 887 | 225× | .bind(&scope.owner) |
| 888 | 225× | .bind(&scope.owner) |
| 889 | 225× | .bind(scope.repo_id) |
| 890 | 225× | .bind(scope.repo_id) |
| 891 | 225× | |
| 892 | 225× | .bind(&scope.owner) |
| 893 | 225× | .bind(scope.repo_id) |
| 894 | 225× | .fetch_all(&mut *tx) |
| 895 | 225× | .await?; |
| 896 | 225× | for j in queued { |
| 897 | 186× | if !runtimes.iter().any(|r| r == &j.runtime) |
| 898 | 180× | || !room.fits(&j.sandbox) |
| 899 | 171× | || !room.disk_fits(j.need_disk_mib) |
| 900 | 170× | || room.declined.contains(&j.id) |
| 901 | | { |
| 902 | 17× | continue; |
| 903 | 169× | } |
| 904 | | |
| 905 | 169× | if !j.needs().iter().all(|l| labels.contains(l)) { |
| 906 | 2× | continue; |
| 907 | 167× | } |
| 908 | 167× | let deps = j.depends_on(); |
| 909 | | |
| 910 | 167× | let mut ready = j.queued_ms.unwrap_or(0).max(j.approved_ms.unwrap_or(0)); |
| 911 | 167× | if !deps.is_empty() { |
| 912 | 36× | let mut passed = 0i64; |
| 913 | 36× | for d in &deps { |
| 914 | 36× | let st: Option<(String, Option<i64>)> = |
| 915 | 36× | sqlx::query_as("SELECT status, finished_ms FROM flow_jobs WHERE run_id = $1 AND name = $2") |
| 916 | 36× | .bind(j.run_id) |
| 917 | 36× | .bind(d) |
| 918 | 36× | .fetch_optional(&mut *tx) |
| 919 | 36× | .await?; |
| 920 | 36× | if let Some((status, finished)) = st { |
| 921 | 36× | if status == "passed" { |
| 922 | 31× | passed += 1; |
| 923 | 31× | ready = ready.max(finished.unwrap_or(0)); |
| 924 | 31× | } |
| 925 | 0× | } |
| 926 | | } |
| 927 | 36× | if passed != deps.len() as i64 { |
| 928 | 5× | continue; |
| 929 | 31× | } |
| 930 | 131× | } |
| 931 | 162× | if elsewhere(&j) { |
| 932 | 3× | continue; |
| 933 | 159× | } |
| 934 | 159× | let now = now_ms(); |
| 935 | 159× | let done = sqlx::query( |
| 936 | 159× | "UPDATE flow_jobs SET status = 'running', runner = $1, token_hash = $2, started_ms = $3, waiting = '', ready_ms = $5 \ |
| 937 | 159× | WHERE id = $4 AND status = 'queued'", |
| 938 | | ) |
| 939 | 159× | .bind(runner) |
| 940 | 159× | .bind(token_hash) |
| 941 | 159× | .bind(now) |
| 942 | 159× | .bind(j.id) |
| 943 | 159× | .bind(if ready > 0 { ready.min(now) } else { now }) |
| 944 | 159× | .execute(&mut *tx) |
| 945 | 159× | .await?; |
| 946 | 159× | if done.rows_affected() == 1 { |
| 947 | 159× | sqlx::query("UPDATE flow_runs SET status = 'running' WHERE id = $1 AND status = 'queued'") |
| 948 | 159× | .bind(j.run_id) |
| 949 | 159× | .execute(&mut *tx) |
| 950 | 159× | .await?; |
| 951 | 159× | tx.commit().await?; |
| 952 | 159× | return job(pool, j.id).await; |
| 953 | 0× | } |
| 954 | | } |
| 955 | 66× | tx.commit().await?; |
| 956 | 66× | Ok(None) |
| 957 | 225× | } |
| 958 | |
|
| 959 | |
|
| 960 | |
|
| 961 | | pub const DECLINE_HOLDS_MS: i64 = 10 * 60_000; |
| 962 | |
|
| 963 | |
|
| 964 | 225× | pub async fn declined_by(pool: &Pool, runner_id: i64, profile: &str) -> sqlx::Result<std::collections::HashSet<i64>> { |
| 965 | 225× | let rows: Vec<(i64,)> = sqlx::query_as("SELECT job_id FROM flow_declines WHERE runner_id = $1 AND profile = $2 AND ms > $3") |
| 966 | 225× | .bind(runner_id) |
| 967 | 225× | .bind(profile) |
| 968 | 225× | .bind(now_ms() - DECLINE_HOLDS_MS) |
| 969 | 225× | .fetch_all(pool) |
| 970 | 225× | .await?; |
| 971 | 225× | Ok(rows.into_iter().map(|(j,)| j).collect()) |
| 972 | 225× | } |
| 973 | |
|
| 974 | |
|
| 975 | | #[derive(Clone, Debug, sqlx::FromRow)] |
| 976 | | pub struct DeclineRow { |
| 977 | | pub job_id: i64, |
| 978 | | pub runner_id: i64, |
| 979 | | pub runner: String, |
| 980 | | pub reason: String, |
| 981 | | pub ms: i64, |
| 982 | | } |
| 983 | |
|
| 984 | |
|
| 985 | |
|
| 986 | |
|
| 987 | |
|
| 988 | 4× | pub async fn decline( |
| 989 | 4× | pool: &Pool, |
| 990 | 4× | job_id: i64, |
| 991 | 4× | runner_id: i64, |
| 992 | 4× | runner: &str, |
| 993 | 4× | profile: &str, |
| 994 | 4× | reason: &str, |
| 995 | 4× | ) -> sqlx::Result<Option<Vec<DeclineRow>>> { |
| 996 | 4× | let mut tx = super::begin_write(pool).await?; |
| 997 | 4× | let back = sqlx::query( |
| 998 | 4× | "UPDATE flow_jobs SET status = 'queued', runner = '', token_hash = '', started_ms = NULL, runner_id = NULL \ |
| 999 | 4× | WHERE id = $1 AND status = 'running' AND runner_id = $2", |
| 1000 | 4× | ) |
| 1001 | 4× | .bind(job_id) |
| 1002 | 4× | .bind(runner_id) |
| 1003 | 4× | .execute(&mut *tx) |
| 1004 | 4× | .await?; |
| 1005 | 4× | if back.rows_affected() != 1 { |
| 1006 | 1× | tx.rollback().await?; |
| 1007 | 1× | return Ok(None); |
| 1008 | 3× | } |
| 1009 | 3× | sqlx::query( |
| 1010 | 3× | "INSERT INTO flow_declines (job_id, runner_id, runner, profile, reason, ms) VALUES ($1, $2, $3, $4, $5, $6) \ |
| 1011 | 3× | ON CONFLICT (job_id, runner_id) DO UPDATE SET runner = excluded.runner, profile = excluded.profile, reason = excluded.reason, ms = excluded.ms", |
| 1012 | 3× | ) |
| 1013 | 3× | .bind(job_id) |
| 1014 | 3× | .bind(runner_id) |
| 1015 | 3× | .bind(runner) |
| 1016 | 3× | .bind(profile) |
| 1017 | 3× | .bind(reason) |
| 1018 | 3× | .bind(now_ms()) |
| 1019 | 3× | .execute(&mut *tx) |
| 1020 | 3× | .await?; |
| 1021 | 3× | let all: Vec<DeclineRow> = |
| 1022 | 3× | sqlx::query_as("SELECT job_id, runner_id, runner, reason, ms FROM flow_declines WHERE job_id = $1 ORDER BY ms") |
| 1023 | 3× | .bind(job_id) |
| 1024 | 3× | .fetch_all(&mut *tx) |
| 1025 | 3× | .await?; |
| 1026 | 3× | tx.commit().await?; |
| 1027 | 3× | Ok(Some(all)) |
| 1028 | 4× | } |
| 1029 | |
|
| 1030 | |
|
| 1031 | |
|
| 1032 | |
|
| 1033 | |
|
| 1034 | |
|
| 1035 | 3× | pub async fn hand_back(pool: &Pool, job_id: i64, runner_id: Option<i64>) -> sqlx::Result<Option<i64>> { |
| 1036 | 3× | let mut tx = super::begin_write(pool).await?; |
| 1037 | 3× | let now = now_ms(); |
| 1038 | 3× | let back = sqlx::query( |
| 1039 | 3× | "UPDATE flow_jobs SET status = 'queued', runner = '', token_hash = '', started_ms = NULL, runner_id = NULL, \ |
| 1040 | 3× | handed_back_ms = $1, handbacks = handbacks + 1, queued_ms = $2, ready_ms = NULL, placed = '' \ |
| 1041 | 3× | WHERE id = $3 AND status = 'running' AND ($4 IS NULL OR runner_id = $5)", |
| 1042 | 3× | ) |
| 1043 | 3× | .bind(now) |
| 1044 | 3× | .bind(now) |
| 1045 | 3× | .bind(job_id) |
| 1046 | 3× | .bind(runner_id) |
| 1047 | 3× | .bind(runner_id) |
| 1048 | 3× | .execute(&mut *tx) |
| 1049 | 3× | .await?; |
| 1050 | 3× | if back.rows_affected() != 1 { |
| 1051 | 1× | tx.rollback().await?; |
| 1052 | 1× | return Ok(None); |
| 1053 | 2× | } |
| 1054 | 2× | let n: i64 = sqlx::query_scalar("SELECT handbacks FROM flow_jobs WHERE id = $1").bind(job_id).fetch_one(&mut *tx).await?; |
| 1055 | 2× | tx.commit().await?; |
| 1056 | 2× | Ok(Some(n)) |
| 1057 | 3× | } |
| 1058 | |
|
| 1059 | |
|
| 1060 | |
|
| 1061 | |
|
| 1062 | |
|
| 1063 | 159× | pub async fn expected_ms(pool: &Pool, repo_id: i64, name: &str) -> sqlx::Result<Option<i64>> { |
| 1064 | 159× | let mut times: Vec<i64> = sqlx::query_scalar( |
| 1065 | 159× | "SELECT j.finished_ms - j.started_ms FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id \ |
| 1066 | 159× | WHERE r.repo_id = $1 AND j.name = $2 AND j.status = 'passed' AND j.error = '' \ |
| 1067 | 159× | AND j.started_ms IS NOT NULL AND j.finished_ms IS NOT NULL ORDER BY j.id DESC LIMIT 5", |
| 1068 | 159× | ) |
| 1069 | 159× | .bind(repo_id) |
| 1070 | 159× | .bind(name) |
| 1071 | 159× | .fetch_all(pool) |
| 1072 | 159× | .await?; |
| 1073 | 159× | times.sort_unstable(); |
| 1074 | 159× | Ok(times.get(times.len() / 2).copied().filter(|ms| *ms > 0)) |
| 1075 | 159× | } |
| 1076 | |
|
| 1077 | |
|
| 1078 | |
|
| 1079 | 1× | pub async fn running_on_silent_runners(pool: &Pool, before: i64) -> sqlx::Result<Vec<(Job, String)>> { |
| 1080 | 1× | let rows: Vec<(i64, String)> = sqlx::query_as( |
| 1081 | 1× | "SELECT j.id, r.name FROM flow_jobs j JOIN flow_runners r ON r.id = j.runner_id \ |
| 1082 | 1× | WHERE j.status = 'running' AND r.last_seen_ms < $1 ORDER BY j.id", |
| 1083 | 1× | ) |
| 1084 | 1× | .bind(before) |
| 1085 | 1× | .fetch_all(pool) |
| 1086 | 1× | .await?; |
| 1087 | 1× | let mut out = Vec::new(); |
| 1088 | 1× | for (id, runner) in rows { |
| 1089 | 1× | if let Some(j) = job(pool, id).await? { |
| 1090 | 1× | out.push((j, runner)); |
| 1091 | 1× | } |
| 1092 | | } |
| 1093 | 1× | Ok(out) |
| 1094 | 1× | } |
| 1095 | |
|
| 1096 | |
|
| 1097 | 16× | pub async fn declines_of_job(pool: &Pool, job_id: i64) -> sqlx::Result<Vec<DeclineRow>> { |
| 1098 | 16× | sqlx::query_as("SELECT job_id, runner_id, runner, reason, ms FROM flow_declines WHERE job_id = $1 ORDER BY ms") |
| 1099 | 16× | .bind(job_id) |
| 1100 | 16× | .fetch_all(pool) |
| 1101 | 16× | .await |
| 1102 | 16× | } |
| 1103 | |
|
| 1104 | |
|
| 1105 | | #[derive(Clone, Debug, sqlx::FromRow)] |
| 1106 | | pub struct RunnerDecline { |
| 1107 | | pub job_id: i64, |
| 1108 | | pub job: String, |
| 1109 | | pub run_number: i64, |
| 1110 | | pub owner: String, |
| 1111 | | pub name: String, |
| 1112 | | pub reason: String, |
| 1113 | | pub ms: i64, |
| 1114 | | } |
| 1115 | |
|
| 1116 | 4× | pub async fn declines_of_runner(pool: &Pool, runner_id: i64, limit: i64) -> sqlx::Result<Vec<RunnerDecline>> { |
| 1117 | 4× | sqlx::query_as( |
| 1118 | 4× | "SELECT d.job_id, j.name AS job, r.number AS run_number, rep.owner, rep.name, d.reason, d.ms \ |
| 1119 | 4× | FROM flow_declines d JOIN flow_jobs j ON j.id = d.job_id JOIN flow_runs r ON r.id = j.run_id \ |
| 1120 | 4× | JOIN repositories rep ON rep.id = r.repo_id WHERE d.runner_id = $1 ORDER BY d.ms DESC LIMIT $2", |
| 1121 | 4× | ) |
| 1122 | 4× | .bind(runner_id) |
| 1123 | 4× | .bind(limit) |
| 1124 | 4× | .fetch_all(pool) |
| 1125 | 4× | .await |
| 1126 | 4× | } |
| 1127 | |
|
| 1128 | |
|
| 1129 | |
|
| 1130 | |
|
| 1131 | |
|
| 1132 | 8× | pub async fn queued_ranks(pool: &Pool) -> sqlx::Result<std::collections::HashMap<i64, i64>> { |
| 1133 | 8× | let rows: Vec<(i64, i64)> = sqlx::query_as(&format!( |
| 1134 | 8× | "SELECT j.id, CAST({PRIORITY_RANK} AS BIGINT) FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id JOIN repositories rep ON rep.id = r.repo_id \ |
| 1135 | 8× | WHERE j.status = 'queued'" |
| 1136 | 8× | )) |
| 1137 | 8× | .fetch_all(pool) |
| 1138 | 8× | .await?; |
| 1139 | 8× | Ok(rows.into_iter().collect()) |
| 1140 | 8× | } |
| 1141 | |
|
| 1142 | |
|
| 1143 | 8× | pub async fn running_by_runner(pool: &Pool) -> sqlx::Result<std::collections::HashMap<i64, i64>> { |
| 1144 | 8× | let rows: Vec<(i64, i64)> = |
| 1145 | 8× | sqlx::query_as("SELECT runner_id, COUNT(*) FROM flow_jobs WHERE status = 'running' AND runner_id IS NOT NULL GROUP BY runner_id") |
| 1146 | 8× | .fetch_all(pool) |
| 1147 | 8× | .await?; |
| 1148 | 8× | Ok(rows.into_iter().collect()) |
| 1149 | 8× | } |
| 1150 | |
|
| 1151 | 8× | pub async fn ready_queued(pool: &Pool) -> sqlx::Result<Vec<(Job, String, i64)>> { |
| 1152 | 8× | let queued: Vec<Job> = sqlx::query_as( |
| 1153 | 8× | "SELECT j.* FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id \ |
| 1154 | 8× | WHERE j.status = 'queued' AND j.approval != 'waiting' AND r.status IN ('queued', 'running') ORDER BY j.run_id, j.depth, j.id LIMIT 500", |
| 1155 | 8× | ) |
| 1156 | 8× | .fetch_all(pool) |
| 1157 | 8× | .await?; |
| 1158 | 8× | let mut out = Vec::new(); |
| 1159 | 24× | for j in queued { |
| 1160 | 24× | let mut ready = true; |
| 1161 | 24× | for d in j.depends_on() { |
| 1162 | 9× | let st: Option<String> = sqlx::query_scalar("SELECT status FROM flow_jobs WHERE run_id = $1 AND name = $2") |
| 1163 | 9× | .bind(j.run_id) |
| 1164 | 9× | .bind(&d) |
| 1165 | 9× | .fetch_optional(pool) |
| 1166 | 9× | .await?; |
| 1167 | 9× | if st.as_deref() != Some("passed") { |
| 1168 | 9× | ready = false; |
| 1169 | 9× | break; |
| 1170 | 0× | } |
| 1171 | | } |
| 1172 | 24× | if !ready { |
| 1173 | 9× | continue; |
| 1174 | 15× | } |
| 1175 | 15× | let repo: Option<(String, i64)> = |
| 1176 | 15× | sqlx::query_as("SELECT rep.owner, rep.id FROM flow_runs r JOIN repositories rep ON rep.id = r.repo_id WHERE r.id = $1") |
| 1177 | 15× | .bind(j.run_id) |
| 1178 | 15× | .fetch_optional(pool) |
| 1179 | 15× | .await?; |
| 1180 | 15× | if let Some((owner, id)) = repo { |
| 1181 | 15× | out.push((j, owner, id)); |
| 1182 | 15× | } |
| 1183 | | } |
| 1184 | 8× | Ok(out) |
| 1185 | 8× | } |
| 1186 | |
|
| 1187 | 17× | pub async fn set_waiting(pool: &Pool, job_id: i64, waiting: &str) -> sqlx::Result<()> { |
| 1188 | 17× | sqlx::query("UPDATE flow_jobs SET waiting = $1 WHERE id = $2 AND waiting <> $3") |
| 1189 | 17× | .bind(waiting) |
| 1190 | 17× | .bind(job_id) |
| 1191 | 17× | .bind(waiting) |
| 1192 | 17× | .execute(pool) |
| 1193 | 17× | .await?; |
| 1194 | 17× | Ok(()) |
| 1195 | 17× | } |
| 1196 | |
|
| 1197 | |
|
| 1198 | 44× | pub async fn append_log(pool: &Pool, job_id: i64, records: &[flow_core::api::Record], cap: i64) -> sqlx::Result<usize> { |
| 1199 | 44× | let mut tx = super::begin_write(pool).await?; |
| 1200 | 44× | let count: i64 = sqlx::query_scalar("SELECT log_count FROM flow_jobs WHERE id = $1").bind(job_id).fetch_one(&mut *tx).await?; |
| 1201 | 44× | let room = (cap - count).max(0) as usize; |
| 1202 | 44× | let keep = &records[..records.len().min(room)]; |
| 1203 | 361× | for (i, r) in keep.iter().enumerate() { |
| 1204 | 361× | sqlx::query("INSERT INTO flow_logs (job_id, seq, t, stream, text) VALUES ($1, $2, $3, $4, $5)") |
| 1205 | 361× | .bind(job_id) |
| 1206 | 361× | .bind(count + i as i64 + 1) |
| 1207 | 361× | .bind(r.t as i64) |
| 1208 | 361× | .bind(&r.stream) |
| 1209 | 361× | .bind(&r.text) |
| 1210 | 361× | .execute(&mut *tx) |
| 1211 | 361× | .await?; |
| 1212 | | } |
| 1213 | 44× | sqlx::query("UPDATE flow_jobs SET log_count = log_count + $1 WHERE id = $2") |
| 1214 | 44× | .bind(keep.len() as i64) |
| 1215 | 44× | .bind(job_id) |
| 1216 | 44× | .execute(&mut *tx) |
| 1217 | 44× | .await?; |
| 1218 | 44× | tx.commit().await?; |
| 1219 | 44× | Ok(keep.len()) |
| 1220 | 44× | } |
| 1221 | |
|
| 1222 | 73× | pub async fn log(pool: &Pool, job_id: i64, after: i64, limit: i64) -> sqlx::Result<Vec<LogLine>> { |
| 1223 | 73× | sqlx::query_as("SELECT seq, t, stream, text FROM flow_logs WHERE job_id = $1 AND seq > $2 ORDER BY seq LIMIT $3") |
| 1224 | 73× | .bind(job_id) |
| 1225 | 73× | .bind(after) |
| 1226 | 73× | .bind(limit) |
| 1227 | 73× | .fetch_all(pool) |
| 1228 | 73× | .await |
| 1229 | 73× | } |
| 1230 | |
|
| 1231 | |
|
| 1232 | |
|
| 1233 | |
|
| 1234 | 3× | pub async fn running_past_limit(pool: &Pool, now: i64, default_s: u64, grace_ms: i64) -> sqlx::Result<Vec<Job>> { |
| 1235 | 3× | sqlx::query_as( |
| 1236 | 3× | "SELECT * FROM flow_jobs WHERE status = 'running' \ |
| 1237 | 3× | AND COALESCE(started_ms, 0) + (CASE WHEN timeout_s > 0 THEN timeout_s ELSE $1 END) * 1000 + $2 < $3 ORDER BY started_ms", |
| 1238 | 3× | ) |
| 1239 | 3× | .bind(default_s as i64) |
| 1240 | 3× | .bind(grace_ms) |
| 1241 | 3× | .bind(now) |
| 1242 | 3× | .fetch_all(pool) |
| 1243 | 3× | .await |
| 1244 | 3× | } |
| 1245 | |
|
| 1246 | |
|
| 1247 | 159× | pub async fn set_job_timeout(pool: &Pool, job_id: i64, timeout_s: u64) -> sqlx::Result<()> { |
| 1248 | 159× | sqlx::query("UPDATE flow_jobs SET timeout_s = $1 WHERE id = $2").bind(timeout_s as i64).bind(job_id).execute(pool).await?; |
| 1249 | 159× | Ok(()) |
| 1250 | 159× | } |
| 1251 | |
|
| 1252 | 0× | pub async fn running_since_before(pool: &Pool, ms: i64) -> sqlx::Result<Vec<Job>> { |
| 1253 | 0× | sqlx::query_as("SELECT * FROM flow_jobs WHERE status = 'running' AND COALESCE(started_ms, 0) < $1 ORDER BY started_ms") |
| 1254 | 0× | .bind(ms) |
| 1255 | 0× | .fetch_all(pool) |
| 1256 | 0× | .await |
| 1257 | 0× | } |
| 1258 | |
|
| 1259 | |
|
| 1260 | 2× | pub async fn running_on(pool: &Pool, runner_id: i64) -> sqlx::Result<Vec<Job>> { |
| 1261 | 2× | sqlx::query_as("SELECT * FROM flow_jobs WHERE status = 'running' AND runner_id = $1 ORDER BY id").bind(runner_id).fetch_all(pool).await |
| 1262 | 2× | } |
| 1263 | |
|
| 1264 | | pub struct Outcome<'a> { |
| 1265 | | pub status: &'a str, |
| 1266 | | pub exit_code: Option<i64>, |
| 1267 | | pub error: &'a str, |
| 1268 | | pub receipt: &'a str, |
| 1269 | | pub receipt_hash: &'a str, |
| 1270 | | } |
| 1271 | |
|
| 1272 | |
|
| 1273 | |
|
| 1274 | 107× | pub async fn finish(pool: &Pool, job_id: i64, o: Outcome<'_>) -> sqlx::Result<Vec<Job>> { |
| 1275 | 107× | let mut tx = super::begin_write(pool).await?; |
| 1276 | 107× | let now = now_ms(); |
| 1277 | 107× | let j: Job = sqlx::query_as("SELECT * FROM flow_jobs WHERE id = $1").bind(job_id).fetch_one(&mut *tx).await?; |
| 1278 | 107× | sqlx::query("UPDATE flow_jobs SET status = $1, exit_code = $2, error = $3, receipt = $4, receipt_hash = $5, finished_ms = $6, token_hash = '' WHERE id = $7") |
| 1279 | 107× | .bind(o.status) |
| 1280 | 107× | .bind(o.exit_code) |
| 1281 | 107× | .bind(o.error) |
| 1282 | 107× | .bind(o.receipt) |
| 1283 | 107× | .bind(o.receipt_hash) |
| 1284 | 107× | .bind(now) |
| 1285 | 107× | .bind(job_id) |
| 1286 | 107× | .execute(&mut *tx) |
| 1287 | 107× | .await?; |
| 1288 | | |
| 1289 | | |
| 1290 | 107× | if o.status == "passed" { |
| 1291 | 88× | let rootfs = serde_json::from_str::<serde_json::Value>(&j.sandbox) |
| 1292 | 88× | .ok() |
| 1293 | 88× | .and_then(|v| v["rootfs"].as_str().map(str::to_ascii_lowercase)) |
| 1294 | 88× | .unwrap_or_default(); |
| 1295 | 88× | if rootfs.len() == 64 { |
| 1296 | 88× | sqlx::query( |
| 1297 | 88× | "INSERT INTO flow_images (hash, arch, seen_ms) SELECT $1, r.arch, $2 FROM flow_jobs j JOIN flow_runners r ON r.id = j.runner_id \ |
| 1298 | 88× | WHERE j.id = $3 AND r.arch != '' ON CONFLICT (hash) DO NOTHING", |
| 1299 | 88× | ) |
| 1300 | 88× | .bind(&rootfs) |
| 1301 | 88× | .bind(now) |
| 1302 | 88× | .bind(job_id) |
| 1303 | 88× | .execute(&mut *tx) |
| 1304 | 88× | .await?; |
| 1305 | 0× | } |
| 1306 | 19× | } |
| 1307 | 107× | let mut changed = vec![job_id]; |
| 1308 | | |
| 1309 | | loop { |
| 1310 | 115× | let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = $1").bind(j.run_id).fetch_all(&mut *tx).await?; |
| 1311 | 115× | let blocked: Vec<i64> = all |
| 1312 | 115× | .iter() |
| 1313 | 324× | .filter(|x| x.status == "queued") |
| 1314 | 115× | .filter(|x| { |
| 1315 | 95× | x.depends_on() |
| 1316 | 95× | .iter() |
| 1317 | 175× | .any(|d| all.iter().any(|y| &y.name == d && matches!(y.status.as_str(), "failed" | "skipped" | "error" | "cancelled"))) |
| 1318 | 95× | }) |
| 1319 | 115× | .map(|x| x.id) |
| 1320 | 115× | .collect(); |
| 1321 | 115× | if blocked.is_empty() { |
| 1322 | 107× | break; |
| 1323 | 8× | } |
| 1324 | 8× | for id in blocked { |
| 1325 | 8× | sqlx::query("UPDATE flow_jobs SET status = 'skipped', finished_ms = $1 WHERE id = $2") |
| 1326 | 8× | .bind(now) |
| 1327 | 8× | .bind(id) |
| 1328 | 8× | .execute(&mut *tx) |
| 1329 | 8× | .await?; |
| 1330 | 8× | changed.push(id); |
| 1331 | | } |
| 1332 | | } |
| 1333 | 107× | let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = $1").bind(j.run_id).fetch_all(&mut *tx).await?; |
| 1334 | 107× | if all.iter().all(Job::done) { |
| 1335 | 93× | let status = if all.iter().all(|x| x.status == "passed") { "passed" } else { "failed" }; |
| 1336 | 42× | sqlx::query("UPDATE flow_runs SET status = $1, finished_ms = $2 WHERE id = $3") |
| 1337 | 42× | .bind(status) |
| 1338 | 42× | .bind(now) |
| 1339 | 42× | .bind(j.run_id) |
| 1340 | 42× | .execute(&mut *tx) |
| 1341 | 42× | .await?; |
| 1342 | 65× | } |
| 1343 | 107× | tx.commit().await?; |
| 1344 | 107× | let mut out = Vec::new(); |
| 1345 | 115× | for id in changed { |
| 1346 | 115× | if let Some(x) = job(pool, id).await? { |
| 1347 | 115× | out.push(x); |
| 1348 | 115× | } |
| 1349 | | } |
| 1350 | 107× | Ok(out) |
| 1351 | 107× | } |
| 1352 | |
|
| 1353 | |
|
| 1354 | |
|
| 1355 | |
|
| 1356 | 16× | pub async fn cancel_run(pool: &Pool, run_id: i64, why: &str) -> sqlx::Result<Vec<Job>> { |
| 1357 | 16× | let now = now_ms(); |
| 1358 | 16× | let mut tx = pool.begin().await?; |
| 1359 | 16× | let ids: Vec<(i64,)> = sqlx::query_as("SELECT id FROM flow_jobs WHERE run_id = $1 AND status IN ('queued', 'running')") |
| 1360 | 16× | .bind(run_id) |
| 1361 | 16× | .fetch_all(&mut *tx) |
| 1362 | 16× | .await?; |
| 1363 | 16× | sqlx::query( |
| 1364 | 16× | "UPDATE flow_jobs SET status = 'cancelled', error = $1, finished_ms = $2, token_hash = '' |
| 1365 | 16× | WHERE run_id = $3 AND status IN ('queued', 'running')", |
| 1366 | 16× | ) |
| 1367 | 16× | .bind(why) |
| 1368 | 16× | .bind(now) |
| 1369 | 16× | .bind(run_id) |
| 1370 | 16× | .execute(&mut *tx) |
| 1371 | 16× | .await?; |
| 1372 | 16× | sqlx::query("UPDATE flow_runs SET status = 'cancelled', finished_ms = $1 WHERE id = $2 AND status IN ('queued', 'running')") |
| 1373 | 16× | .bind(now) |
| 1374 | 16× | .bind(run_id) |
| 1375 | 16× | .execute(&mut *tx) |
| 1376 | 16× | .await?; |
| 1377 | 16× | tx.commit().await?; |
| 1378 | 16× | let mut out = Vec::new(); |
| 1379 | 40× | for (id,) in ids { |
| 1380 | 40× | if let Some(x) = job(pool, id).await? { |
| 1381 | 40× | out.push(x); |
| 1382 | 40× | } |
| 1383 | | } |
| 1384 | 16× | Ok(out) |
| 1385 | 16× | } |
| 1386 | |
|
| 1387 | |
|
| 1388 | |
|
| 1389 | |
|
| 1390 | |
|
| 1391 | |
|
| 1392 | 2× | pub async fn rerun_job(pool: &Pool, run_id: i64, name: &str) -> sqlx::Result<Vec<Job>> { |
| 1393 | 2× | let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = $1").bind(run_id).fetch_all(pool).await?; |
| 1394 | 4× | if !all.iter().any(|j| j.name == name) { |
| 1395 | 0× | return Ok(Vec::new()); |
| 1396 | 2× | } |
| 1397 | 2× | let mut again = vec![name.to_string()]; |
| 1398 | | loop { |
| 1399 | 3× | let more: Vec<String> = all |
| 1400 | 3× | .iter() |
| 1401 | 9× | .filter(|j| !again.contains(&j.name) && j.depends_on().iter().any(|d| again.contains(d))) |
| 1402 | 3× | .map(|j| j.name.clone()) |
| 1403 | 3× | .collect(); |
| 1404 | 3× | if more.is_empty() { |
| 1405 | 2× | break; |
| 1406 | 1× | } |
| 1407 | 1× | again.extend(more); |
| 1408 | | } |
| 1409 | 6× | let ids: Vec<i64> = all.iter().filter(|j| again.contains(&j.name)).map(|j| j.id).collect(); |
| 1410 | 2× | let mut tx = pool.begin().await?; |
| 1411 | 3× | for id in &ids { |
| 1412 | 18× | for table in ["flow_logs", "flow_artifacts", "flow_declines", "test_reports", "coverage_reports", "reports"] { |
| 1413 | 18× | sqlx::query(&format!("DELETE FROM {table} WHERE job_id = $1")).bind(id).execute(&mut *tx).await?; |
| 1414 | | } |
| 1415 | 3× | sqlx::query( |
| 1416 | 3× | "UPDATE flow_jobs SET status = 'queued', runner = '', token_hash = '', started_ms = NULL, finished_ms = NULL, |
| 1417 | 3× | exit_code = NULL, error = '', receipt = '', receipt_hash = '', log_count = 0, also_read = '', receipt_attest = '', |
| 1418 | 3× | receipt_signer = '', receipt_note = '', runner_id = NULL, waiting = '', placed = '', peak_disk_mib = NULL, |
| 1419 | 3× | peak_memory_mib = NULL, phases = '', approval = CASE WHEN approval = '' THEN '' ELSE 'waiting' END, approved_by = '', |
| 1420 | 3× | approved_ms = NULL, approval_seal = '', approved_via = '', queued_ms = $2, ready_ms = NULL, handed_back_ms = NULL, |
| 1421 | 3× | handbacks = 0 WHERE id = $1", |
| 1422 | 3× | ) |
| 1423 | 3× | .bind(id) |
| 1424 | 3× | .bind(now_ms()) |
| 1425 | 3× | .execute(&mut *tx) |
| 1426 | 3× | .await?; |
| 1427 | | } |
| 1428 | 2× | sqlx::query("UPDATE flow_runs SET status = 'running', finished_ms = NULL WHERE id = $1").bind(run_id).execute(&mut *tx).await?; |
| 1429 | 2× | tx.commit().await?; |
| 1430 | 2× | let mut out = Vec::new(); |
| 1431 | 3× | for id in ids { |
| 1432 | 3× | if let Some(x) = job(pool, id).await? { |
| 1433 | 3× | out.push(x); |
| 1434 | 3× | } |
| 1435 | | } |
| 1436 | 2× | Ok(out) |
| 1437 | 2× | } |
| 1438 | |
|
| 1439 | |
|
| 1440 | |
|
| 1441 | |
|
| 1442 | 126× | pub async fn live_runs_before( |
| 1443 | 126× | pool: &Pool, |
| 1444 | 126× | repo_id: i64, |
| 1445 | 126× | bookmark: &str, |
| 1446 | 126× | trigger: &str, |
| 1447 | 126× | number: i64, |
| 1448 | 126× | running_too: bool, |
| 1449 | 126× | ) -> sqlx::Result<Vec<Run>> { |
| 1450 | 126× | sqlx::query_as( |
| 1451 | 126× | "SELECT * FROM flow_runs WHERE repo_id = $1 AND bookmark = $2 AND number < $3 AND trigger = $4 |
| 1452 | 126× | AND (status = 'queued' OR ($5 AND status = 'running')) ORDER BY id", |
| 1453 | 126× | ) |
| 1454 | 126× | .bind(repo_id) |
| 1455 | 126× | .bind(bookmark) |
| 1456 | 126× | .bind(number) |
| 1457 | 126× | .bind(trigger) |
| 1458 | 126× | .bind(running_too) |
| 1459 | 126× | .fetch_all(pool) |
| 1460 | 126× | .await |
| 1461 | 126× | } |
| 1462 | |
|
| 1463 | |
|
| 1464 | |
|
| 1465 | 44× | pub async fn unstarted_runs_on(pool: &Pool, repo_id: i64, bookmark: &str, trigger: &str, before_ms: i64) -> sqlx::Result<Vec<Run>> { |
| 1466 | 44× | sqlx::query_as( |
| 1467 | 44× | "SELECT * FROM flow_runs WHERE repo_id = $1 AND bookmark = $2 AND trigger = $3 AND status = 'queued' AND created_ms <= $4 ORDER BY id", |
| 1468 | 44× | ) |
| 1469 | 44× | .bind(repo_id) |
| 1470 | 44× | .bind(bookmark) |
| 1471 | 44× | .bind(trigger) |
| 1472 | 44× | .bind(before_ms) |
| 1473 | 44× | .fetch_all(pool) |
| 1474 | 44× | .await |
| 1475 | 44× | } |
| 1476 | |
|
| 1477 | |
|
| 1478 | 6× | pub async fn latest_for_heads(pool: &Pool, repo_id: i64, heads: &str) -> sqlx::Result<Option<Run>> { |
| 1479 | 6× | sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = $1 AND heads = $2 ORDER BY id DESC LIMIT 1") |
| 1480 | 6× | .bind(repo_id) |
| 1481 | 6× | .bind(heads) |
| 1482 | 6× | .fetch_optional(pool) |
| 1483 | 6× | .await |
| 1484 | 6× | } |
| 1485 | |
|
| 1486 | |
|
| 1487 | 11× | pub async fn set_receipt(pool: &Pool, job_id: i64, attest: &str, signer: &str, note: &str) -> sqlx::Result<()> { |
| 1488 | 11× | sqlx::query("UPDATE flow_jobs SET receipt_attest = $1, receipt_signer = $2, receipt_note = $3 WHERE id = $4") |
| 1489 | 11× | .bind(attest) |
| 1490 | 11× | .bind(signer) |
| 1491 | 11× | .bind(note) |
| 1492 | 11× | .bind(job_id) |
| 1493 | 11× | .execute(pool) |
| 1494 | 11× | .await?; |
| 1495 | 11× | Ok(()) |
| 1496 | 11× | } |
| 1497 | |
|
| 1498 | |
|
| 1499 | | #[derive(Clone, Debug, FromRow)] |
| 1500 | | pub struct Artifact { |
| 1501 | | pub job_id: i64, |
| 1502 | | pub name: String, |
| 1503 | | pub path: String, |
| 1504 | | pub blob: String, |
| 1505 | | pub size: i64, |
| 1506 | | |
| 1507 | | pub content: String, |
| 1508 | | pub created_ms: i64, |
| 1509 | | } |
| 1510 | |
|
| 1511 | 14× | pub async fn add_artifact(pool: &Pool, a: &Artifact) -> sqlx::Result<()> { |
| 1512 | 14× | sqlx::query( |
| 1513 | 14× | "INSERT INTO flow_artifacts (job_id, name, path, blob, size, content, created_ms) VALUES ($1, $2, $3, $4, $5, $6, $7) ON CONFLICT (job_id, name) DO UPDATE SET path = excluded.path, blob = excluded.blob, size = excluded.size, content = excluded.content, created_ms = excluded.created_ms", |
| 1514 | 14× | ) |
| 1515 | 14× | .bind(a.job_id) |
| 1516 | 14× | .bind(&a.name) |
| 1517 | 14× | .bind(&a.path) |
| 1518 | 14× | .bind(&a.blob) |
| 1519 | 14× | .bind(a.size) |
| 1520 | 14× | .bind(&a.content) |
| 1521 | 14× | .bind(a.created_ms) |
| 1522 | 14× | .execute(pool) |
| 1523 | 14× | .await?; |
| 1524 | 14× | Ok(()) |
| 1525 | 14× | } |
| 1526 | |
|
| 1527 | |
|
| 1528 | |
|
| 1529 | 4× | pub async fn artifact_blobs(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<String>> { |
| 1530 | 4× | sqlx::query_scalar( |
| 1531 | 4× | "SELECT DISTINCT a.blob FROM flow_artifacts a JOIN flow_jobs j ON j.id = a.job_id JOIN flow_runs r ON r.id = j.run_id WHERE r.repo_id = $1", |
| 1532 | 4× | ) |
| 1533 | 4× | .bind(repo_id) |
| 1534 | 4× | .fetch_all(pool) |
| 1535 | 4× | .await |
| 1536 | 4× | } |
| 1537 | |
|
| 1538 | |
|
| 1539 | |
|
| 1540 | 152× | pub async fn artifacts(pool: &Pool, job_id: i64) -> sqlx::Result<Vec<Artifact>> { |
| 1541 | 152× | sqlx::query_as("SELECT * FROM flow_artifacts WHERE job_id = $1 AND name NOT LIKE '.%' ORDER BY name").bind(job_id).fetch_all(pool).await |
| 1542 | 152× | } |
| 1543 | |
|
| 1544 | |
|
| 1545 | |
|
| 1546 | | pub const CHECKPOINT: &str = ".checkpoint"; |
| 1547 | |
|
| 1548 | |
|
| 1549 | |
|
| 1550 | 3× | pub async fn checkpoint(pool: &Pool, job_id: i64) -> sqlx::Result<Option<Artifact>> { |
| 1551 | 3× | sqlx::query_as("SELECT * FROM flow_artifacts WHERE job_id = $1 AND name = $2").bind(job_id).bind(CHECKPOINT).fetch_optional(pool).await |
| 1552 | 3× | } |
| 1553 | |
|
| 1554 | |
|
| 1555 | |
|
| 1556 | | #[derive(Clone, Debug, FromRow)] |
| 1557 | | pub struct RepoArtifact { |
| 1558 | | pub run_number: i64, |
| 1559 | | pub bookmark: String, |
| 1560 | | pub job: String, |
| 1561 | | pub name: String, |
| 1562 | | pub path: String, |
| 1563 | | pub size: i64, |
| 1564 | | pub content: String, |
| 1565 | | pub created_ms: i64, |
| 1566 | | } |
| 1567 | |
|
| 1568 | |
|
| 1569 | 2× | pub async fn repo_artifacts(pool: &Pool, repo_id: i64, limit: i64) -> sqlx::Result<Vec<RepoArtifact>> { |
| 1570 | 2× | sqlx::query_as( |
| 1571 | 2× | "SELECT r.number AS run_number, r.bookmark, j.name AS job, a.name, a.path, a.size, a.content, a.created_ms |
| 1572 | 2× | FROM flow_artifacts a JOIN flow_jobs j ON j.id = a.job_id JOIN flow_runs r ON r.id = j.run_id |
| 1573 | 2× | WHERE r.repo_id = $1 ORDER BY a.created_ms DESC, r.number DESC, j.name, a.name LIMIT $2", |
| 1574 | 2× | ) |
| 1575 | 2× | .bind(repo_id) |
| 1576 | 2× | .bind(limit) |
| 1577 | 2× | .fetch_all(pool) |
| 1578 | 2× | .await |
| 1579 | 2× | } |
| 1580 | |
|
| 1581 | |
|
| 1582 | |
|
| 1583 | | #[derive(Clone, Debug, FromRow)] |
| 1584 | | pub struct Provenance { |
| 1585 | | pub run_number: i64, |
| 1586 | | pub job: String, |
| 1587 | | pub name: String, |
| 1588 | | pub receipt_attest: String, |
| 1589 | | pub receipt_signer: String, |
| 1590 | | } |
| 1591 | |
|
| 1592 | |
|
| 1593 | |
|
| 1594 | |
|
| 1595 | 21× | pub async fn provenance(pool: &Pool, repo_id: i64, content: &str) -> sqlx::Result<Option<Provenance>> { |
| 1596 | 21× | sqlx::query_as( |
| 1597 | 21× | "SELECT r.number AS run_number, j.name AS job, a.name AS name, j.receipt_attest, j.receipt_signer \ |
| 1598 | 21× | FROM flow_artifacts a JOIN flow_jobs j ON j.id = a.job_id JOIN flow_runs r ON r.id = j.run_id \ |
| 1599 | 21× | WHERE r.repo_id = $1 AND a.content = $2 AND j.status = 'passed' \ |
| 1600 | 21× | ORDER BY (j.receipt_attest != '') DESC, r.number DESC LIMIT 1", |
| 1601 | 21× | ) |
| 1602 | 21× | .bind(repo_id) |
| 1603 | 21× | .bind(content) |
| 1604 | 21× | .fetch_optional(pool) |
| 1605 | 21× | .await |
| 1606 | 21× | } |
| 1607 | |
|
| 1608 | |
|
| 1609 | |
|
| 1610 | 2× | pub async fn approve_job(pool: &Pool, id: i64, who: &str, seal: &str, via: &str) -> sqlx::Result<bool> { |
| 1611 | 2× | let r = sqlx::query( |
| 1612 | 2× | "UPDATE flow_jobs SET approval = 'approved', approved_by = $1, approved_ms = $2, approval_seal = $3, approved_via = $5 \ |
| 1613 | 2× | WHERE id = $4 AND status = 'queued' AND approval = 'waiting'", |
| 1614 | 2× | ) |
| 1615 | 2× | .bind(who) |
| 1616 | 2× | .bind(now_ms()) |
| 1617 | 2× | .bind(seal) |
| 1618 | 2× | .bind(id) |
| 1619 | 2× | .bind(via) |
| 1620 | 2× | .execute(pool) |
| 1621 | 2× | .await?; |
| 1622 | 2× | Ok(r.rows_affected() > 0) |
| 1623 | 2× | } |
| 1624 | |
|
| 1625 | |
|
| 1626 | 1× | pub async fn note_rejection(pool: &Pool, id: i64, who: &str, via: &str) -> sqlx::Result<()> { |
| 1627 | 1× | sqlx::query("UPDATE flow_jobs SET approved_by = $1, approved_ms = $2, approved_via = $4 WHERE id = $3") |
| 1628 | 1× | .bind(who) |
| 1629 | 1× | .bind(now_ms()) |
| 1630 | 1× | .bind(id) |
| 1631 | 1× | .bind(via) |
| 1632 | 1× | .execute(pool) |
| 1633 | 1× | .await?; |
| 1634 | 1× | Ok(()) |
| 1635 | 1× | } |
| 1636 | |
|
| 1637 | |
|
| 1638 | 2× | pub async fn waiting_for_approval(pool: &Pool) -> sqlx::Result<Vec<Job>> { |
| 1639 | 2× | sqlx::query_as( |
| 1640 | 2× | "SELECT j.* FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id \ |
| 1641 | 2× | WHERE j.status = 'queued' AND j.approval = 'waiting' AND r.status IN ('queued', 'running')", |
| 1642 | 2× | ) |
| 1643 | 2× | .fetch_all(pool) |
| 1644 | 2× | .await |
| 1645 | 2× | } |
| 1646 | |
|
| 1647 | |
|
| 1648 | | #[derive(Clone, Debug, FromRow)] |
| 1649 | | pub struct Deployment { |
| 1650 | | pub environment: String, |
| 1651 | | pub job: String, |
| 1652 | | pub run_number: i64, |
| 1653 | | pub state: String, |
| 1654 | | pub bookmark: String, |
| 1655 | | pub actor: String, |
| 1656 | | pub approved_by: String, |
| 1657 | | pub finished_ms: i64, |
| 1658 | | } |
| 1659 | |
|
| 1660 | |
|
| 1661 | 2× | pub async fn deployments(pool: &Pool, repo_id: i64, limit: i64) -> sqlx::Result<Vec<Deployment>> { |
| 1662 | 2× | sqlx::query_as( |
| 1663 | 2× | "SELECT j.environment, j.name AS job, r.number AS run_number, r.state, r.bookmark, r.actor, j.approved_by, \ |
| 1664 | 2× | COALESCE(j.finished_ms, 0) AS finished_ms FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id \ |
| 1665 | 2× | WHERE r.repo_id = $1 AND j.environment != '' AND j.status = 'passed' ORDER BY j.finished_ms DESC LIMIT $2", |
| 1666 | 2× | ) |
| 1667 | 2× | .bind(repo_id) |
| 1668 | 2× | .bind(limit) |
| 1669 | 2× | .fetch_all(pool) |
| 1670 | 2× | .await |
| 1671 | 2× | } |