| 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 |
t if t.starts_with("rerun:") => format!("re-run of #{} by {}", &t[6..], self.actor), |
| 33 |
_ => format!("pushed by {}", self.actor), |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
pub fn head_list(&self) -> Vec<&str> { |
| 38 |
self.heads.split('+').filter(|h| !h.is_empty()).collect() |
| 39 |
} |
| 40 |
|
| 41 |
pub fn live(&self) -> bool { |
| 42 |
matches!(self.status.as_str(), "queued" | "running") |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
#[derive(Clone, Debug, FromRow)] |
| 47 |
pub struct Job { |
| 48 |
pub id: i64, |
| 49 |
pub run_id: i64, |
| 50 |
pub name: String, |
| 51 |
pub depth: i64, |
| 52 |
pub depends: String, |
| 53 |
pub spec: String, |
| 54 |
pub sandbox: String, |
| 55 |
pub runtime: String, |
| 56 |
pub status: String, |
| 57 |
pub runner: String, |
| 58 |
pub token_hash: String, |
| 59 |
pub started_ms: Option<i64>, |
| 60 |
pub finished_ms: Option<i64>, |
| 61 |
pub exit_code: Option<i64>, |
| 62 |
pub error: String, |
| 63 |
pub receipt: String, |
| 64 |
pub receipt_hash: String, |
| 65 |
pub log_count: i64, |
| 66 |
|
| 67 |
pub also_read: String, |
| 68 |
} |
| 69 |
|
| 70 |
impl Job { |
| 71 |
pub fn depends_on(&self) -> Vec<String> { |
| 72 |
serde_json::from_str(&self.depends).unwrap_or_default() |
| 73 |
} |
| 74 |
|
| 75 |
pub fn done(&self) -> bool { |
| 76 |
matches!(self.status.as_str(), "passed" | "failed" | "skipped" | "error") |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
#[derive(Clone, Debug, FromRow)] |
| 81 |
pub struct LogLine { |
| 82 |
pub seq: i64, |
| 83 |
pub t: i64, |
| 84 |
pub stream: String, |
| 85 |
pub text: String, |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
pub struct NewJob { |
| 90 |
pub name: String, |
| 91 |
pub depth: i64, |
| 92 |
pub depends: Vec<String>, |
| 93 |
pub spec: String, |
| 94 |
pub sandbox: String, |
| 95 |
pub runtime: String, |
| 96 |
} |
| 97 |
|
| 98 |
pub struct NewRun<'a> { |
| 99 |
pub repo_id: i64, |
| 100 |
pub bookmark: &'a str, |
| 101 |
pub state: &'a str, |
| 102 |
pub heads: &'a str, |
| 103 |
pub pipeline_hash: &'a str, |
| 104 |
pub actor: &'a str, |
| 105 |
|
| 106 |
pub trigger: &'a str, |
| 107 |
|
| 108 |
pub error: Option<&'a str>, |
| 109 |
} |
| 110 |
|
| 111 |
pub async fn create_run(pool: &Pool, r: NewRun<'_>, jobs: &[NewJob]) -> sqlx::Result<i64> { |
| 112 |
let mut tx = super::begin_write(pool).await?; |
| 113 |
let now = now_ms(); |
| 114 |
let status = if r.error.is_some() { "error" } else { "queued" }; |
| 115 |
let id: i64 = sqlx::query_scalar( |
| 116 |
"INSERT INTO flow_runs (repo_id, number, bookmark, state, heads, pipeline_hash, status, error, actor, trigger, created_ms, finished_ms) \ |
| 117 |
VALUES (?, (SELECT COALESCE(MAX(number), 0) + 1 FROM flow_runs WHERE repo_id = ?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id", |
| 118 |
) |
| 119 |
.bind(r.repo_id) |
| 120 |
.bind(r.repo_id) |
| 121 |
.bind(r.bookmark) |
| 122 |
.bind(r.state) |
| 123 |
.bind(r.heads) |
| 124 |
.bind(r.pipeline_hash) |
| 125 |
.bind(status) |
| 126 |
.bind(r.error.unwrap_or("")) |
| 127 |
.bind(r.actor) |
| 128 |
.bind(r.trigger) |
| 129 |
.bind(now) |
| 130 |
.bind(r.error.map(|_| now)) |
| 131 |
.fetch_one(&mut *tx) |
| 132 |
.await?; |
| 133 |
for j in jobs { |
| 134 |
sqlx::query( |
| 135 |
"INSERT INTO flow_jobs (run_id, name, depth, depends, spec, sandbox, runtime, status) VALUES (?, ?, ?, ?, ?, ?, ?, 'queued')", |
| 136 |
) |
| 137 |
.bind(id) |
| 138 |
.bind(&j.name) |
| 139 |
.bind(j.depth) |
| 140 |
.bind(serde_json::to_string(&j.depends).unwrap_or_else(|_| "[]".into())) |
| 141 |
.bind(&j.spec) |
| 142 |
.bind(&j.sandbox) |
| 143 |
.bind(&j.runtime) |
| 144 |
.execute(&mut *tx) |
| 145 |
.await?; |
| 146 |
} |
| 147 |
tx.commit().await?; |
| 148 |
Ok(id) |
| 149 |
} |
| 150 |
|
| 151 |
pub async fn runs(pool: &Pool, repo_id: i64, limit: i64, before: Option<i64>) -> sqlx::Result<Vec<Run>> { |
| 152 |
sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND id < ? ORDER BY id DESC LIMIT ?") |
| 153 |
.bind(repo_id) |
| 154 |
.bind(before.unwrap_or(i64::MAX)) |
| 155 |
.bind(limit) |
| 156 |
.fetch_all(pool) |
| 157 |
.await |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
pub async fn run(pool: &Pool, repo_id: i64, number: i64) -> sqlx::Result<Option<Run>> { |
| 162 |
sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND number = ?").bind(repo_id).bind(number).fetch_optional(pool).await |
| 163 |
} |
| 164 |
|
| 165 |
pub async fn run_by_id(pool: &Pool, id: i64) -> sqlx::Result<Option<Run>> { |
| 166 |
sqlx::query_as("SELECT * FROM flow_runs WHERE id = ?").bind(id).fetch_optional(pool).await |
| 167 |
} |
| 168 |
|
| 169 |
pub async fn jobs(pool: &Pool, run_id: i64) -> sqlx::Result<Vec<Job>> { |
| 170 |
sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = ? ORDER BY depth, name").bind(run_id).fetch_all(pool).await |
| 171 |
} |
| 172 |
|
| 173 |
pub async fn job(pool: &Pool, id: i64) -> sqlx::Result<Option<Job>> { |
| 174 |
sqlx::query_as("SELECT * FROM flow_jobs WHERE id = ?").bind(id).fetch_optional(pool).await |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
pub async fn job_by_token(pool: &Pool, token_hash: &str, max_age_ms: i64) -> sqlx::Result<Option<Job>> { |
| 179 |
sqlx::query_as("SELECT * FROM flow_jobs WHERE token_hash = ? AND status = 'running' AND started_ms >= ?") |
| 180 |
.bind(token_hash) |
| 181 |
.bind(now_ms() - max_age_ms) |
| 182 |
.fetch_optional(pool) |
| 183 |
.await |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
pub async fn set_also_read(pool: &Pool, job_id: i64, repo_ids: &[i64]) -> sqlx::Result<()> { |
| 190 |
let list: Vec<String> = repo_ids.iter().map(|i| i.to_string()).collect(); |
| 191 |
sqlx::query("UPDATE flow_jobs SET also_read = ? WHERE id = ?").bind(list.join(",")).bind(job_id).execute(pool).await?; |
| 192 |
Ok(()) |
| 193 |
} |
| 194 |
|
| 195 |
impl Job { |
| 196 |
pub fn also_read_ids(&self) -> Vec<i64> { |
| 197 |
self.also_read.split(',').filter_map(|x| x.trim().parse().ok()).collect() |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
#[derive(Clone, Copy, Debug, Default)] |
| 203 |
pub struct Room { |
| 204 |
pub memory_mib: Option<u32>, |
| 205 |
pub cpus: Option<u32>, |
| 206 |
} |
| 207 |
|
| 208 |
impl Room { |
| 209 |
|
| 210 |
pub fn fits(&self, sandbox_json: &str) -> bool { |
| 211 |
let v: serde_json::Value = serde_json::from_str(sandbox_json).unwrap_or_default(); |
| 212 |
let need = |k: &str| v[k].as_u64().unwrap_or(0); |
| 213 |
self.memory_mib.is_none_or(|m| need("memory_mib") <= m as u64) && self.cpus.is_none_or(|c| need("cpus") <= c as u64) |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
pub async fn claim(pool: &Pool, runtimes: &[String], runner: &str, token_hash: &str) -> sqlx::Result<Option<Job>> { |
| 218 |
claim_within(pool, runtimes, runner, token_hash, Room::default()).await |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
pub async fn claim_within(pool: &Pool, runtimes: &[String], runner: &str, token_hash: &str, room: Room) -> sqlx::Result<Option<Job>> { |
| 223 |
let mut tx = super::begin_write(pool).await?; |
| 224 |
let queued: Vec<Job> = sqlx::query_as( |
| 225 |
"SELECT j.* FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id WHERE j.status = 'queued' AND r.status IN ('queued', 'running') ORDER BY j.run_id, j.depth, j.id LIMIT 200", |
| 226 |
) |
| 227 |
.fetch_all(&mut *tx) |
| 228 |
.await?; |
| 229 |
for j in queued { |
| 230 |
if !runtimes.iter().any(|r| r == &j.runtime) || !room.fits(&j.sandbox) { |
| 231 |
continue; |
| 232 |
} |
| 233 |
let deps = j.depends_on(); |
| 234 |
if !deps.is_empty() { |
| 235 |
let mut passed = 0i64; |
| 236 |
for d in &deps { |
| 237 |
let st: Option<String> = sqlx::query_scalar("SELECT status FROM flow_jobs WHERE run_id = ? AND name = ?") |
| 238 |
.bind(j.run_id) |
| 239 |
.bind(d) |
| 240 |
.fetch_optional(&mut *tx) |
| 241 |
.await?; |
| 242 |
if st.as_deref() == Some("passed") { |
| 243 |
passed += 1; |
| 244 |
} |
| 245 |
} |
| 246 |
if passed != deps.len() as i64 { |
| 247 |
continue; |
| 248 |
} |
| 249 |
} |
| 250 |
let now = now_ms(); |
| 251 |
let done = sqlx::query( |
| 252 |
"UPDATE flow_jobs SET status = 'running', runner = ?, token_hash = ?, started_ms = ? WHERE id = ? AND status = 'queued'", |
| 253 |
) |
| 254 |
.bind(runner) |
| 255 |
.bind(token_hash) |
| 256 |
.bind(now) |
| 257 |
.bind(j.id) |
| 258 |
.execute(&mut *tx) |
| 259 |
.await?; |
| 260 |
if done.rows_affected() == 1 { |
| 261 |
sqlx::query("UPDATE flow_runs SET status = 'running' WHERE id = ? AND status = 'queued'") |
| 262 |
.bind(j.run_id) |
| 263 |
.execute(&mut *tx) |
| 264 |
.await?; |
| 265 |
tx.commit().await?; |
| 266 |
return job(pool, j.id).await; |
| 267 |
} |
| 268 |
} |
| 269 |
tx.commit().await?; |
| 270 |
Ok(None) |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
pub async fn append_log(pool: &Pool, job_id: i64, records: &[flow_core::api::Record], cap: i64) -> sqlx::Result<usize> { |
| 275 |
let mut tx = super::begin_write(pool).await?; |
| 276 |
let count: i64 = sqlx::query_scalar("SELECT log_count FROM flow_jobs WHERE id = ?").bind(job_id).fetch_one(&mut *tx).await?; |
| 277 |
let room = (cap - count).max(0) as usize; |
| 278 |
let keep = &records[..records.len().min(room)]; |
| 279 |
for (i, r) in keep.iter().enumerate() { |
| 280 |
sqlx::query("INSERT INTO flow_logs (job_id, seq, t, stream, text) VALUES (?, ?, ?, ?, ?)") |
| 281 |
.bind(job_id) |
| 282 |
.bind(count + i as i64 + 1) |
| 283 |
.bind(r.t as i64) |
| 284 |
.bind(&r.stream) |
| 285 |
.bind(&r.text) |
| 286 |
.execute(&mut *tx) |
| 287 |
.await?; |
| 288 |
} |
| 289 |
sqlx::query("UPDATE flow_jobs SET log_count = log_count + ? WHERE id = ?") |
| 290 |
.bind(keep.len() as i64) |
| 291 |
.bind(job_id) |
| 292 |
.execute(&mut *tx) |
| 293 |
.await?; |
| 294 |
tx.commit().await?; |
| 295 |
Ok(keep.len()) |
| 296 |
} |
| 297 |
|
| 298 |
pub async fn log(pool: &Pool, job_id: i64, after: i64, limit: i64) -> sqlx::Result<Vec<LogLine>> { |
| 299 |
sqlx::query_as("SELECT seq, t, stream, text FROM flow_logs WHERE job_id = ? AND seq > ? ORDER BY seq LIMIT ?") |
| 300 |
.bind(job_id) |
| 301 |
.bind(after) |
| 302 |
.bind(limit) |
| 303 |
.fetch_all(pool) |
| 304 |
.await |
| 305 |
} |
| 306 |
|
| 307 |
pub struct Outcome<'a> { |
| 308 |
pub status: &'a str, |
| 309 |
pub exit_code: Option<i64>, |
| 310 |
pub error: &'a str, |
| 311 |
pub receipt: &'a str, |
| 312 |
pub receipt_hash: &'a str, |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
pub async fn finish(pool: &Pool, job_id: i64, o: Outcome<'_>) -> sqlx::Result<Vec<Job>> { |
| 318 |
let mut tx = super::begin_write(pool).await?; |
| 319 |
let now = now_ms(); |
| 320 |
let j: Job = sqlx::query_as("SELECT * FROM flow_jobs WHERE id = ?").bind(job_id).fetch_one(&mut *tx).await?; |
| 321 |
sqlx::query("UPDATE flow_jobs SET status = ?, exit_code = ?, error = ?, receipt = ?, receipt_hash = ?, finished_ms = ?, token_hash = '' WHERE id = ?") |
| 322 |
.bind(o.status) |
| 323 |
.bind(o.exit_code) |
| 324 |
.bind(o.error) |
| 325 |
.bind(o.receipt) |
| 326 |
.bind(o.receipt_hash) |
| 327 |
.bind(now) |
| 328 |
.bind(job_id) |
| 329 |
.execute(&mut *tx) |
| 330 |
.await?; |
| 331 |
let mut changed = vec![job_id]; |
| 332 |
|
| 333 |
loop { |
| 334 |
let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = ?").bind(j.run_id).fetch_all(&mut *tx).await?; |
| 335 |
let blocked: Vec<i64> = all |
| 336 |
.iter() |
| 337 |
.filter(|x| x.status == "queued") |
| 338 |
.filter(|x| { |
| 339 |
x.depends_on() |
| 340 |
.iter() |
| 341 |
.any(|d| all.iter().any(|y| &y.name == d && matches!(y.status.as_str(), "failed" | "skipped" | "error"))) |
| 342 |
}) |
| 343 |
.map(|x| x.id) |
| 344 |
.collect(); |
| 345 |
if blocked.is_empty() { |
| 346 |
break; |
| 347 |
} |
| 348 |
for id in blocked { |
| 349 |
sqlx::query("UPDATE flow_jobs SET status = 'skipped', finished_ms = ? WHERE id = ?") |
| 350 |
.bind(now) |
| 351 |
.bind(id) |
| 352 |
.execute(&mut *tx) |
| 353 |
.await?; |
| 354 |
changed.push(id); |
| 355 |
} |
| 356 |
} |
| 357 |
let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = ?").bind(j.run_id).fetch_all(&mut *tx).await?; |
| 358 |
if all.iter().all(Job::done) { |
| 359 |
let status = if all.iter().all(|x| x.status == "passed") { "passed" } else { "failed" }; |
| 360 |
sqlx::query("UPDATE flow_runs SET status = ?, finished_ms = ? WHERE id = ?") |
| 361 |
.bind(status) |
| 362 |
.bind(now) |
| 363 |
.bind(j.run_id) |
| 364 |
.execute(&mut *tx) |
| 365 |
.await?; |
| 366 |
} |
| 367 |
tx.commit().await?; |
| 368 |
let mut out = Vec::new(); |
| 369 |
for id in changed { |
| 370 |
if let Some(x) = job(pool, id).await? { |
| 371 |
out.push(x); |
| 372 |
} |
| 373 |
} |
| 374 |
Ok(out) |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
pub async fn latest_for_heads(pool: &Pool, repo_id: i64, heads: &str) -> sqlx::Result<Option<Run>> { |
| 379 |
sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND heads = ? ORDER BY id DESC LIMIT 1") |
| 380 |
.bind(repo_id) |
| 381 |
.bind(heads) |
| 382 |
.fetch_optional(pool) |
| 383 |
.await |
| 384 |
} |