| 42fa9554 Andy McCall ↶ |
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, |
| a0d4bbfb Andy McCall ↶ |
10 |
|
| 11 |
#[sqlx(default)] |
| 12 |
pub number: i64, |
| 1edac4b6 Andy McCall ↶ |
13 |
|
| 14 |
#[sqlx(default)] |
| 15 |
pub trigger: String, |
| 42fa9554 Andy McCall ↶ |
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 { |
| 1edac4b6 Andy McCall ↶ |
28 |
|
| 29 |
pub fn started_by(&self) -> String { |
| 30 |
match self.trigger.as_str() { |
| 31 |
"manual" => format!("started by {}", self.actor), |
| c889911b Andy McCall ↶ |
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), |
| 1edac4b6 Andy McCall ↶ |
35 |
_ => format!("pushed by {}", self.actor), |
| 36 |
} |
| 37 |
} |
| 38 |
|
| c889911b Andy McCall ↶ |
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 |
|
| 42fa9554 Andy McCall ↶ |
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 |
|
| 4d2e44d7 Andy McCall ↶ |
62 |
#[derive(Clone, Debug, Default, FromRow)] |
| 42fa9554 Andy McCall ↶ |
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, |
| fd3db644 Andy McCall ↶ |
82 |
|
| 83 |
pub also_read: String, |
| cc19697f Andy McCall ↶ |
84 |
|
| 85 |
pub receipt_attest: String, |
| 86 |
|
| 87 |
pub receipt_signer: String, |
| 88 |
|
| 89 |
pub receipt_note: String, |
| 4d2e44d7 Andy McCall ↶ |
90 |
|
| 91 |
|
| 92 |
pub waiting: String, |
| 42fa9554 Andy McCall ↶ |
93 |
} |
| 94 |
|
| 95 |
impl Job { |
| 96 |
pub fn depends_on(&self) -> Vec<String> { |
| 97 |
serde_json::from_str(&self.depends).unwrap_or_default() |
| 98 |
} |
| 99 |
|
| 100 |
pub fn done(&self) -> bool { |
| 745d225d Andy McCall ↶ |
101 |
matches!(self.status.as_str(), "passed" | "failed" | "skipped" | "error" | "cancelled") |
| 42fa9554 Andy McCall ↶ |
102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
#[derive(Clone, Debug, FromRow)] |
| 106 |
pub struct LogLine { |
| 107 |
pub seq: i64, |
| 108 |
pub t: i64, |
| 109 |
pub stream: String, |
| 110 |
pub text: String, |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
pub struct NewJob { |
| 115 |
pub name: String, |
| 116 |
pub depth: i64, |
| 117 |
pub depends: Vec<String>, |
| 118 |
pub spec: String, |
| 119 |
pub sandbox: String, |
| 120 |
pub runtime: String, |
| 121 |
} |
| 122 |
|
| 123 |
pub struct NewRun<'a> { |
| 124 |
pub repo_id: i64, |
| 125 |
pub bookmark: &'a str, |
| 126 |
pub state: &'a str, |
| 127 |
pub heads: &'a str, |
| 128 |
pub pipeline_hash: &'a str, |
| 129 |
pub actor: &'a str, |
| 1edac4b6 Andy McCall ↶ |
130 |
|
| 131 |
pub trigger: &'a str, |
| 42fa9554 Andy McCall ↶ |
132 |
|
| 133 |
pub error: Option<&'a str>, |
| 134 |
} |
| 135 |
|
| 136 |
pub async fn create_run(pool: &Pool, r: NewRun<'_>, jobs: &[NewJob]) -> sqlx::Result<i64> { |
| 941abdd6 Andy McCall ↶ |
137 |
let mut tx = super::begin_write(pool).await?; |
| 42fa9554 Andy McCall ↶ |
138 |
let now = now_ms(); |
| 139 |
let status = if r.error.is_some() { "error" } else { "queued" }; |
| 140 |
let id: i64 = sqlx::query_scalar( |
| 1edac4b6 Andy McCall ↶ |
141 |
"INSERT INTO flow_runs (repo_id, number, bookmark, state, heads, pipeline_hash, status, error, actor, trigger, created_ms, finished_ms) \ |
| 142 |
VALUES (?, (SELECT COALESCE(MAX(number), 0) + 1 FROM flow_runs WHERE repo_id = ?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id", |
| 42fa9554 Andy McCall ↶ |
143 |
) |
| 144 |
.bind(r.repo_id) |
| a0d4bbfb Andy McCall ↶ |
145 |
.bind(r.repo_id) |
| 42fa9554 Andy McCall ↶ |
146 |
.bind(r.bookmark) |
| 147 |
.bind(r.state) |
| 148 |
.bind(r.heads) |
| 149 |
.bind(r.pipeline_hash) |
| 150 |
.bind(status) |
| 151 |
.bind(r.error.unwrap_or("")) |
| 152 |
.bind(r.actor) |
| 1edac4b6 Andy McCall ↶ |
153 |
.bind(r.trigger) |
| 42fa9554 Andy McCall ↶ |
154 |
.bind(now) |
| 155 |
.bind(r.error.map(|_| now)) |
| 156 |
.fetch_one(&mut *tx) |
| 157 |
.await?; |
| 158 |
for j in jobs { |
| 159 |
sqlx::query( |
| 160 |
"INSERT INTO flow_jobs (run_id, name, depth, depends, spec, sandbox, runtime, status) VALUES (?, ?, ?, ?, ?, ?, ?, 'queued')", |
| 161 |
) |
| 162 |
.bind(id) |
| 163 |
.bind(&j.name) |
| 164 |
.bind(j.depth) |
| 165 |
.bind(serde_json::to_string(&j.depends).unwrap_or_else(|_| "[]".into())) |
| 166 |
.bind(&j.spec) |
| 167 |
.bind(&j.sandbox) |
| 168 |
.bind(&j.runtime) |
| 169 |
.execute(&mut *tx) |
| 170 |
.await?; |
| 171 |
} |
| 172 |
tx.commit().await?; |
| 173 |
Ok(id) |
| 174 |
} |
| 175 |
|
| c4c87f48 Andy McCall ↶ |
176 |
|
| 177 |
fn labels_in(spec: &str, sandbox: &str) -> Vec<String> { |
| 178 |
let mut out: Vec<String> = [spec, sandbox] |
| 179 |
.iter() |
| 180 |
.filter_map(|j| serde_json::from_str::<serde_json::Value>(j).ok()) |
| 181 |
.flat_map(|v| v["runs_on"].as_array().cloned().unwrap_or_default()) |
| 182 |
.filter_map(|l| l.as_str().map(String::from)) |
| 183 |
.collect(); |
| 184 |
out.sort(); |
| 185 |
out.dedup(); |
| 186 |
out |
| 187 |
} |
| 188 |
|
| 189 |
impl Job { |
| 190 |
|
| 191 |
pub fn needs(&self) -> Vec<String> { |
| 192 |
labels_in(&self.spec, &self.sandbox) |
| 4d2e44d7 Andy McCall ↶ |
193 |
} |
| 194 |
|
| 195 |
|
| 196 |
pub fn arch(&self) -> Option<String> { |
| 197 |
arch_in(&self.sandbox) |
| c4c87f48 Andy McCall ↶ |
198 |
} |
| 199 |
} |
| 200 |
|
| efd29a00 Andy McCall ↶ |
201 |
|
| 202 |
fn arch_in(sandbox: &str) -> Option<String> { |
| 203 |
serde_json::from_str::<serde_json::Value>(sandbox).ok()?["arch"].as_str().map(String::from) |
| 204 |
} |
| 205 |
|
| bf3d0754 Andy McCall ↶ |
206 |
|
| 207 |
#[derive(Clone, Debug, FromRow)] |
| 208 |
pub struct QueueEntry { |
| 209 |
pub job_id: i64, |
| 210 |
pub run_id: i64, |
| 211 |
pub repo_id: i64, |
| 212 |
pub owner: String, |
| 213 |
pub name: String, |
| 214 |
pub visibility: String, |
| 215 |
pub run_number: i64, |
| 216 |
pub job: String, |
| 217 |
pub status: String, |
| b66e70b7 Andy McCall ↶ |
218 |
|
| 219 |
pub runner: String, |
| c4c87f48 Andy McCall ↶ |
220 |
pub spec: String, |
| 221 |
pub sandbox: String, |
| 222 |
} |
| 223 |
|
| 224 |
impl QueueEntry { |
| 225 |
|
| 226 |
pub fn needs(&self) -> Vec<String> { |
| 227 |
labels_in(&self.spec, &self.sandbox) |
| efd29a00 Andy McCall ↶ |
228 |
} |
| 229 |
|
| 230 |
|
| 231 |
pub fn arch(&self) -> Option<String> { |
| 232 |
arch_in(&self.sandbox) |
| c4c87f48 Andy McCall ↶ |
233 |
} |
| bf3d0754 Andy McCall ↶ |
234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
pub async fn queue(pool: &Pool) -> sqlx::Result<Vec<QueueEntry>> { |
| 239 |
sqlx::query_as( |
| c4c87f48 Andy McCall ↶ |
240 |
"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 \ |
| bf3d0754 Andy McCall ↶ |
241 |
FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id JOIN repositories p ON p.id = r.repo_id \ |
| 242 |
WHERE j.status IN ('queued', 'running') AND r.status IN ('queued', 'running') \ |
| 243 |
ORDER BY CASE j.status WHEN 'running' THEN 0 ELSE 1 END, j.run_id, j.depth, j.id", |
| 244 |
) |
| 245 |
.fetch_all(pool) |
| 246 |
.await |
| 247 |
} |
| 248 |
|
| 90326c96 Andy McCall ↶ |
249 |
|
| 250 |
pub async fn runs_by_number(pool: &Pool, repo_id: i64, limit: i64, before: Option<i64>) -> sqlx::Result<Vec<Run>> { |
| 251 |
sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND number < ? ORDER BY number DESC LIMIT ?") |
| 252 |
.bind(repo_id) |
| 253 |
.bind(before.unwrap_or(i64::MAX)) |
| 254 |
.bind(limit) |
| 255 |
.fetch_all(pool) |
| 256 |
.await |
| 257 |
} |
| 258 |
|
| d7ce41ac Andy McCall ↶ |
259 |
|
| 260 |
|
| 745d225d Andy McCall ↶ |
261 |
|
| 262 |
|
| 263 |
|
| d7ce41ac Andy McCall ↶ |
264 |
pub async fn latest_on(pool: &Pool, repo_id: i64, bookmark: &str) -> sqlx::Result<Option<Run>> { |
| 745d225d Andy McCall ↶ |
265 |
sqlx::query_as( |
| 266 |
"SELECT * FROM flow_runs WHERE repo_id = ? AND bookmark = ? AND heads != '' AND status != 'cancelled' |
| 267 |
AND NOT (trigger = 'push' AND status IN ('queued', 'running')) ORDER BY id DESC LIMIT 1", |
| 268 |
) |
| 269 |
.bind(repo_id) |
| 270 |
.bind(bookmark) |
| 271 |
.fetch_optional(pool) |
| 272 |
.await |
| d7ce41ac Andy McCall ↶ |
273 |
} |
| 274 |
|
| 42fa9554 Andy McCall ↶ |
275 |
pub async fn runs(pool: &Pool, repo_id: i64, limit: i64, before: Option<i64>) -> sqlx::Result<Vec<Run>> { |
| 276 |
sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND id < ? ORDER BY id DESC LIMIT ?") |
| 277 |
.bind(repo_id) |
| 278 |
.bind(before.unwrap_or(i64::MAX)) |
| 279 |
.bind(limit) |
| 280 |
.fetch_all(pool) |
| 281 |
.await |
| 282 |
} |
| 283 |
|
| a0d4bbfb Andy McCall ↶ |
284 |
|
| 285 |
pub async fn run(pool: &Pool, repo_id: i64, number: i64) -> sqlx::Result<Option<Run>> { |
| 286 |
sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND number = ?").bind(repo_id).bind(number).fetch_optional(pool).await |
| 42fa9554 Andy McCall ↶ |
287 |
} |
| 288 |
|
| 289 |
pub async fn run_by_id(pool: &Pool, id: i64) -> sqlx::Result<Option<Run>> { |
| 290 |
sqlx::query_as("SELECT * FROM flow_runs WHERE id = ?").bind(id).fetch_optional(pool).await |
| 291 |
} |
| 292 |
|
| 293 |
pub async fn jobs(pool: &Pool, run_id: i64) -> sqlx::Result<Vec<Job>> { |
| 294 |
sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = ? ORDER BY depth, name").bind(run_id).fetch_all(pool).await |
| 295 |
} |
| 296 |
|
| 297 |
pub async fn job(pool: &Pool, id: i64) -> sqlx::Result<Option<Job>> { |
| 298 |
sqlx::query_as("SELECT * FROM flow_jobs WHERE id = ?").bind(id).fetch_optional(pool).await |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
pub async fn job_by_token(pool: &Pool, token_hash: &str, max_age_ms: i64) -> sqlx::Result<Option<Job>> { |
| 303 |
sqlx::query_as("SELECT * FROM flow_jobs WHERE token_hash = ? AND status = 'running' AND started_ms >= ?") |
| 304 |
.bind(token_hash) |
| 305 |
.bind(now_ms() - max_age_ms) |
| 306 |
.fetch_optional(pool) |
| 307 |
.await |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| fd3db644 Andy McCall ↶ |
312 |
|
| 313 |
pub async fn set_also_read(pool: &Pool, job_id: i64, repo_ids: &[i64]) -> sqlx::Result<()> { |
| 314 |
let list: Vec<String> = repo_ids.iter().map(|i| i.to_string()).collect(); |
| 315 |
sqlx::query("UPDATE flow_jobs SET also_read = ? WHERE id = ?").bind(list.join(",")).bind(job_id).execute(pool).await?; |
| 316 |
Ok(()) |
| 317 |
} |
| 318 |
|
| 319 |
impl Job { |
| 320 |
pub fn also_read_ids(&self) -> Vec<i64> { |
| 321 |
self.also_read.split(',').filter_map(|x| x.trim().parse().ok()).collect() |
| 322 |
} |
| 323 |
} |
| 324 |
|
| b0612e7d Andy McCall ↶ |
325 |
|
| 326 |
|
| 327 |
#[derive(Clone, Debug, Default)] |
| 76bc5a05 Andy McCall ↶ |
328 |
pub struct Room { |
| 329 |
pub memory_mib: Option<u32>, |
| 330 |
pub cpus: Option<u32>, |
| b0612e7d Andy McCall ↶ |
331 |
pub images: Option<std::collections::HashSet<String>>, |
| efd29a00 Andy McCall ↶ |
332 |
|
| 333 |
pub arch: String, |
| 334 |
|
| 335 |
pub image_archs: std::collections::HashMap<String, String>, |
| 4d2e44d7 Andy McCall ↶ |
336 |
|
| 337 |
pub declined: std::collections::HashSet<i64>, |
| 92a797b2 Andy McCall ↶ |
338 |
|
| 339 |
|
| 340 |
pub fetchable: std::collections::HashSet<String>, |
| 76bc5a05 Andy McCall ↶ |
341 |
} |
| 342 |
|
| 343 |
impl Room { |
| b0612e7d Andy McCall ↶ |
344 |
|
| 345 |
|
| 76bc5a05 Andy McCall ↶ |
346 |
pub fn fits(&self, sandbox_json: &str) -> bool { |
| 347 |
let v: serde_json::Value = serde_json::from_str(sandbox_json).unwrap_or_default(); |
| 348 |
let need = |k: &str| v[k].as_u64().unwrap_or(0); |
| b0612e7d Andy McCall ↶ |
349 |
let rootfs = v["rootfs"].as_str().unwrap_or(""); |
| efd29a00 Andy McCall ↶ |
350 |
|
| 351 |
|
| 352 |
let wanted = v["arch"].as_str().map(String::from).or_else(|| self.image_archs.get(rootfs).cloned()); |
| 353 |
let arch_ok = self.arch.is_empty() || wanted.as_deref().is_none_or(|w| w == self.arch); |
| 354 |
arch_ok |
| 355 |
&& self.memory_mib.is_none_or(|m| need("memory_mib") <= m as u64) |
| b0612e7d Andy McCall ↶ |
356 |
&& self.cpus.is_none_or(|c| need("cpus") <= c as u64) |
| 92a797b2 Andy McCall ↶ |
357 |
&& (rootfs.is_empty() || self.images.as_ref().is_none_or(|have| have.contains(rootfs)) || self.fetchable.contains(rootfs)) |
| efd29a00 Andy McCall ↶ |
358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
pub async fn record_image_archs(pool: &Pool, archs: &std::collections::BTreeMap<String, String>) -> sqlx::Result<()> { |
| 363 |
for (hash, arch) in archs.iter().take(500) { |
| 364 |
if hash.len() != 64 || !flow_core::arch_ok(arch) { |
| 365 |
continue; |
| 366 |
} |
| 367 |
sqlx::query("INSERT INTO flow_images (hash, arch, seen_ms) VALUES (?, ?, ?) ON CONFLICT (hash) DO UPDATE SET arch = excluded.arch, seen_ms = excluded.seen_ms") |
| 368 |
.bind(hash.to_ascii_lowercase()) |
| 369 |
.bind(arch) |
| 370 |
.bind(now_ms()) |
| 371 |
.execute(pool) |
| 372 |
.await?; |
| 76bc5a05 Andy McCall ↶ |
373 |
} |
| efd29a00 Andy McCall ↶ |
374 |
Ok(()) |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
pub async fn image_archs(pool: &Pool) -> sqlx::Result<std::collections::HashMap<String, String>> { |
| 379 |
let rows: Vec<(String, String)> = sqlx::query_as("SELECT hash, arch FROM flow_images").fetch_all(pool).await?; |
| 380 |
Ok(rows.into_iter().collect()) |
| 76bc5a05 Andy McCall ↶ |
381 |
} |
| 382 |
|
| 42fa9554 Andy McCall ↶ |
383 |
pub async fn claim(pool: &Pool, runtimes: &[String], runner: &str, token_hash: &str) -> sqlx::Result<Option<Job>> { |
| c4c87f48 Andy McCall ↶ |
384 |
claim_within(pool, runtimes, runner, token_hash, Room::default(), &Default::default(), &[]).await |
| 76bc5a05 Andy McCall ↶ |
385 |
} |
| 386 |
|
| 387 |
|
| 74be9796 Andy McCall ↶ |
388 |
|
| 389 |
|
| 390 |
|
| 391 |
pub async fn claim_within( |
| 392 |
pool: &Pool, |
| 393 |
runtimes: &[String], |
| 394 |
runner: &str, |
| 395 |
token_hash: &str, |
| 396 |
room: Room, |
| 397 |
scope: &super::runners::Scope, |
| c4c87f48 Andy McCall ↶ |
398 |
labels: &[String], |
| 74be9796 Andy McCall ↶ |
399 |
) -> sqlx::Result<Option<Job>> { |
| 941abdd6 Andy McCall ↶ |
400 |
let mut tx = super::begin_write(pool).await?; |
| 42fa9554 Andy McCall ↶ |
401 |
let queued: Vec<Job> = sqlx::query_as( |
| 74be9796 Andy McCall ↶ |
402 |
"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 \ |
| 403 |
WHERE j.status = 'queued' AND r.status IN ('queued', 'running') \ |
| 404 |
AND (? = '' OR rep.owner = ?) AND (? IS NULL OR r.repo_id = ?) \ |
| 405 |
ORDER BY j.run_id, j.depth, j.id LIMIT 200", |
| 42fa9554 Andy McCall ↶ |
406 |
) |
| 74be9796 Andy McCall ↶ |
407 |
.bind(&scope.owner) |
| 408 |
.bind(&scope.owner) |
| 409 |
.bind(scope.repo_id) |
| 410 |
.bind(scope.repo_id) |
| 42fa9554 Andy McCall ↶ |
411 |
.fetch_all(&mut *tx) |
| 412 |
.await?; |
| 413 |
for j in queued { |
| 4d2e44d7 Andy McCall ↶ |
414 |
if !runtimes.iter().any(|r| r == &j.runtime) || !room.fits(&j.sandbox) || room.declined.contains(&j.id) { |
| c4c87f48 Andy McCall ↶ |
415 |
continue; |
| 416 |
} |
| 417 |
|
| 418 |
if !j.needs().iter().all(|l| labels.contains(l)) { |
| 42fa9554 Andy McCall ↶ |
419 |
continue; |
| 420 |
} |
| 421 |
let deps = j.depends_on(); |
| 422 |
if !deps.is_empty() { |
| 423 |
let mut passed = 0i64; |
| 424 |
for d in &deps { |
| 425 |
let st: Option<String> = sqlx::query_scalar("SELECT status FROM flow_jobs WHERE run_id = ? AND name = ?") |
| 426 |
.bind(j.run_id) |
| 427 |
.bind(d) |
| 428 |
.fetch_optional(&mut *tx) |
| 429 |
.await?; |
| 430 |
if st.as_deref() == Some("passed") { |
| 431 |
passed += 1; |
| 432 |
} |
| 433 |
} |
| 434 |
if passed != deps.len() as i64 { |
| 435 |
continue; |
| 436 |
} |
| 437 |
} |
| 438 |
let now = now_ms(); |
| 439 |
let done = sqlx::query( |
| 440 |
"UPDATE flow_jobs SET status = 'running', runner = ?, token_hash = ?, started_ms = ? WHERE id = ? AND status = 'queued'", |
| 441 |
) |
| 442 |
.bind(runner) |
| 443 |
.bind(token_hash) |
| 444 |
.bind(now) |
| 445 |
.bind(j.id) |
| 446 |
.execute(&mut *tx) |
| 447 |
.await?; |
| 448 |
if done.rows_affected() == 1 { |
| 449 |
sqlx::query("UPDATE flow_runs SET status = 'running' WHERE id = ? AND status = 'queued'") |
| 450 |
.bind(j.run_id) |
| 451 |
.execute(&mut *tx) |
| 452 |
.await?; |
| 453 |
tx.commit().await?; |
| 454 |
return job(pool, j.id).await; |
| 455 |
} |
| 456 |
} |
| 457 |
tx.commit().await?; |
| 458 |
Ok(None) |
| 4d2e44d7 Andy McCall ↶ |
459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
pub const DECLINE_HOLDS_MS: i64 = 10 * 60_000; |
| 464 |
|
| 465 |
|
| 466 |
pub async fn declined_by(pool: &Pool, runner_id: i64, profile: &str) -> sqlx::Result<std::collections::HashSet<i64>> { |
| 467 |
let rows: Vec<(i64,)> = sqlx::query_as("SELECT job_id FROM flow_declines WHERE runner_id = ? AND profile = ? AND ms > ?") |
| 468 |
.bind(runner_id) |
| 469 |
.bind(profile) |
| 470 |
.bind(now_ms() - DECLINE_HOLDS_MS) |
| 471 |
.fetch_all(pool) |
| 472 |
.await?; |
| 473 |
Ok(rows.into_iter().map(|(j,)| j).collect()) |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
#[derive(Clone, Debug, sqlx::FromRow)] |
| 478 |
pub struct DeclineRow { |
| 479 |
pub job_id: i64, |
| 480 |
pub runner_id: i64, |
| 481 |
pub runner: String, |
| 482 |
pub reason: String, |
| 483 |
pub ms: i64, |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
pub async fn decline( |
| 491 |
pool: &Pool, |
| 492 |
job_id: i64, |
| 493 |
runner_id: i64, |
| 494 |
runner: &str, |
| 495 |
profile: &str, |
| 496 |
reason: &str, |
| 497 |
) -> sqlx::Result<Option<Vec<DeclineRow>>> { |
| 498 |
let mut tx = super::begin_write(pool).await?; |
| 499 |
let back = sqlx::query( |
| 500 |
"UPDATE flow_jobs SET status = 'queued', runner = '', token_hash = '', started_ms = NULL, runner_id = NULL \ |
| 501 |
WHERE id = ? AND status = 'running' AND runner_id = ?", |
| 502 |
) |
| 503 |
.bind(job_id) |
| 504 |
.bind(runner_id) |
| 505 |
.execute(&mut *tx) |
| 506 |
.await?; |
| 507 |
if back.rows_affected() != 1 { |
| 508 |
tx.rollback().await?; |
| 509 |
return Ok(None); |
| 510 |
} |
| 511 |
sqlx::query( |
| 512 |
"INSERT INTO flow_declines (job_id, runner_id, runner, profile, reason, ms) VALUES (?, ?, ?, ?, ?, ?) \ |
| 513 |
ON CONFLICT (job_id, runner_id) DO UPDATE SET runner = excluded.runner, profile = excluded.profile, reason = excluded.reason, ms = excluded.ms", |
| 514 |
) |
| 515 |
.bind(job_id) |
| 516 |
.bind(runner_id) |
| 517 |
.bind(runner) |
| 518 |
.bind(profile) |
| 519 |
.bind(reason) |
| 520 |
.bind(now_ms()) |
| 521 |
.execute(&mut *tx) |
| 522 |
.await?; |
| 523 |
let all: Vec<DeclineRow> = |
| 524 |
sqlx::query_as("SELECT job_id, runner_id, runner, reason, ms FROM flow_declines WHERE job_id = ? ORDER BY ms") |
| 525 |
.bind(job_id) |
| 526 |
.fetch_all(&mut *tx) |
| 527 |
.await?; |
| 528 |
tx.commit().await?; |
| 529 |
Ok(Some(all)) |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
pub async fn declines_of_job(pool: &Pool, job_id: i64) -> sqlx::Result<Vec<DeclineRow>> { |
| 534 |
sqlx::query_as("SELECT job_id, runner_id, runner, reason, ms FROM flow_declines WHERE job_id = ? ORDER BY ms") |
| 535 |
.bind(job_id) |
| 536 |
.fetch_all(pool) |
| 537 |
.await |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
#[derive(Clone, Debug, sqlx::FromRow)] |
| 542 |
pub struct RunnerDecline { |
| 543 |
pub job_id: i64, |
| 544 |
pub job: String, |
| 545 |
pub run_number: i64, |
| 546 |
pub owner: String, |
| 547 |
pub name: String, |
| 548 |
pub reason: String, |
| 549 |
pub ms: i64, |
| 550 |
} |
| 551 |
|
| 552 |
pub async fn declines_of_runner(pool: &Pool, runner_id: i64, limit: i64) -> sqlx::Result<Vec<RunnerDecline>> { |
| 553 |
sqlx::query_as( |
| 554 |
"SELECT d.job_id, j.name AS job, r.number AS run_number, rep.owner, rep.name, d.reason, d.ms \ |
| 555 |
FROM flow_declines d JOIN flow_jobs j ON j.id = d.job_id JOIN flow_runs r ON r.id = j.run_id \ |
| 556 |
JOIN repositories rep ON rep.id = r.repo_id WHERE d.runner_id = ? ORDER BY d.ms DESC LIMIT ?", |
| 557 |
) |
| 558 |
.bind(runner_id) |
| 559 |
.bind(limit) |
| 560 |
.fetch_all(pool) |
| 561 |
.await |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
pub async fn ready_queued(pool: &Pool) -> sqlx::Result<Vec<(Job, String, i64)>> { |
| 567 |
let queued: Vec<Job> = sqlx::query_as( |
| 568 |
"SELECT j.* FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id \ |
| 569 |
WHERE j.status = 'queued' AND r.status IN ('queued', 'running') ORDER BY j.run_id, j.depth, j.id LIMIT 500", |
| 570 |
) |
| 571 |
.fetch_all(pool) |
| 572 |
.await?; |
| 573 |
let mut out = Vec::new(); |
| 574 |
for j in queued { |
| 575 |
let mut ready = true; |
| 576 |
for d in j.depends_on() { |
| 577 |
let st: Option<String> = sqlx::query_scalar("SELECT status FROM flow_jobs WHERE run_id = ? AND name = ?") |
| 578 |
.bind(j.run_id) |
| 579 |
.bind(&d) |
| 580 |
.fetch_optional(pool) |
| 581 |
.await?; |
| 582 |
if st.as_deref() != Some("passed") { |
| 583 |
ready = false; |
| 584 |
break; |
| 585 |
} |
| 586 |
} |
| 587 |
if !ready { |
| 588 |
continue; |
| 589 |
} |
| 590 |
let repo: Option<(String, i64)> = |
| 591 |
sqlx::query_as("SELECT rep.owner, rep.id FROM flow_runs r JOIN repositories rep ON rep.id = r.repo_id WHERE r.id = ?") |
| 592 |
.bind(j.run_id) |
| 593 |
.fetch_optional(pool) |
| 594 |
.await?; |
| 595 |
if let Some((owner, id)) = repo { |
| 596 |
out.push((j, owner, id)); |
| 597 |
} |
| 598 |
} |
| 599 |
Ok(out) |
| 600 |
} |
| 601 |
|
| 602 |
pub async fn set_waiting(pool: &Pool, job_id: i64, waiting: &str) -> sqlx::Result<()> { |
| 603 |
sqlx::query("UPDATE flow_jobs SET waiting = ? WHERE id = ? AND waiting <> ?") |
| 604 |
.bind(waiting) |
| 605 |
.bind(job_id) |
| 606 |
.bind(waiting) |
| 607 |
.execute(pool) |
| 608 |
.await?; |
| 609 |
Ok(()) |
| 42fa9554 Andy McCall ↶ |
610 |
} |
| 611 |
|
| 612 |
|
| 613 |
pub async fn append_log(pool: &Pool, job_id: i64, records: &[flow_core::api::Record], cap: i64) -> sqlx::Result<usize> { |
| 941abdd6 Andy McCall ↶ |
614 |
let mut tx = super::begin_write(pool).await?; |
| 42fa9554 Andy McCall ↶ |
615 |
let count: i64 = sqlx::query_scalar("SELECT log_count FROM flow_jobs WHERE id = ?").bind(job_id).fetch_one(&mut *tx).await?; |
| 616 |
let room = (cap - count).max(0) as usize; |
| 617 |
let keep = &records[..records.len().min(room)]; |
| 618 |
for (i, r) in keep.iter().enumerate() { |
| 619 |
sqlx::query("INSERT INTO flow_logs (job_id, seq, t, stream, text) VALUES (?, ?, ?, ?, ?)") |
| 620 |
.bind(job_id) |
| 621 |
.bind(count + i as i64 + 1) |
| 622 |
.bind(r.t as i64) |
| 623 |
.bind(&r.stream) |
| 624 |
.bind(&r.text) |
| 625 |
.execute(&mut *tx) |
| 626 |
.await?; |
| 627 |
} |
| 628 |
sqlx::query("UPDATE flow_jobs SET log_count = log_count + ? WHERE id = ?") |
| 629 |
.bind(keep.len() as i64) |
| 630 |
.bind(job_id) |
| 631 |
.execute(&mut *tx) |
| 632 |
.await?; |
| 633 |
tx.commit().await?; |
| 634 |
Ok(keep.len()) |
| 635 |
} |
| 636 |
|
| 637 |
pub async fn log(pool: &Pool, job_id: i64, after: i64, limit: i64) -> sqlx::Result<Vec<LogLine>> { |
| 638 |
sqlx::query_as("SELECT seq, t, stream, text FROM flow_logs WHERE job_id = ? AND seq > ? ORDER BY seq LIMIT ?") |
| 639 |
.bind(job_id) |
| 640 |
.bind(after) |
| 641 |
.bind(limit) |
| 642 |
.fetch_all(pool) |
| 643 |
.await |
| 644 |
} |
| 645 |
|
| 070a7b64 Andy McCall ↶ |
646 |
|
| 5dead1f4 Andy McCall ↶ |
647 |
|
| 648 |
|
| 649 |
pub async fn running_past_limit(pool: &Pool, now: i64, default_s: u64, grace_ms: i64) -> sqlx::Result<Vec<Job>> { |
| 650 |
sqlx::query_as( |
| 651 |
"SELECT * FROM flow_jobs WHERE status = 'running' \ |
| 652 |
AND COALESCE(started_ms, 0) + (CASE WHEN timeout_s > 0 THEN timeout_s ELSE ? END) * 1000 + ? < ? ORDER BY started_ms", |
| 653 |
) |
| 654 |
.bind(default_s as i64) |
| 655 |
.bind(grace_ms) |
| 656 |
.bind(now) |
| 657 |
.fetch_all(pool) |
| 658 |
.await |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
pub async fn set_job_timeout(pool: &Pool, job_id: i64, timeout_s: u64) -> sqlx::Result<()> { |
| 663 |
sqlx::query("UPDATE flow_jobs SET timeout_s = ? WHERE id = ?").bind(timeout_s as i64).bind(job_id).execute(pool).await?; |
| 664 |
Ok(()) |
| 665 |
} |
| 666 |
|
| 070a7b64 Andy McCall ↶ |
667 |
pub async fn running_since_before(pool: &Pool, ms: i64) -> sqlx::Result<Vec<Job>> { |
| 668 |
sqlx::query_as("SELECT * FROM flow_jobs WHERE status = 'running' AND COALESCE(started_ms, 0) < ? ORDER BY started_ms") |
| 669 |
.bind(ms) |
| 670 |
.fetch_all(pool) |
| 671 |
.await |
| 672 |
} |
| 673 |
|
| eef677c4 Andy McCall ↶ |
674 |
|
| 675 |
pub async fn running_on(pool: &Pool, runner_id: i64) -> sqlx::Result<Vec<Job>> { |
| 676 |
sqlx::query_as("SELECT * FROM flow_jobs WHERE status = 'running' AND runner_id = ? ORDER BY id").bind(runner_id).fetch_all(pool).await |
| 677 |
} |
| 678 |
|
| 42fa9554 Andy McCall ↶ |
679 |
pub struct Outcome<'a> { |
| 680 |
pub status: &'a str, |
| 681 |
pub exit_code: Option<i64>, |
| 682 |
pub error: &'a str, |
| 683 |
pub receipt: &'a str, |
| 684 |
pub receipt_hash: &'a str, |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
pub async fn finish(pool: &Pool, job_id: i64, o: Outcome<'_>) -> sqlx::Result<Vec<Job>> { |
| 941abdd6 Andy McCall ↶ |
690 |
let mut tx = super::begin_write(pool).await?; |
| 42fa9554 Andy McCall ↶ |
691 |
let now = now_ms(); |
| 692 |
let j: Job = sqlx::query_as("SELECT * FROM flow_jobs WHERE id = ?").bind(job_id).fetch_one(&mut *tx).await?; |
| 693 |
sqlx::query("UPDATE flow_jobs SET status = ?, exit_code = ?, error = ?, receipt = ?, receipt_hash = ?, finished_ms = ?, token_hash = '' WHERE id = ?") |
| 694 |
.bind(o.status) |
| 695 |
.bind(o.exit_code) |
| 696 |
.bind(o.error) |
| 697 |
.bind(o.receipt) |
| 698 |
.bind(o.receipt_hash) |
| 699 |
.bind(now) |
| 700 |
.bind(job_id) |
| 701 |
.execute(&mut *tx) |
| 702 |
.await?; |
| 703 |
let mut changed = vec![job_id]; |
| 704 |
|
| 705 |
loop { |
| 706 |
let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = ?").bind(j.run_id).fetch_all(&mut *tx).await?; |
| 707 |
let blocked: Vec<i64> = all |
| 708 |
.iter() |
| 709 |
.filter(|x| x.status == "queued") |
| 710 |
.filter(|x| { |
| 711 |
x.depends_on() |
| 712 |
.iter() |
| 713 |
.any(|d| all.iter().any(|y| &y.name == d && matches!(y.status.as_str(), "failed" | "skipped" | "error"))) |
| 714 |
}) |
| 715 |
.map(|x| x.id) |
| 716 |
.collect(); |
| 717 |
if blocked.is_empty() { |
| 718 |
break; |
| 719 |
} |
| 720 |
for id in blocked { |
| 721 |
sqlx::query("UPDATE flow_jobs SET status = 'skipped', finished_ms = ? WHERE id = ?") |
| 722 |
.bind(now) |
| 723 |
.bind(id) |
| 724 |
.execute(&mut *tx) |
| 725 |
.await?; |
| 726 |
changed.push(id); |
| 727 |
} |
| 728 |
} |
| 729 |
let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = ?").bind(j.run_id).fetch_all(&mut *tx).await?; |
| 730 |
if all.iter().all(Job::done) { |
| 731 |
let status = if all.iter().all(|x| x.status == "passed") { "passed" } else { "failed" }; |
| 732 |
sqlx::query("UPDATE flow_runs SET status = ?, finished_ms = ? WHERE id = ?") |
| 733 |
.bind(status) |
| 734 |
.bind(now) |
| 735 |
.bind(j.run_id) |
| 736 |
.execute(&mut *tx) |
| 737 |
.await?; |
| 738 |
} |
| 739 |
tx.commit().await?; |
| 740 |
let mut out = Vec::new(); |
| 741 |
for id in changed { |
| 745d225d Andy McCall ↶ |
742 |
if let Some(x) = job(pool, id).await? { |
| 743 |
out.push(x); |
| 744 |
} |
| 745 |
} |
| 746 |
Ok(out) |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
pub async fn cancel_run(pool: &Pool, run_id: i64, why: &str) -> sqlx::Result<Vec<Job>> { |
| 753 |
let now = now_ms(); |
| 754 |
let mut tx = pool.begin().await?; |
| 755 |
let ids: Vec<(i64,)> = sqlx::query_as("SELECT id FROM flow_jobs WHERE run_id = ? AND status IN ('queued', 'running')") |
| 756 |
.bind(run_id) |
| 757 |
.fetch_all(&mut *tx) |
| 758 |
.await?; |
| 759 |
sqlx::query( |
| 760 |
"UPDATE flow_jobs SET status = 'cancelled', error = ?, finished_ms = ?, token_hash = '' |
| 761 |
WHERE run_id = ? AND status IN ('queued', 'running')", |
| 762 |
) |
| 763 |
.bind(why) |
| 764 |
.bind(now) |
| 765 |
.bind(run_id) |
| 766 |
.execute(&mut *tx) |
| 767 |
.await?; |
| 768 |
sqlx::query("UPDATE flow_runs SET status = 'cancelled', finished_ms = ? WHERE id = ? AND status IN ('queued', 'running')") |
| 769 |
.bind(now) |
| 770 |
.bind(run_id) |
| 771 |
.execute(&mut *tx) |
| 772 |
.await?; |
| 773 |
tx.commit().await?; |
| 774 |
let mut out = Vec::new(); |
| 775 |
for (id,) in ids { |
| 42fa9554 Andy McCall ↶ |
776 |
if let Some(x) = job(pool, id).await? { |
| 777 |
out.push(x); |
| 778 |
} |
| 779 |
} |
| 780 |
Ok(out) |
| 745d225d Andy McCall ↶ |
781 |
} |
| 782 |
|
| 783 |
|
| 784 |
pub async fn live_push_runs_before(pool: &Pool, repo_id: i64, bookmark: &str, number: i64) -> sqlx::Result<Vec<Run>> { |
| 785 |
sqlx::query_as( |
| 786 |
"SELECT * FROM flow_runs WHERE repo_id = ? AND bookmark = ? AND number < ? AND trigger = 'push' |
| 787 |
AND status IN ('queued', 'running') ORDER BY id", |
| 788 |
) |
| 789 |
.bind(repo_id) |
| 790 |
.bind(bookmark) |
| 791 |
.bind(number) |
| 792 |
.fetch_all(pool) |
| 793 |
.await |
| 42fa9554 Andy McCall ↶ |
794 |
} |
| 736f6813 Andy McCall ↶ |
795 |
|
| 796 |
|
| 797 |
pub async fn latest_for_heads(pool: &Pool, repo_id: i64, heads: &str) -> sqlx::Result<Option<Run>> { |
| 798 |
sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND heads = ? ORDER BY id DESC LIMIT 1") |
| 799 |
.bind(repo_id) |
| 800 |
.bind(heads) |
| 801 |
.fetch_optional(pool) |
| 802 |
.await |
| cc19697f Andy McCall ↶ |
803 |
} |
| 804 |
|
| 805 |
|
| 806 |
pub async fn set_receipt(pool: &Pool, job_id: i64, attest: &str, signer: &str, note: &str) -> sqlx::Result<()> { |
| 807 |
sqlx::query("UPDATE flow_jobs SET receipt_attest = ?, receipt_signer = ?, receipt_note = ? WHERE id = ?") |
| 808 |
.bind(attest) |
| 809 |
.bind(signer) |
| 810 |
.bind(note) |
| 811 |
.bind(job_id) |
| 812 |
.execute(pool) |
| 813 |
.await?; |
| 814 |
Ok(()) |
| 736f6813 Andy McCall ↶ |
815 |
} |
| a3c8288b Andy McCall ↶ |
816 |
|
| 817 |
|
| 818 |
#[derive(Clone, Debug, FromRow)] |
| 819 |
pub struct Artifact { |
| 820 |
pub job_id: i64, |
| 821 |
pub name: String, |
| 822 |
pub path: String, |
| 823 |
pub blob: String, |
| 824 |
pub size: i64, |
| 825 |
|
| 826 |
pub content: String, |
| 827 |
pub created_ms: i64, |
| 828 |
} |
| 829 |
|
| 830 |
pub async fn add_artifact(pool: &Pool, a: &Artifact) -> sqlx::Result<()> { |
| 831 |
sqlx::query("INSERT OR REPLACE INTO flow_artifacts (job_id, name, path, blob, size, content, created_ms) VALUES (?, ?, ?, ?, ?, ?, ?)") |
| 832 |
.bind(a.job_id) |
| 833 |
.bind(&a.name) |
| 834 |
.bind(&a.path) |
| 835 |
.bind(&a.blob) |
| 836 |
.bind(a.size) |
| 837 |
.bind(&a.content) |
| 838 |
.bind(a.created_ms) |
| 839 |
.execute(pool) |
| 840 |
.await?; |
| 841 |
Ok(()) |
| 842 |
} |
| 843 |
|
| 45977501 Andy McCall ↶ |
844 |
|
| 845 |
|
| 846 |
pub async fn artifact_blobs(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<String>> { |
| 847 |
sqlx::query_scalar( |
| 848 |
"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 = ?", |
| 849 |
) |
| 850 |
.bind(repo_id) |
| 851 |
.fetch_all(pool) |
| 852 |
.await |
| 853 |
} |
| 854 |
|
| a36bab6b Andy McCall ↶ |
855 |
|
| a3c8288b Andy McCall ↶ |
856 |
pub async fn artifacts(pool: &Pool, job_id: i64) -> sqlx::Result<Vec<Artifact>> { |
| 857 |
sqlx::query_as("SELECT * FROM flow_artifacts WHERE job_id = ? ORDER BY name").bind(job_id).fetch_all(pool).await |
| 858 |
} |
| 10708ab8 Andy McCall ↶ |
859 |
|
| 860 |
|
| 861 |
|
| 862 |
#[derive(Clone, Debug, FromRow)] |
| 863 |
pub struct Provenance { |
| 864 |
pub run_number: i64, |
| 865 |
pub job: String, |
| 866 |
pub name: String, |
| 867 |
pub receipt_attest: String, |
| 868 |
pub receipt_signer: String, |
| 869 |
} |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
pub async fn provenance(pool: &Pool, repo_id: i64, content: &str) -> sqlx::Result<Option<Provenance>> { |
| 875 |
sqlx::query_as( |
| 876 |
"SELECT r.number AS run_number, j.name AS job, a.name AS name, j.receipt_attest, j.receipt_signer \ |
| 877 |
FROM flow_artifacts a JOIN flow_jobs j ON j.id = a.job_id JOIN flow_runs r ON r.id = j.run_id \ |
| 878 |
WHERE r.repo_id = ? AND a.content = ? AND j.status = 'passed' \ |
| 879 |
ORDER BY (j.receipt_attest != '') DESC, r.number DESC LIMIT 1", |
| 880 |
) |
| 881 |
.bind(repo_id) |
| 882 |
.bind(content) |
| 883 |
.fetch_optional(pool) |
| 884 |
.await |
| 885 |
} |