diff --git a/app/.env.example b/app/.env.example index b191cc3f..3d088655 100644 --- a/app/.env.example +++ b/app/.env.example @@ -318,7 +318,7 @@ VISUALCI_STRIPE_WEBHOOK_SECRET= # Secret. The demo reset's token. # VISUALCI_DEMO_HEARTBEAT_TOKEN= -# The sweep of abandoned builds (every five minutes). +# The builds sweep (every minute). # VISUALCI_BUILDS_EXPIRE_HEARTBEAT_URL= # Secret. The builds sweep's token. diff --git a/app/src/config.rs b/app/src/config.rs index 26ffdece..4d497b5c 100644 --- a/app/src/config.rs +++ b/app/src/config.rs @@ -422,7 +422,7 @@ pub struct Heartbeats { /// The demo reset's token. #[env("VISUALCI_DEMO_HEARTBEAT_TOKEN", secret)] pub demo_token: Option, - /// The sweep of abandoned builds (every five minutes). + /// The builds sweep (every minute). #[env("VISUALCI_BUILDS_EXPIRE_HEARTBEAT_URL")] pub builds_expire_url: Option, /// The builds sweep's token. diff --git a/app/src/console/mod.rs b/app/src/console/mod.rs index ea2fc8f5..f14383f4 100644 --- a/app/src/console/mod.rs +++ b/app/src/console/mod.rs @@ -1,4 +1,4 @@ -//! The schedule (`laravel schedule:run` every minute, in production a CronJob): the builds sweep every five minutes; +//! The schedule (`laravel schedule:run` every minute, in production a CronJob): the builds sweep every minute; //! retention, the groups check, the demo reset, the credit override reminders and the deleted projects' purge daily. //! Each run pushes its result to a heartbeat, so a task that failed or stopped running alerts; the purge is watched //! through `/status` instead (`oldest_deleted_project_unpurged_seconds`). @@ -14,7 +14,7 @@ pub mod staff; #[schedule::tasks] fn schedule(schedule: &mut Schedule) { - schedule.call(sweep_builds).name("builds-sweep").every_five_minutes().without_overlapping().on_one_runner(); + schedule.call(sweep_builds).name("builds-sweep").every_minute().without_overlapping().on_one_runner(); schedule.command("retention:prune").daily_at("03:00").without_overlapping().on_one_runner(); schedule.command("groups:check").daily_at("04:00").without_overlapping().on_one_runner(); schedule.command("demo:reset").name("demo-reset").daily_at("05:00").without_overlapping().on_one_runner(); diff --git a/app/src/domain/processing.rs b/app/src/domain/processing.rs index 835717b1..8cec66d1 100644 --- a/app/src/domain/processing.rs +++ b/app/src/domain/processing.rs @@ -85,10 +85,10 @@ pub async fn lock(build_id: i64) -> laravel::Result<()> { /// [`HEARTBEAT_SECONDS`]; a run that ends releases it, and one whose worker was killed (a deploy, an out-of-memory /// kill) stops renewing it, so the build is free again within this time and the sweep runs it again. (A lock held for a /// whole run's timeout kept a killed run's build stuck for 20 minutes.) -pub const HOLD_SECONDS: i64 = 120; +pub const HOLD_SECONDS: i64 = 60; /// How often a run renews its hold on the build. -const HEARTBEAT_SECONDS: u64 = 30; +const HEARTBEAT_SECONDS: u64 = 15; /// How long the note to run a build again lasts ([`run`]): longer than any run. const AGAIN_MINUTES: i64 = 20; @@ -127,6 +127,17 @@ pub struct Hold { pub heartbeat: std::time::Duration, } +/// The name of a build's processing lock ([`exclusive`]). +fn processing_lock_name(build_id: i64) -> String { + format!("builds.process:{build_id}") +} + +/// A build's processing lock, to see whether a run holds it. +fn processing_lock(build_id: i64) -> laravel::Result { + use laravel::time::ToSpan; + laravel::cache::lock(&processing_lock_name(build_id), 1.seconds()) +} + /// [`exclusive`] with its timing given. /// /// # Errors @@ -137,7 +148,7 @@ where Fut: std::future::Future>, { use laravel::time::ToSpan; - let lock = laravel::cache::lock(&format!("builds.process:{build_id}"), hold.seconds.seconds())?; + let lock = laravel::cache::lock(&processing_lock_name(build_id), hold.seconds.seconds())?; let out = lock .block(wait_seconds.seconds()) .get(|| async { @@ -1177,8 +1188,10 @@ pub async fn compare_now( pub const LOCAL_IDLE: &str = "Closed without review: a local build, idle for 7 days."; /// How long a local build waits for its own person's review before it leaves the waiting lists. pub const LOCAL_IDLE_DAYS: i32 = 7; -/// A build still processing this long after its last change is run again: its job's dispatch may have been lost. -pub const RESTART_AFTER_MINUTES: i32 = 10; +/// A build still processing this long after its last change, with no run holding it, is run again: its job's dispatch +/// was lost, or its run's worker was killed (a deploy) and the job's next attempt found the dead run's hold still there +/// and left it a note it never read (4 Dec 2026, build 1069). A live run renews its hold, so it's never run twice. +pub const RESTART_AFTER_MINUTES: i32 = 3; /// A build still processing this long after it was finalized fails: something keeps it from finishing. pub const GIVE_UP_AFTER_HOURS: i32 = 6; /// The error on a build that never finished processing. @@ -1233,8 +1246,13 @@ pub async fn sweep() -> laravel::Result { for id in idle.iter().chain(&stuck) { crate::events::BuildChanged { build_id: *id }.dispatch().await?; } - for id in &restart { - crate::jobs::process_build::ProcessBuild { build_id: *id }.dispatch().await?; + let mut restarted = 0; + for id in restart { + if processing_lock(id)?.is_locked().await? { + continue; + } + crate::jobs::process_build::ProcessBuild { build_id: id }.dispatch().await?; + restarted += 1; } - Ok(Swept { expired, idle_local_closed: idle.len(), restarted: restart.len(), stuck_failed: stuck.len() }) + Ok(Swept { expired, idle_local_closed: idle.len(), restarted, stuck_failed: stuck.len() }) } diff --git a/app/src/http/status.rs b/app/src/http/status.rs index bad19648..a918694d 100644 --- a/app/src/http/status.rs +++ b/app/src/http/status.rs @@ -213,7 +213,7 @@ async fn numbers() -> laravel::Result> { "renders_queued": get("renders_queued")?, "oldest_render_seconds": get("oldest_render_seconds")?, "builds_processing": get("builds_processing")?, - // The builds sweep (every five minutes) runs a build again after 10 minutes without progress and fails it + // The builds sweep (every minute) runs a build again 3 minutes after its last progress once nothing holds it, and fails it // after 6 hours: past that, the sweep has stopped. "oldest_processing_seconds": get("oldest_processing_seconds")?, "last_render_seconds_ago": get("last_render_seconds_ago")?, diff --git a/app/src/jobs/process_build.rs b/app/src/jobs/process_build.rs index 8594f92b..9859a230 100644 --- a/app/src/jobs/process_build.rs +++ b/app/src/jobs/process_build.rs @@ -23,7 +23,7 @@ pub struct ProcessBuild { max_wait = 30.seconds(), timeout = 15.minutes(), tries = 3, - backoff = [3.minutes(), 5.minutes()] + backoff = [90.seconds(), 5.minutes()] )] impl ProcessBuild { fn debounce_id(&self) -> i64 { diff --git a/app/tests/sweep.rs b/app/tests/sweep.rs index 4c6b945b..482607c2 100644 --- a/app/tests/sweep.rs +++ b/app/tests/sweep.rs @@ -1,4 +1,4 @@ -//! The builds sweep (every five minutes): abandoned builds expire, idle local builds close, builds that stopped +//! The builds sweep (every minute): abandoned builds expire, idle local builds close, builds that stopped //! progressing run again, and builds stuck for hours fail. `/status` reports how far behind the background work is. use app::domain::builds::{self, CreateParams}; @@ -78,3 +78,28 @@ async fn the_sweep_expires_closes_restarts_and_gives_up(app: Application) -> lar assert_eq!(again, processing::Swept::default(), "a second sweep finds nothing"); Ok(()) } + +/// A build whose run was killed (a deploy) after the job's next attempt found the dead run's hold and left: the sweep +/// runs it again once the hold has lapsed, a few minutes after its last change, and never while a live run holds it +/// (4 Dec 2026, build 1069 sat in processing with no job). +#[laravel::test] +async fn the_sweep_runs_a_stalled_build_again_once_nothing_holds_it(_app: Application) -> laravel::Result<()> { + use laravel::time::ToSpan; + let org = fixtures::organization("acme").await?; + let project = fixtures::project(&org, "docs").await?; + let stalled = build(&project, "b1", "ci").await?; + db::query!( + "UPDATE builds SET state = 'processing', finalized_at = now() - interval '5 minutes', \ + updated_at = now() - interval '4 minutes' WHERE id = {stalled}" + ) + .update() + .await?; + let held = laravel::cache::lock(&format!("builds.process:{stalled}"), 120.seconds())?; + assert!(held.acquire().await?, "the test takes the build's hold"); + assert_eq!(processing::sweep().await?.restarted, 0, "a run holds it"); + assert_eq!(Build::find_or_fail(stalled).await?.state, "processing"); + held.release().await?; + assert_eq!(processing::sweep().await?.restarted, 1, "nothing holds it now"); + assert_ne!(Build::find_or_fail(stalled).await?.state, "processing", "run again: an empty build concludes"); + Ok(()) +} diff --git a/frontend/content/docs/self-hosting/environment-variables.md b/frontend/content/docs/self-hosting/environment-variables.md index 9570a879..b5079fbc 100644 --- a/frontend/content/docs/self-hosting/environment-variables.md +++ b/frontend/content/docs/self-hosting/environment-variables.md @@ -194,7 +194,7 @@ Each scheduled job pushes its result to a [Gatus](https://gatus.io) external end | `VISUALCI_RETENTION_HEARTBEAT_TOKEN` | string | | Secret. Image retention's token. | | `VISUALCI_DEMO_HEARTBEAT_URL` | string | | The demo organization's daily reset. | | `VISUALCI_DEMO_HEARTBEAT_TOKEN` | string | | Secret. The demo reset's token. | -| `VISUALCI_BUILDS_EXPIRE_HEARTBEAT_URL` | string | | The sweep of abandoned builds (every five minutes). | +| `VISUALCI_BUILDS_EXPIRE_HEARTBEAT_URL` | string | | The builds sweep (every minute). | | `VISUALCI_BUILDS_EXPIRE_HEARTBEAT_TOKEN` | string | | Secret. The builds sweep's token. | | `VISUALCI_GROUPS_CHECK_HEARTBEAT_URL` | string | | The daily check of snapshot groups. | | `VISUALCI_GROUPS_CHECK_HEARTBEAT_TOKEN` | string | | Secret. The groups check's token. |