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