Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion app/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ pub struct Heartbeats {
/// The demo reset's token.
#[env("VISUALCI_DEMO_HEARTBEAT_TOKEN", secret)]
pub demo_token: Option<String>,
/// The sweep of abandoned builds (every five minutes).
/// The builds sweep (every minute).
#[env("VISUALCI_BUILDS_EXPIRE_HEARTBEAT_URL")]
pub builds_expire_url: Option<String>,
/// The builds sweep's token.
Expand Down
4 changes: 2 additions & 2 deletions app/src/console/mod.rs
Original file line number Diff line number Diff line change
@@ -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`).
Expand All @@ -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();
Expand Down
34 changes: 26 additions & 8 deletions app/src/domain/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<laravel::cache::Lock> {
use laravel::time::ToSpan;
laravel::cache::lock(&processing_lock_name(build_id), 1.seconds())
}

/// [`exclusive`] with its timing given.
///
/// # Errors
Expand All @@ -137,7 +148,7 @@ where
Fut: std::future::Future<Output = laravel::Result<T>>,
{
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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -1233,8 +1246,13 @@ pub async fn sweep() -> laravel::Result<Swept> {
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() })
}
2 changes: 1 addition & 1 deletion app/src/http/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ async fn numbers() -> laravel::Result<Json<Value>> {
"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")?,
Expand Down
2 changes: 1 addition & 1 deletion app/src/jobs/process_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 26 additions & 1 deletion app/tests/sweep.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
Loading