summaryrefslogtreecommitdiffstats
path: root/src/db
diff options
context:
space:
mode:
Diffstat (limited to 'src/db')
-rw-r--r--src/db/builds.rs29
-rw-r--r--src/db/queries.rs52
2 files changed, 79 insertions, 2 deletions
diff --git a/src/db/builds.rs b/src/db/builds.rs
index 0a7013c..27fa23b 100644
--- a/src/db/builds.rs
+++ b/src/db/builds.rs
@@ -161,13 +161,17 @@ pub fn extend_lease(db: &Db, id: &str, lease_secs: i64) -> Result<()> {
/// Reaper pass (§8.2): expired leases go back to the queue and their in-flight
/// step is marked interrupted, so the checkpoint decides where it resumes.
+///
+/// Both `leased` and `running` are reaped: a worker sets `running` as soon as it
+/// starts a step, so a process that dies mid-step leaves a `running` row, not a
+/// `leased` one. Reaping only `leased` would strand every real crash.
pub fn reap_expired(db: &Db) -> Result<usize> {
let now = time::now();
db.writer.tx(move |tx| {
let ids: Vec<String> = {
let mut stmt = tx.prepare(
- "SELECT id FROM build WHERE state='leased' AND lease_expires_at IS NOT NULL
- AND lease_expires_at < ?1",
+ "SELECT id FROM build WHERE state IN ('leased','running')
+ AND lease_expires_at IS NOT NULL AND lease_expires_at < ?1",
)?;
let rows = stmt.query_map(params![now], |r| r.get::<_, String>(0))?;
rows.filter_map(|r| r.ok()).collect()
@@ -484,3 +488,24 @@ pub fn requeue_transient(db: &Db, id: &str) -> Result<()> {
Ok(())
})
}
+
+/// Explicitly retry a failed build (§8.4). This is a human decision, not a
+/// policy: the failed step is put back to pending so it runs again, everything
+/// that succeeded stays checkpointed, and the attempt counter keeps counting.
+pub fn retry(db: &Db, id: &str) -> Result<()> {
+ let id = id.to_string();
+ let at = time::now();
+ db.writer.tx(move |tx| {
+ tx.execute(
+ "UPDATE build_step SET status='pending', error=NULL, started_at=NULL, finished_at=NULL
+ WHERE build_id=?1 AND status IN ('failed','interrupted')",
+ params![id],
+ )?;
+ tx.execute(
+ "UPDATE build SET state='queued', error=NULL, cancel_requested=0,
+ lease_owner=NULL, lease_expires_at=NULL, updated_at=?2 WHERE id=?1",
+ params![id, at],
+ )?;
+ Ok(())
+ })
+}
diff --git a/src/db/queries.rs b/src/db/queries.rs
index 3adc92f..fa530b4 100644
--- a/src/db/queries.rs
+++ b/src/db/queries.rs
@@ -951,3 +951,55 @@ pub fn index_pkgbase_drift(db: &Db) -> Result<(Vec<String>, Vec<String>)> {
Ok((orphans, missing))
})
}
+
+/// An approved change whose build failed and which therefore never reached the
+/// index (§8.4). This is the state that must never look healthy: a human said
+/// yes, the build died, and the index is quietly still serving the previous
+/// version. Surfacing it is the whole point of `approved_but_unbuilt`.
+#[derive(Debug, Clone)]
+pub struct Unbuilt {
+ pub pkgbase: String,
+ /// The version that was approved but never published.
+ pub approved: String,
+ /// The version the index actually serves, empty if nothing is served.
+ pub serving: String,
+ pub error: String,
+ pub build_id: String,
+}
+
+pub fn approved_but_unbuilt(db: &Db) -> Result<Vec<Unbuilt>> {
+ db.read(|c| {
+ let mut stmt = c.prepare(
+ "SELECT p.name,
+ (SELECT (CASE WHEN c.epoch='0' THEN '' ELSE c.epoch||':' END) || c.pkgver || '-' || c.pkgrel
+ FROM candidate c WHERE c.id = b.candidate_id),
+ COALESCE((SELECT (CASE WHEN r.epoch='0' THEN '' ELSE r.epoch||':' END) || r.pkgver || '-' || r.pkgrel
+ FROM release r JOIN pkgbase pb ON pb.current_accepted_release_id = r.id
+ WHERE pb.id = p.id AND r.state='published'), ''),
+ COALESCE(b.error, ''), b.id
+ FROM build b
+ JOIN pkgbase p ON p.id = b.pkgbase_id
+ WHERE b.state = 'failed'
+ AND b.candidate_id IS NOT NULL
+ AND NOT EXISTS (SELECT 1 FROM release r WHERE r.candidate_id = b.candidate_id)
+ AND b.id = (SELECT b2.id FROM build b2
+ WHERE b2.candidate_id = b.candidate_id
+ ORDER BY b2.rowid DESC LIMIT 1)
+ ORDER BY p.name",
+ )?;
+ let rows = stmt.query_map([], |r| {
+ Ok(Unbuilt {
+ pkgbase: r.get(0)?,
+ approved: r.get(1)?,
+ serving: r.get(2)?,
+ error: r.get(3)?,
+ build_id: r.get(4)?,
+ })
+ })?;
+ let mut out = Vec::new();
+ for r in rows {
+ out.push(r?);
+ }
+ Ok(out)
+ })
+}