| 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 |
#[sqlx(default)] |
| 15 |
pub trigger: String, |
| 16 |
pub bookmark: String, |
| 17 |
pub state: String, |
| 18 |
pub heads: String, |
| 19 |
pub pipeline_hash: String, |
| 20 |
pub status: String, |
| 21 |
pub error: String, |
| 22 |
pub actor: String, |
| 23 |
pub created_ms: i64, |
| 24 |
pub finished_ms: Option<i64>, |
| 25 |
} |
| 26 |
|
| 27 |
impl Run { |
| 28 |
|
| 29 |
pub fn started_by(&self) -> String { |
| 30 |
match self.trigger.as_str() { |
| 31 |
"manual" => format!("started by {}", self.actor), |
| 32 |
"schedule" => "started on schedule".to_string(), |
| 33 |
t if t.starts_with("tag:") => format!("tag {} pushed by {}", &t[4..], self.actor), |
| 34 |
t if t.starts_with("rerun:") => format!("re-run of #{} by {}", self.rerun_of().unwrap_or(""), self.actor), |
| 35 |
_ => format!("pushed by {}", self.actor), |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
pub fn rerun_of(&self) -> Option<&str> { |
| 41 |
let rest = self.trigger.strip_prefix("rerun:")?; |
| 42 |
Some(rest.split(':').next().unwrap_or(rest)) |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
pub fn trigger_label(&self) -> String { |
| 47 |
match self.rerun_of() { |
| 48 |
Some(n) => format!("rerun:{n}"), |
| 49 |
None => self.trigger.clone(), |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
pub fn head_list(&self) -> Vec<&str> { |
| 54 |
self.heads.split('+').filter(|h| !h.is_empty()).collect() |
| 55 |
} |
| 56 |
|
| 57 |
pub fn live(&self) -> bool { |
| 58 |
matches!(self.status.as_str(), "queued" | "running") |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
#[derive(Clone, Debug, Default, FromRow)] |
| 63 |
pub struct Job { |
| 64 |
pub id: i64, |
| 65 |
pub run_id: i64, |
| 66 |
pub name: String, |
| 67 |
pub depth: i64, |
| 68 |
pub depends: String, |
| 69 |
pub spec: String, |
| 70 |
pub sandbox: String, |
| 71 |
pub runtime: String, |
| 72 |
pub status: String, |
| 73 |
pub runner: String, |
| 74 |
pub token_hash: String, |
| 75 |
pub started_ms: Option<i64>, |
| 76 |
pub finished_ms: Option<i64>, |
| 77 |
pub exit_code: Option<i64>, |
| 78 |
pub error: String, |
| 79 |
pub receipt: String, |
| 80 |
pub receipt_hash: String, |
| 81 |
pub log_count: i64, |
| 82 |
|
| 83 |
pub also_read: String, |
| 84 |
|
| 85 |
pub receipt_attest: String, |
| 86 |
|
| 87 |
pub receipt_signer: String, |
| 88 |
|
| 89 |
pub receipt_note: String, |
| 90 |
|
| 91 |
|
| 92 |
pub waiting: String, |
| 93 |
|
| 94 |
pub peak_disk_mib: Option<i64>, |
| 95 |
pub peak_memory_mib: Option<i64>, |
| 96 |
|
| 97 |
pub need_disk_mib: Option<i64>, |
| 98 |
|
| 99 |
pub placed: String, |
| 100 |
|
| 101 |
pub approval: String, |
| 102 |
|
| 103 |
pub approved_by: String, |
| 104 |
pub approved_ms: Option<i64>, |
| 105 |
|
| 106 |
pub environment: String, |
| 107 |
|
| 108 |
pub approval_seal: String, |
| 109 |
} |
| 110 |
|
| 111 |
impl Job { |
| 112 |
pub fn depends_on(&self) -> Vec<String> { |
| 113 |
serde_json::from_str(&self.depends).unwrap_or_default() |
| 114 |
} |
| 115 |
|
| 116 |
pub fn done(&self) -> bool { |
| 117 |
matches!(self.status.as_str(), "passed" | "failed" | "skipped" | "error" | "cancelled") |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
#[derive(Clone, Debug, FromRow)] |
| 122 |
pub struct LogLine { |
| 123 |
pub seq: i64, |
| 124 |
pub t: i64, |
| 125 |
pub stream: String, |
| 126 |
pub text: String, |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
pub struct NewJob { |
| 131 |
pub name: String, |
| 132 |
pub depth: i64, |
| 133 |
pub depends: Vec<String>, |
| 134 |
pub spec: String, |
| 135 |
pub sandbox: String, |
| 136 |
pub runtime: String, |
| 137 |
|
| 138 |
pub need_disk_mib: Option<i64>, |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
const NEED_FROM_RUNS: i64 = 5; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
pub async fn learned_disk_need(pool: &Pool, repo_id: i64, name: &str) -> sqlx::Result<Option<i64>> { |
| 148 |
let peak: Option<i64> = sqlx::query_scalar( |
| 149 |
"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 \ |
| 150 |
WHERE r.repo_id = $1 AND j.name = $2 AND j.peak_disk_mib IS NOT NULL ORDER BY j.id DESC LIMIT $3)", |
| 151 |
) |
| 152 |
.bind(repo_id) |
| 153 |
.bind(name) |
| 154 |
.bind(NEED_FROM_RUNS) |
| 155 |
.fetch_one(pool) |
| 156 |
.await?; |
| 157 |
Ok(peak.map(|p| p + p / 4 + 64)) |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
pub async fn set_placed(pool: &Pool, job_id: i64, placed: &str) -> sqlx::Result<()> { |
| 162 |
sqlx::query("UPDATE flow_jobs SET placed = $1 WHERE id = $2").bind(placed).bind(job_id).execute(pool).await?; |
| 163 |
Ok(()) |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
pub async fn set_peaks(pool: &Pool, job_id: i64, disk_mib: Option<u64>, memory_mib: Option<u64>) -> sqlx::Result<()> { |
| 168 |
sqlx::query("UPDATE flow_jobs SET peak_disk_mib = $1, peak_memory_mib = $2 WHERE id = $3") |
| 169 |
.bind(disk_mib.map(|v| v.min(i64::MAX as u64) as i64)) |
| 170 |
.bind(memory_mib.map(|v| v.min(i64::MAX as u64) as i64)) |
| 171 |
.bind(job_id) |
| 172 |
.execute(pool) |
| 173 |
.await?; |
| 174 |
Ok(()) |
| 175 |
} |
| 176 |
|
| 177 |
pub struct NewRun<'a> { |
| 178 |
pub repo_id: i64, |
| 179 |
pub bookmark: &'a str, |
| 180 |
pub state: &'a str, |
| 181 |
pub heads: &'a str, |
| 182 |
pub pipeline_hash: &'a str, |
| 183 |
pub actor: &'a str, |
| 184 |
|
| 185 |
pub trigger: &'a str, |
| 186 |
|
| 187 |
pub error: Option<&'a str>, |
| 188 |
} |
| 189 |
|
| 190 |
pub async fn create_run(pool: &Pool, r: NewRun<'_>, jobs: &[NewJob]) -> sqlx::Result<i64> { |
| 191 |
let mut tx = super::begin_write(pool).await?; |
| 192 |
let now = now_ms(); |
| 193 |
let status = if r.error.is_some() { "error" } else { "queued" }; |
| 194 |
let id: i64 = sqlx::query_scalar( |
| 195 |
"INSERT INTO flow_runs (repo_id, number, bookmark, state, heads, pipeline_hash, status, error, actor, trigger, created_ms, finished_ms) \ |
| 196 |
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", |
| 197 |
) |
| 198 |
.bind(r.repo_id) |
| 199 |
.bind(r.repo_id) |
| 200 |
.bind(r.bookmark) |
| 201 |
.bind(r.state) |
| 202 |
.bind(r.heads) |
| 203 |
.bind(r.pipeline_hash) |
| 204 |
.bind(status) |
| 205 |
.bind(r.error.unwrap_or("")) |
| 206 |
.bind(r.actor) |
| 207 |
.bind(r.trigger) |
| 208 |
.bind(now) |
| 209 |
.bind(r.error.map(|_| now)) |
| 210 |
.fetch_one(&mut *tx) |
| 211 |
.await?; |
| 212 |
for j in jobs { |
| 213 |
sqlx::query( |
| 214 |
"INSERT INTO flow_jobs (run_id, name, depth, depends, spec, sandbox, runtime, status, need_disk_mib, approval, environment) \ |
| 215 |
VALUES ($1, $2, $3, $4, $5, $6, $7, 'queued', $8, $9, $10)", |
| 216 |
) |
| 217 |
.bind(id) |
| 218 |
.bind(&j.name) |
| 219 |
.bind(j.depth) |
| 220 |
.bind(serde_json::to_string(&j.depends).unwrap_or_else(|_| "[]".into())) |
| 221 |
.bind(&j.spec) |
| 222 |
.bind(&j.sandbox) |
| 223 |
.bind(&j.runtime) |
| 224 |
.bind(j.need_disk_mib) |
| 225 |
.bind(if needs_approval(&j.spec) { "waiting" } else { "" }) |
| 226 |
.bind(environment_of(&j.spec)) |
| 227 |
.execute(&mut *tx) |
| 228 |
.await?; |
| 229 |
} |
| 230 |
tx.commit().await?; |
| 231 |
Ok(id) |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
fn environment_of(spec: &str) -> String { |
| 236 |
serde_json::from_str::<serde_json::Value>(spec).ok().and_then(|v| v["environment"].as_str().map(String::from)).unwrap_or_default() |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
fn needs_approval(spec: &str) -> bool { |
| 241 |
serde_json::from_str::<serde_json::Value>(spec).is_ok_and(|v| !v["approval"].is_null()) |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
fn labels_in(spec: &str, sandbox: &str) -> Vec<String> { |
| 246 |
let mut out: Vec<String> = [spec, sandbox] |
| 247 |
.iter() |
| 248 |
.filter_map(|j| serde_json::from_str::<serde_json::Value>(j).ok()) |
| 249 |
.flat_map(|v| v["runs_on"].as_array().cloned().unwrap_or_default()) |
| 250 |
.filter_map(|l| l.as_str().map(String::from)) |
| 251 |
.collect(); |
| 252 |
out.sort(); |
| 253 |
out.dedup(); |
| 254 |
out |
| 255 |
} |
| 256 |
|
| 257 |
impl Job { |
| 258 |
|
| 259 |
pub fn needs(&self) -> Vec<String> { |
| 260 |
labels_in(&self.spec, &self.sandbox) |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
pub fn arch(&self) -> Option<String> { |
| 265 |
arch_in(&self.sandbox) |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
fn arch_in(sandbox: &str) -> Option<String> { |
| 271 |
serde_json::from_str::<serde_json::Value>(sandbox).ok()?["arch"].as_str().map(String::from) |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
#[derive(Clone, Debug, FromRow)] |
| 276 |
pub struct QueueEntry { |
| 277 |
pub job_id: i64, |
| 278 |
pub run_id: i64, |
| 279 |
pub repo_id: i64, |
| 280 |
pub owner: String, |
| 281 |
pub name: String, |
| 282 |
pub visibility: String, |
| 283 |
pub run_number: i64, |
| 284 |
pub job: String, |
| 285 |
pub status: String, |
| 286 |
|
| 287 |
pub runner: String, |
| 288 |
pub spec: String, |
| 289 |
pub sandbox: String, |
| 290 |
} |
| 291 |
|
| 292 |
impl QueueEntry { |
| 293 |
|
| 294 |
pub fn needs(&self) -> Vec<String> { |
| 295 |
labels_in(&self.spec, &self.sandbox) |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
pub fn arch(&self) -> Option<String> { |
| 300 |
arch_in(&self.sandbox) |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
pub async fn queue(pool: &Pool) -> sqlx::Result<Vec<QueueEntry>> { |
| 307 |
sqlx::query_as( |
| 308 |
"SELECT j.id AS job_id, j.run_id, r.repo_id, p.owner, p.name, p.visibility, r.number AS run_number, j.name AS job, j.status, j.runner, j.spec, j.sandbox \ |
| 309 |
FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id JOIN repositories p ON p.id = r.repo_id \ |
| 310 |
WHERE j.status IN ('queued', 'running') AND j.approval != 'waiting' AND r.status IN ('queued', 'running') \ |
| 311 |
ORDER BY CASE j.status WHEN 'running' THEN 0 ELSE 1 END, j.run_id, j.depth, j.id", |
| 312 |
) |
| 313 |
.fetch_all(pool) |
| 314 |
.await |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
pub async fn runs_by_number(pool: &Pool, repo_id: i64, limit: i64, before: Option<i64>) -> sqlx::Result<Vec<Run>> { |
| 319 |
sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = $1 AND number < $2 ORDER BY number DESC LIMIT $3") |
| 320 |
.bind(repo_id) |
| 321 |
.bind(before.unwrap_or(i64::MAX)) |
| 322 |
.bind(limit) |
| 323 |
.fetch_all(pool) |
| 324 |
.await |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
pub async fn latest_on(pool: &Pool, repo_id: i64, bookmark: &str) -> sqlx::Result<Option<Run>> { |
| 333 |
sqlx::query_as( |
| 334 |
"SELECT * FROM flow_runs WHERE repo_id = $1 AND bookmark = $2 AND heads != '' AND status != 'cancelled' |
| 335 |
AND NOT (trigger = 'push' AND status IN ('queued', 'running')) ORDER BY id DESC LIMIT 1", |
| 336 |
) |
| 337 |
.bind(repo_id) |
| 338 |
.bind(bookmark) |
| 339 |
.fetch_optional(pool) |
| 340 |
.await |
| 341 |
} |
| 342 |
|
| 343 |
pub async fn runs(pool: &Pool, repo_id: i64, limit: i64, before: Option<i64>) -> sqlx::Result<Vec<Run>> { |
| 344 |
sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = $1 AND id < $2 ORDER BY id DESC LIMIT $3") |
| 345 |
.bind(repo_id) |
| 346 |
.bind(before.unwrap_or(i64::MAX)) |
| 347 |
.bind(limit) |
| 348 |
.fetch_all(pool) |
| 349 |
.await |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
pub async fn run(pool: &Pool, repo_id: i64, number: i64) -> sqlx::Result<Option<Run>> { |
| 354 |
sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = $1 AND number = $2").bind(repo_id).bind(number).fetch_optional(pool).await |
| 355 |
} |
| 356 |
|
| 357 |
pub async fn run_by_id(pool: &Pool, id: i64) -> sqlx::Result<Option<Run>> { |
| 358 |
sqlx::query_as("SELECT * FROM flow_runs WHERE id = $1").bind(id).fetch_optional(pool).await |
| 359 |
} |
| 360 |
|
| 361 |
pub async fn jobs(pool: &Pool, run_id: i64) -> sqlx::Result<Vec<Job>> { |
| 362 |
sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = $1 ORDER BY depth, name").bind(run_id).fetch_all(pool).await |
| 363 |
} |
| 364 |
|
| 365 |
pub async fn job(pool: &Pool, id: i64) -> sqlx::Result<Option<Job>> { |
| 366 |
sqlx::query_as("SELECT * FROM flow_jobs WHERE id = $1").bind(id).fetch_optional(pool).await |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
pub async fn job_by_token(pool: &Pool, token_hash: &str, max_age_ms: i64) -> sqlx::Result<Option<Job>> { |
| 371 |
sqlx::query_as("SELECT * FROM flow_jobs WHERE token_hash = $1 AND status = 'running' AND started_ms >= $2") |
| 372 |
.bind(token_hash) |
| 373 |
.bind(now_ms() - max_age_ms) |
| 374 |
.fetch_optional(pool) |
| 375 |
.await |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
pub async fn set_also_read(pool: &Pool, job_id: i64, repo_ids: &[i64]) -> sqlx::Result<()> { |
| 382 |
let list: Vec<String> = repo_ids.iter().map(|i| i.to_string()).collect(); |
| 383 |
sqlx::query("UPDATE flow_jobs SET also_read = $1 WHERE id = $2").bind(list.join(",")).bind(job_id).execute(pool).await?; |
| 384 |
Ok(()) |
| 385 |
} |
| 386 |
|
| 387 |
impl Job { |
| 388 |
pub fn also_read_ids(&self) -> Vec<i64> { |
| 389 |
self.also_read.split(',').filter_map(|x| x.trim().parse().ok()).collect() |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
#[derive(Clone, Debug, Default)] |
| 396 |
pub struct Room { |
| 397 |
pub memory_mib: Option<u32>, |
| 398 |
pub cpus: Option<u32>, |
| 399 |
pub images: Option<std::collections::HashSet<String>>, |
| 400 |
|
| 401 |
pub arch: String, |
| 402 |
|
| 403 |
pub image_archs: std::collections::HashMap<String, String>, |
| 404 |
|
| 405 |
pub declined: std::collections::HashSet<i64>, |
| 406 |
|
| 407 |
|
| 408 |
pub fetchable: std::collections::HashSet<String>, |
| 409 |
|
| 410 |
|
| 411 |
pub free_disk_mib: Option<u64>, |
| 412 |
} |
| 413 |
|
| 414 |
impl Room { |
| 415 |
|
| 416 |
pub fn disk_fits(&self, need: Option<i64>) -> bool { |
| 417 |
match (need, self.free_disk_mib) { |
| 418 |
(Some(n), Some(free)) => n.max(0) as u64 <= free, |
| 419 |
_ => true, |
| 420 |
} |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
impl Room { |
| 425 |
|
| 426 |
|
| 427 |
pub fn fits(&self, sandbox_json: &str) -> bool { |
| 428 |
let v: serde_json::Value = serde_json::from_str(sandbox_json).unwrap_or_default(); |
| 429 |
let need = |k: &str| v[k].as_u64().unwrap_or(0); |
| 430 |
let rootfs = v["rootfs"].as_str().unwrap_or(""); |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
let wanted = v["arch"].as_str().map(String::from).or_else(|| self.image_archs.get(rootfs).cloned()); |
| 435 |
let arch_ok = self.arch.is_empty() || wanted.as_deref().is_none_or(|w| w == self.arch); |
| 436 |
arch_ok |
| 437 |
&& self.memory_mib.is_none_or(|m| need("memory_mib") <= m as u64) |
| 438 |
&& self.cpus.is_none_or(|c| need("cpus") <= c as u64) |
| 439 |
&& (rootfs.is_empty() || self.images.as_ref().is_none_or(|have| have.contains(rootfs)) || self.fetchable.contains(rootfs)) |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
pub async fn record_image_archs(pool: &Pool, archs: &std::collections::BTreeMap<String, String>) -> sqlx::Result<()> { |
| 445 |
for (hash, arch) in archs.iter().take(500) { |
| 446 |
if hash.len() != 64 || !flow_core::arch_ok(arch) { |
| 447 |
continue; |
| 448 |
} |
| 449 |
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") |
| 450 |
.bind(hash.to_ascii_lowercase()) |
| 451 |
.bind(arch) |
| 452 |
.bind(now_ms()) |
| 453 |
.execute(pool) |
| 454 |
.await?; |
| 455 |
} |
| 456 |
Ok(()) |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
pub async fn learn_archs_from_runs(pool: &Pool) -> sqlx::Result<u64> { |
| 463 |
let rootfs = crate::db::json_text("j.sandbox", "rootfs"); |
| 464 |
let r = sqlx::query(&format!( |
| 465 |
"INSERT INTO flow_images (hash, arch, seen_ms) |
| 466 |
SELECT lower({rootfs}), MIN(r.arch), $1 FROM flow_jobs j JOIN flow_runners r ON r.id = j.runner_id |
| 467 |
WHERE j.status = 'passed' AND r.arch != '' AND length({rootfs}) = 64 |
| 468 |
GROUP BY lower({rootfs}) |
| 469 |
ON CONFLICT (hash) DO NOTHING" |
| 470 |
)) |
| 471 |
.bind(now_ms()) |
| 472 |
.execute(pool) |
| 473 |
.await?; |
| 474 |
Ok(r.rows_affected()) |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
pub async fn image_archs(pool: &Pool) -> sqlx::Result<std::collections::HashMap<String, String>> { |
| 479 |
let rows: Vec<(String, String)> = sqlx::query_as("SELECT hash, arch FROM flow_images").fetch_all(pool).await?; |
| 480 |
Ok(rows.into_iter().collect()) |
| 481 |
} |
| 482 |
|
| 483 |
pub async fn claim(pool: &Pool, runtimes: &[String], runner: &str, token_hash: &str) -> sqlx::Result<Option<Job>> { |
| 484 |
claim_within(pool, runtimes, runner, token_hash, Room::default(), &Default::default(), &[]).await |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
pub async fn claim_within( |
| 492 |
pool: &Pool, |
| 493 |
runtimes: &[String], |
| 494 |
runner: &str, |
| 495 |
token_hash: &str, |
| 496 |
room: Room, |
| 497 |
scope: &super::runners::Scope, |
| 498 |
labels: &[String], |
| 499 |
) -> sqlx::Result<Option<Job>> { |
| 500 |
claim_ranked(pool, runtimes, runner, token_hash, room, scope, labels, &|_| false).await |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
#[allow(clippy::too_many_arguments)] |
| 506 |
pub async fn claim_ranked( |
| 507 |
pool: &Pool, |
| 508 |
runtimes: &[String], |
| 509 |
runner: &str, |
| 510 |
token_hash: &str, |
| 511 |
room: Room, |
| 512 |
scope: &super::runners::Scope, |
| 513 |
labels: &[String], |
| 514 |
elsewhere: &(dyn Fn(&Job) -> bool + Sync), |
| 515 |
) -> sqlx::Result<Option<Job>> { |
| 516 |
let mut tx = super::begin_write(pool).await?; |
| 517 |
let queued: Vec<Job> = sqlx::query_as( |
| 518 |
"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 \ |
| 519 |
WHERE j.status = 'queued' AND j.approval != 'waiting' AND r.status IN ('queued', 'running') \ |
| 520 |
AND ($1 = '' OR rep.owner = $2) AND ($3 IS NULL OR r.repo_id = $4) \ |
| 521 |
AND NOT ($5 = '' AND $6 IS NULL AND rep.flow = 'own') \ |
| 522 |
ORDER BY j.run_id, j.depth, j.id LIMIT 200", |
| 523 |
) |
| 524 |
.bind(&scope.owner) |
| 525 |
.bind(&scope.owner) |
| 526 |
.bind(scope.repo_id) |
| 527 |
.bind(scope.repo_id) |
| 528 |
|
| 529 |
.bind(&scope.owner) |
| 530 |
.bind(scope.repo_id) |
| 531 |
.fetch_all(&mut *tx) |
| 532 |
.await?; |
| 533 |
for j in queued { |
| 534 |
if !runtimes.iter().any(|r| r == &j.runtime) |
| 535 |
|| !room.fits(&j.sandbox) |
| 536 |
|| !room.disk_fits(j.need_disk_mib) |
| 537 |
|| room.declined.contains(&j.id) |
| 538 |
{ |
| 539 |
continue; |
| 540 |
} |
| 541 |
|
| 542 |
if !j.needs().iter().all(|l| labels.contains(l)) { |
| 543 |
continue; |
| 544 |
} |
| 545 |
let deps = j.depends_on(); |
| 546 |
if !deps.is_empty() { |
| 547 |
let mut passed = 0i64; |
| 548 |
for d in &deps { |
| 549 |
let st: Option<String> = sqlx::query_scalar("SELECT status FROM flow_jobs WHERE run_id = $1 AND name = $2") |
| 550 |
.bind(j.run_id) |
| 551 |
.bind(d) |
| 552 |
.fetch_optional(&mut *tx) |
| 553 |
.await?; |
| 554 |
if st.as_deref() == Some("passed") { |
| 555 |
passed += 1; |
| 556 |
} |
| 557 |
} |
| 558 |
if passed != deps.len() as i64 { |
| 559 |
continue; |
| 560 |
} |
| 561 |
} |
| 562 |
if elsewhere(&j) { |
| 563 |
continue; |
| 564 |
} |
| 565 |
let now = now_ms(); |
| 566 |
let done = sqlx::query( |
| 567 |
"UPDATE flow_jobs SET status = 'running', runner = $1, token_hash = $2, started_ms = $3, waiting = '' WHERE id = $4 AND status = 'queued'", |
| 568 |
) |
| 569 |
.bind(runner) |
| 570 |
.bind(token_hash) |
| 571 |
.bind(now) |
| 572 |
.bind(j.id) |
| 573 |
.execute(&mut *tx) |
| 574 |
.await?; |
| 575 |
if done.rows_affected() == 1 { |
| 576 |
sqlx::query("UPDATE flow_runs SET status = 'running' WHERE id = $1 AND status = 'queued'") |
| 577 |
.bind(j.run_id) |
| 578 |
.execute(&mut *tx) |
| 579 |
.await?; |
| 580 |
tx.commit().await?; |
| 581 |
return job(pool, j.id).await; |
| 582 |
} |
| 583 |
} |
| 584 |
tx.commit().await?; |
| 585 |
Ok(None) |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
pub const DECLINE_HOLDS_MS: i64 = 10 * 60_000; |
| 591 |
|
| 592 |
|
| 593 |
pub async fn declined_by(pool: &Pool, runner_id: i64, profile: &str) -> sqlx::Result<std::collections::HashSet<i64>> { |
| 594 |
let rows: Vec<(i64,)> = sqlx::query_as("SELECT job_id FROM flow_declines WHERE runner_id = $1 AND profile = $2 AND ms > $3") |
| 595 |
.bind(runner_id) |
| 596 |
.bind(profile) |
| 597 |
.bind(now_ms() - DECLINE_HOLDS_MS) |
| 598 |
.fetch_all(pool) |
| 599 |
.await?; |
| 600 |
Ok(rows.into_iter().map(|(j,)| j).collect()) |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
#[derive(Clone, Debug, sqlx::FromRow)] |
| 605 |
pub struct DeclineRow { |
| 606 |
pub job_id: i64, |
| 607 |
pub runner_id: i64, |
| 608 |
pub runner: String, |
| 609 |
pub reason: String, |
| 610 |
pub ms: i64, |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
pub async fn decline( |
| 618 |
pool: &Pool, |
| 619 |
job_id: i64, |
| 620 |
runner_id: i64, |
| 621 |
runner: &str, |
| 622 |
profile: &str, |
| 623 |
reason: &str, |
| 624 |
) -> sqlx::Result<Option<Vec<DeclineRow>>> { |
| 625 |
let mut tx = super::begin_write(pool).await?; |
| 626 |
let back = sqlx::query( |
| 627 |
"UPDATE flow_jobs SET status = 'queued', runner = '', token_hash = '', started_ms = NULL, runner_id = NULL \ |
| 628 |
WHERE id = $1 AND status = 'running' AND runner_id = $2", |
| 629 |
) |
| 630 |
.bind(job_id) |
| 631 |
.bind(runner_id) |
| 632 |
.execute(&mut *tx) |
| 633 |
.await?; |
| 634 |
if back.rows_affected() != 1 { |
| 635 |
tx.rollback().await?; |
| 636 |
return Ok(None); |
| 637 |
} |
| 638 |
sqlx::query( |
| 639 |
"INSERT INTO flow_declines (job_id, runner_id, runner, profile, reason, ms) VALUES ($1, $2, $3, $4, $5, $6) \ |
| 640 |
ON CONFLICT (job_id, runner_id) DO UPDATE SET runner = excluded.runner, profile = excluded.profile, reason = excluded.reason, ms = excluded.ms", |
| 641 |
) |
| 642 |
.bind(job_id) |
| 643 |
.bind(runner_id) |
| 644 |
.bind(runner) |
| 645 |
.bind(profile) |
| 646 |
.bind(reason) |
| 647 |
.bind(now_ms()) |
| 648 |
.execute(&mut *tx) |
| 649 |
.await?; |
| 650 |
let all: Vec<DeclineRow> = |
| 651 |
sqlx::query_as("SELECT job_id, runner_id, runner, reason, ms FROM flow_declines WHERE job_id = $1 ORDER BY ms") |
| 652 |
.bind(job_id) |
| 653 |
.fetch_all(&mut *tx) |
| 654 |
.await?; |
| 655 |
tx.commit().await?; |
| 656 |
Ok(Some(all)) |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
pub async fn declines_of_job(pool: &Pool, job_id: i64) -> sqlx::Result<Vec<DeclineRow>> { |
| 661 |
sqlx::query_as("SELECT job_id, runner_id, runner, reason, ms FROM flow_declines WHERE job_id = $1 ORDER BY ms") |
| 662 |
.bind(job_id) |
| 663 |
.fetch_all(pool) |
| 664 |
.await |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
#[derive(Clone, Debug, sqlx::FromRow)] |
| 669 |
pub struct RunnerDecline { |
| 670 |
pub job_id: i64, |
| 671 |
pub job: String, |
| 672 |
pub run_number: i64, |
| 673 |
pub owner: String, |
| 674 |
pub name: String, |
| 675 |
pub reason: String, |
| 676 |
pub ms: i64, |
| 677 |
} |
| 678 |
|
| 679 |
pub async fn declines_of_runner(pool: &Pool, runner_id: i64, limit: i64) -> sqlx::Result<Vec<RunnerDecline>> { |
| 680 |
sqlx::query_as( |
| 681 |
"SELECT d.job_id, j.name AS job, r.number AS run_number, rep.owner, rep.name, d.reason, d.ms \ |
| 682 |
FROM flow_declines d JOIN flow_jobs j ON j.id = d.job_id JOIN flow_runs r ON r.id = j.run_id \ |
| 683 |
JOIN repositories rep ON rep.id = r.repo_id WHERE d.runner_id = $1 ORDER BY d.ms DESC LIMIT $2", |
| 684 |
) |
| 685 |
.bind(runner_id) |
| 686 |
.bind(limit) |
| 687 |
.fetch_all(pool) |
| 688 |
.await |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
pub async fn ready_queued(pool: &Pool) -> sqlx::Result<Vec<(Job, String, i64)>> { |
| 694 |
let queued: Vec<Job> = sqlx::query_as( |
| 695 |
"SELECT j.* FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id \ |
| 696 |
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", |
| 697 |
) |
| 698 |
.fetch_all(pool) |
| 699 |
.await?; |
| 700 |
let mut out = Vec::new(); |
| 701 |
for j in queued { |
| 702 |
let mut ready = true; |
| 703 |
for d in j.depends_on() { |
| 704 |
let st: Option<String> = sqlx::query_scalar("SELECT status FROM flow_jobs WHERE run_id = $1 AND name = $2") |
| 705 |
.bind(j.run_id) |
| 706 |
.bind(&d) |
| 707 |
.fetch_optional(pool) |
| 708 |
.await?; |
| 709 |
if st.as_deref() != Some("passed") { |
| 710 |
ready = false; |
| 711 |
break; |
| 712 |
} |
| 713 |
} |
| 714 |
if !ready { |
| 715 |
continue; |
| 716 |
} |
| 717 |
let repo: Option<(String, i64)> = |
| 718 |
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") |
| 719 |
.bind(j.run_id) |
| 720 |
.fetch_optional(pool) |
| 721 |
.await?; |
| 722 |
if let Some((owner, id)) = repo { |
| 723 |
out.push((j, owner, id)); |
| 724 |
} |
| 725 |
} |
| 726 |
Ok(out) |
| 727 |
} |
| 728 |
|
| 729 |
pub async fn set_waiting(pool: &Pool, job_id: i64, waiting: &str) -> sqlx::Result<()> { |
| 730 |
sqlx::query("UPDATE flow_jobs SET waiting = $1 WHERE id = $2 AND waiting <> $3") |
| 731 |
.bind(waiting) |
| 732 |
.bind(job_id) |
| 733 |
.bind(waiting) |
| 734 |
.execute(pool) |
| 735 |
.await?; |
| 736 |
Ok(()) |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
pub async fn append_log(pool: &Pool, job_id: i64, records: &[flow_core::api::Record], cap: i64) -> sqlx::Result<usize> { |
| 741 |
let mut tx = super::begin_write(pool).await?; |
| 742 |
let count: i64 = sqlx::query_scalar("SELECT log_count FROM flow_jobs WHERE id = $1").bind(job_id).fetch_one(&mut *tx).await?; |
| 743 |
let room = (cap - count).max(0) as usize; |
| 744 |
let keep = &records[..records.len().min(room)]; |
| 745 |
for (i, r) in keep.iter().enumerate() { |
| 746 |
sqlx::query("INSERT INTO flow_logs (job_id, seq, t, stream, text) VALUES ($1, $2, $3, $4, $5)") |
| 747 |
.bind(job_id) |
| 748 |
.bind(count + i as i64 + 1) |
| 749 |
.bind(r.t as i64) |
| 750 |
.bind(&r.stream) |
| 751 |
.bind(&r.text) |
| 752 |
.execute(&mut *tx) |
| 753 |
.await?; |
| 754 |
} |
| 755 |
sqlx::query("UPDATE flow_jobs SET log_count = log_count + $1 WHERE id = $2") |
| 756 |
.bind(keep.len() as i64) |
| 757 |
.bind(job_id) |
| 758 |
.execute(&mut *tx) |
| 759 |
.await?; |
| 760 |
tx.commit().await?; |
| 761 |
Ok(keep.len()) |
| 762 |
} |
| 763 |
|
| 764 |
pub async fn log(pool: &Pool, job_id: i64, after: i64, limit: i64) -> sqlx::Result<Vec<LogLine>> { |
| 765 |
sqlx::query_as("SELECT seq, t, stream, text FROM flow_logs WHERE job_id = $1 AND seq > $2 ORDER BY seq LIMIT $3") |
| 766 |
.bind(job_id) |
| 767 |
.bind(after) |
| 768 |
.bind(limit) |
| 769 |
.fetch_all(pool) |
| 770 |
.await |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
pub async fn running_past_limit(pool: &Pool, now: i64, default_s: u64, grace_ms: i64) -> sqlx::Result<Vec<Job>> { |
| 777 |
sqlx::query_as( |
| 778 |
"SELECT * FROM flow_jobs WHERE status = 'running' \ |
| 779 |
AND COALESCE(started_ms, 0) + (CASE WHEN timeout_s > 0 THEN timeout_s ELSE $1 END) * 1000 + $2 < $3 ORDER BY started_ms", |
| 780 |
) |
| 781 |
.bind(default_s as i64) |
| 782 |
.bind(grace_ms) |
| 783 |
.bind(now) |
| 784 |
.fetch_all(pool) |
| 785 |
.await |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
pub async fn set_job_timeout(pool: &Pool, job_id: i64, timeout_s: u64) -> sqlx::Result<()> { |
| 790 |
sqlx::query("UPDATE flow_jobs SET timeout_s = $1 WHERE id = $2").bind(timeout_s as i64).bind(job_id).execute(pool).await?; |
| 791 |
Ok(()) |
| 792 |
} |
| 793 |
|
| 794 |
pub async fn running_since_before(pool: &Pool, ms: i64) -> sqlx::Result<Vec<Job>> { |
| 795 |
sqlx::query_as("SELECT * FROM flow_jobs WHERE status = 'running' AND COALESCE(started_ms, 0) < $1 ORDER BY started_ms") |
| 796 |
.bind(ms) |
| 797 |
.fetch_all(pool) |
| 798 |
.await |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
pub async fn running_on(pool: &Pool, runner_id: i64) -> sqlx::Result<Vec<Job>> { |
| 803 |
sqlx::query_as("SELECT * FROM flow_jobs WHERE status = 'running' AND runner_id = $1 ORDER BY id").bind(runner_id).fetch_all(pool).await |
| 804 |
} |
| 805 |
|
| 806 |
pub struct Outcome<'a> { |
| 807 |
pub status: &'a str, |
| 808 |
pub exit_code: Option<i64>, |
| 809 |
pub error: &'a str, |
| 810 |
pub receipt: &'a str, |
| 811 |
pub receipt_hash: &'a str, |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
pub async fn finish(pool: &Pool, job_id: i64, o: Outcome<'_>) -> sqlx::Result<Vec<Job>> { |
| 817 |
let mut tx = super::begin_write(pool).await?; |
| 818 |
let now = now_ms(); |
| 819 |
let j: Job = sqlx::query_as("SELECT * FROM flow_jobs WHERE id = $1").bind(job_id).fetch_one(&mut *tx).await?; |
| 820 |
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") |
| 821 |
.bind(o.status) |
| 822 |
.bind(o.exit_code) |
| 823 |
.bind(o.error) |
| 824 |
.bind(o.receipt) |
| 825 |
.bind(o.receipt_hash) |
| 826 |
.bind(now) |
| 827 |
.bind(job_id) |
| 828 |
.execute(&mut *tx) |
| 829 |
.await?; |
| 830 |
|
| 831 |
|
| 832 |
if o.status == "passed" { |
| 833 |
let rootfs = serde_json::from_str::<serde_json::Value>(&j.sandbox) |
| 834 |
.ok() |
| 835 |
.and_then(|v| v["rootfs"].as_str().map(str::to_ascii_lowercase)) |
| 836 |
.unwrap_or_default(); |
| 837 |
if rootfs.len() == 64 { |
| 838 |
sqlx::query( |
| 839 |
"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 \ |
| 840 |
WHERE j.id = $3 AND r.arch != '' ON CONFLICT (hash) DO NOTHING", |
| 841 |
) |
| 842 |
.bind(&rootfs) |
| 843 |
.bind(now) |
| 844 |
.bind(job_id) |
| 845 |
.execute(&mut *tx) |
| 846 |
.await?; |
| 847 |
} |
| 848 |
} |
| 849 |
let mut changed = vec![job_id]; |
| 850 |
|
| 851 |
loop { |
| 852 |
let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = $1").bind(j.run_id).fetch_all(&mut *tx).await?; |
| 853 |
let blocked: Vec<i64> = all |
| 854 |
.iter() |
| 855 |
.filter(|x| x.status == "queued") |
| 856 |
.filter(|x| { |
| 857 |
x.depends_on() |
| 858 |
.iter() |
| 859 |
.any(|d| all.iter().any(|y| &y.name == d && matches!(y.status.as_str(), "failed" | "skipped" | "error" | "cancelled"))) |
| 860 |
}) |
| 861 |
.map(|x| x.id) |
| 862 |
.collect(); |
| 863 |
if blocked.is_empty() { |
| 864 |
break; |
| 865 |
} |
| 866 |
for id in blocked { |
| 867 |
sqlx::query("UPDATE flow_jobs SET status = 'skipped', finished_ms = $1 WHERE id = $2") |
| 868 |
.bind(now) |
| 869 |
.bind(id) |
| 870 |
.execute(&mut *tx) |
| 871 |
.await?; |
| 872 |
changed.push(id); |
| 873 |
} |
| 874 |
} |
| 875 |
let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = $1").bind(j.run_id).fetch_all(&mut *tx).await?; |
| 876 |
if all.iter().all(Job::done) { |
| 877 |
let status = if all.iter().all(|x| x.status == "passed") { "passed" } else { "failed" }; |
| 878 |
sqlx::query("UPDATE flow_runs SET status = $1, finished_ms = $2 WHERE id = $3") |
| 879 |
.bind(status) |
| 880 |
.bind(now) |
| 881 |
.bind(j.run_id) |
| 882 |
.execute(&mut *tx) |
| 883 |
.await?; |
| 884 |
} |
| 885 |
tx.commit().await?; |
| 886 |
let mut out = Vec::new(); |
| 887 |
for id in changed { |
| 888 |
if let Some(x) = job(pool, id).await? { |
| 889 |
out.push(x); |
| 890 |
} |
| 891 |
} |
| 892 |
Ok(out) |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
pub async fn cancel_run(pool: &Pool, run_id: i64, why: &str) -> sqlx::Result<Vec<Job>> { |
| 899 |
let now = now_ms(); |
| 900 |
let mut tx = pool.begin().await?; |
| 901 |
let ids: Vec<(i64,)> = sqlx::query_as("SELECT id FROM flow_jobs WHERE run_id = $1 AND status IN ('queued', 'running')") |
| 902 |
.bind(run_id) |
| 903 |
.fetch_all(&mut *tx) |
| 904 |
.await?; |
| 905 |
sqlx::query( |
| 906 |
"UPDATE flow_jobs SET status = 'cancelled', error = $1, finished_ms = $2, token_hash = '' |
| 907 |
WHERE run_id = $3 AND status IN ('queued', 'running')", |
| 908 |
) |
| 909 |
.bind(why) |
| 910 |
.bind(now) |
| 911 |
.bind(run_id) |
| 912 |
.execute(&mut *tx) |
| 913 |
.await?; |
| 914 |
sqlx::query("UPDATE flow_runs SET status = 'cancelled', finished_ms = $1 WHERE id = $2 AND status IN ('queued', 'running')") |
| 915 |
.bind(now) |
| 916 |
.bind(run_id) |
| 917 |
.execute(&mut *tx) |
| 918 |
.await?; |
| 919 |
tx.commit().await?; |
| 920 |
let mut out = Vec::new(); |
| 921 |
for (id,) in ids { |
| 922 |
if let Some(x) = job(pool, id).await? { |
| 923 |
out.push(x); |
| 924 |
} |
| 925 |
} |
| 926 |
Ok(out) |
| 927 |
} |
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
pub async fn rerun_job(pool: &Pool, run_id: i64, name: &str) -> sqlx::Result<Vec<Job>> { |
| 935 |
let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = $1").bind(run_id).fetch_all(pool).await?; |
| 936 |
if !all.iter().any(|j| j.name == name) { |
| 937 |
return Ok(Vec::new()); |
| 938 |
} |
| 939 |
let mut again = vec![name.to_string()]; |
| 940 |
loop { |
| 941 |
let more: Vec<String> = all |
| 942 |
.iter() |
| 943 |
.filter(|j| !again.contains(&j.name) && j.depends_on().iter().any(|d| again.contains(d))) |
| 944 |
.map(|j| j.name.clone()) |
| 945 |
.collect(); |
| 946 |
if more.is_empty() { |
| 947 |
break; |
| 948 |
} |
| 949 |
again.extend(more); |
| 950 |
} |
| 951 |
let ids: Vec<i64> = all.iter().filter(|j| again.contains(&j.name)).map(|j| j.id).collect(); |
| 952 |
let mut tx = pool.begin().await?; |
| 953 |
for id in &ids { |
| 954 |
for table in ["flow_logs", "flow_artifacts", "flow_declines", "test_reports", "coverage_reports", "reports"] { |
| 955 |
sqlx::query(&format!("DELETE FROM {table} WHERE job_id = $1")).bind(id).execute(&mut *tx).await?; |
| 956 |
} |
| 957 |
sqlx::query( |
| 958 |
"UPDATE flow_jobs SET status = 'queued', runner = '', token_hash = '', started_ms = NULL, finished_ms = NULL, |
| 959 |
exit_code = NULL, error = '', receipt = '', receipt_hash = '', log_count = 0, also_read = '', receipt_attest = '', |
| 960 |
receipt_signer = '', receipt_note = '', runner_id = NULL, waiting = '', placed = '', peak_disk_mib = NULL, |
| 961 |
peak_memory_mib = NULL, approval = CASE WHEN approval = '' THEN '' ELSE 'waiting' END, approved_by = '', |
| 962 |
approved_ms = NULL, approval_seal = '' WHERE id = $1", |
| 963 |
) |
| 964 |
.bind(id) |
| 965 |
.execute(&mut *tx) |
| 966 |
.await?; |
| 967 |
} |
| 968 |
sqlx::query("UPDATE flow_runs SET status = 'running', finished_ms = NULL WHERE id = $1").bind(run_id).execute(&mut *tx).await?; |
| 969 |
tx.commit().await?; |
| 970 |
let mut out = Vec::new(); |
| 971 |
for id in ids { |
| 972 |
if let Some(x) = job(pool, id).await? { |
| 973 |
out.push(x); |
| 974 |
} |
| 975 |
} |
| 976 |
Ok(out) |
| 977 |
} |
| 978 |
|
| 979 |
|
| 980 |
pub async fn live_push_runs_before(pool: &Pool, repo_id: i64, bookmark: &str, number: i64) -> sqlx::Result<Vec<Run>> { |
| 981 |
sqlx::query_as( |
| 982 |
"SELECT * FROM flow_runs WHERE repo_id = $1 AND bookmark = $2 AND number < $3 AND trigger = 'push' |
| 983 |
AND status IN ('queued', 'running') ORDER BY id", |
| 984 |
) |
| 985 |
.bind(repo_id) |
| 986 |
.bind(bookmark) |
| 987 |
.bind(number) |
| 988 |
.fetch_all(pool) |
| 989 |
.await |
| 990 |
} |
| 991 |
|
| 992 |
|
| 993 |
pub async fn latest_for_heads(pool: &Pool, repo_id: i64, heads: &str) -> sqlx::Result<Option<Run>> { |
| 994 |
sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = $1 AND heads = $2 ORDER BY id DESC LIMIT 1") |
| 995 |
.bind(repo_id) |
| 996 |
.bind(heads) |
| 997 |
.fetch_optional(pool) |
| 998 |
.await |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
pub async fn set_receipt(pool: &Pool, job_id: i64, attest: &str, signer: &str, note: &str) -> sqlx::Result<()> { |
| 1003 |
sqlx::query("UPDATE flow_jobs SET receipt_attest = $1, receipt_signer = $2, receipt_note = $3 WHERE id = $4") |
| 1004 |
.bind(attest) |
| 1005 |
.bind(signer) |
| 1006 |
.bind(note) |
| 1007 |
.bind(job_id) |
| 1008 |
.execute(pool) |
| 1009 |
.await?; |
| 1010 |
Ok(()) |
| 1011 |
} |
| 1012 |
|
| 1013 |
|
| 1014 |
#[derive(Clone, Debug, FromRow)] |
| 1015 |
pub struct Artifact { |
| 1016 |
pub job_id: i64, |
| 1017 |
pub name: String, |
| 1018 |
pub path: String, |
| 1019 |
pub blob: String, |
| 1020 |
pub size: i64, |
| 1021 |
|
| 1022 |
pub content: String, |
| 1023 |
pub created_ms: i64, |
| 1024 |
} |
| 1025 |
|
| 1026 |
pub async fn add_artifact(pool: &Pool, a: &Artifact) -> sqlx::Result<()> { |
| 1027 |
sqlx::query( |
| 1028 |
"INSERT OR REPLACE INTO flow_artifacts (job_id, name, path, blob, size, content, created_ms) VALUES ($1, $2, $3, $4, $5, $6, $7)", |
| 1029 |
) |
| 1030 |
.bind(a.job_id) |
| 1031 |
.bind(&a.name) |
| 1032 |
.bind(&a.path) |
| 1033 |
.bind(&a.blob) |
| 1034 |
.bind(a.size) |
| 1035 |
.bind(&a.content) |
| 1036 |
.bind(a.created_ms) |
| 1037 |
.execute(pool) |
| 1038 |
.await?; |
| 1039 |
Ok(()) |
| 1040 |
} |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
pub async fn artifact_blobs(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<String>> { |
| 1045 |
sqlx::query_scalar( |
| 1046 |
"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", |
| 1047 |
) |
| 1048 |
.bind(repo_id) |
| 1049 |
.fetch_all(pool) |
| 1050 |
.await |
| 1051 |
} |
| 1052 |
|
| 1053 |
|
| 1054 |
pub async fn artifacts(pool: &Pool, job_id: i64) -> sqlx::Result<Vec<Artifact>> { |
| 1055 |
sqlx::query_as("SELECT * FROM flow_artifacts WHERE job_id = $1 ORDER BY name").bind(job_id).fetch_all(pool).await |
| 1056 |
} |
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
#[derive(Clone, Debug, FromRow)] |
| 1061 |
pub struct Provenance { |
| 1062 |
pub run_number: i64, |
| 1063 |
pub job: String, |
| 1064 |
pub name: String, |
| 1065 |
pub receipt_attest: String, |
| 1066 |
pub receipt_signer: String, |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
pub async fn provenance(pool: &Pool, repo_id: i64, content: &str) -> sqlx::Result<Option<Provenance>> { |
| 1073 |
sqlx::query_as( |
| 1074 |
"SELECT r.number AS run_number, j.name AS job, a.name AS name, j.receipt_attest, j.receipt_signer \ |
| 1075 |
FROM flow_artifacts a JOIN flow_jobs j ON j.id = a.job_id JOIN flow_runs r ON r.id = j.run_id \ |
| 1076 |
WHERE r.repo_id = $1 AND a.content = $2 AND j.status = 'passed' \ |
| 1077 |
ORDER BY (j.receipt_attest != '') DESC, r.number DESC LIMIT 1", |
| 1078 |
) |
| 1079 |
.bind(repo_id) |
| 1080 |
.bind(content) |
| 1081 |
.fetch_optional(pool) |
| 1082 |
.await |
| 1083 |
} |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
pub async fn approve_job(pool: &Pool, id: i64, who: &str, seal: &str) -> sqlx::Result<bool> { |
| 1088 |
let r = sqlx::query( |
| 1089 |
"UPDATE flow_jobs SET approval = 'approved', approved_by = $1, approved_ms = $2, approval_seal = $3 \ |
| 1090 |
WHERE id = $4 AND status = 'queued' AND approval = 'waiting'", |
| 1091 |
) |
| 1092 |
.bind(who) |
| 1093 |
.bind(now_ms()) |
| 1094 |
.bind(seal) |
| 1095 |
.bind(id) |
| 1096 |
.execute(pool) |
| 1097 |
.await?; |
| 1098 |
Ok(r.rows_affected() > 0) |
| 1099 |
} |
| 1100 |
|
| 1101 |
|
| 1102 |
pub async fn note_rejection(pool: &Pool, id: i64, who: &str) -> sqlx::Result<()> { |
| 1103 |
sqlx::query("UPDATE flow_jobs SET approved_by = $1, approved_ms = $2 WHERE id = $3") |
| 1104 |
.bind(who) |
| 1105 |
.bind(now_ms()) |
| 1106 |
.bind(id) |
| 1107 |
.execute(pool) |
| 1108 |
.await?; |
| 1109 |
Ok(()) |
| 1110 |
} |
| 1111 |
|
| 1112 |
|
| 1113 |
pub async fn waiting_for_approval(pool: &Pool) -> sqlx::Result<Vec<Job>> { |
| 1114 |
sqlx::query_as( |
| 1115 |
"SELECT j.* FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id \ |
| 1116 |
WHERE j.status = 'queued' AND j.approval = 'waiting' AND r.status IN ('queued', 'running')", |
| 1117 |
) |
| 1118 |
.fetch_all(pool) |
| 1119 |
.await |
| 1120 |
} |
| 1121 |
|
| 1122 |
|
| 1123 |
#[derive(Clone, Debug, FromRow)] |
| 1124 |
pub struct Deployment { |
| 1125 |
pub environment: String, |
| 1126 |
pub job: String, |
| 1127 |
pub run_number: i64, |
| 1128 |
pub state: String, |
| 1129 |
pub bookmark: String, |
| 1130 |
pub actor: String, |
| 1131 |
pub approved_by: String, |
| 1132 |
pub finished_ms: i64, |
| 1133 |
} |
| 1134 |
|
| 1135 |
|
| 1136 |
pub async fn deployments(pool: &Pool, repo_id: i64, limit: i64) -> sqlx::Result<Vec<Deployment>> { |
| 1137 |
sqlx::query_as( |
| 1138 |
"SELECT j.environment, j.name AS job, r.number AS run_number, r.state, r.bookmark, r.actor, j.approved_by, \ |
| 1139 |
COALESCE(j.finished_ms, 0) AS finished_ms FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id \ |
| 1140 |
WHERE r.repo_id = $1 AND j.environment != '' AND j.status = 'passed' ORDER BY j.finished_ms DESC LIMIT $2", |
| 1141 |
) |
| 1142 |
.bind(repo_id) |
| 1143 |
.bind(limit) |
| 1144 |
.fetch_all(pool) |
| 1145 |
.await |
| 1146 |
} |