Skip to content

Commit fb76ae4

Browse files
authored
Deploy safety: builds sweep every minute, a stalled build restarts once nothing holds it, a 60 s hold (build 1069 sat 19 minutes after a deploy) (#202)
* The builds sweep runs every minute and restarts a stalled build 3 minutes after its last change once nothing holds it (a killed run's next attempt found the dead hold and left; build 1069 sat 19 minutes) * A run's hold on its build: renewed every 15 s, lapses 60 s after the last renewal; the job's first retry waits 90 s, past a killed run's hold
1 parent 26ab162 commit fb76ae4

8 files changed

Lines changed: 59 additions & 16 deletions

File tree

‎app/.env.example‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ VISUALCI_STRIPE_WEBHOOK_SECRET=
318318
# Secret. The demo reset's token.
319319
# VISUALCI_DEMO_HEARTBEAT_TOKEN=
320320

321-
# The sweep of abandoned builds (every five minutes).
321+
# The builds sweep (every minute).
322322
# VISUALCI_BUILDS_EXPIRE_HEARTBEAT_URL=
323323

324324
# Secret. The builds sweep's token.

‎app/src/config.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ pub struct Heartbeats {
422422
/// The demo reset's token.
423423
#[env("VISUALCI_DEMO_HEARTBEAT_TOKEN", secret)]
424424
pub demo_token: Option<String>,
425-
/// The sweep of abandoned builds (every five minutes).
425+
/// The builds sweep (every minute).
426426
#[env("VISUALCI_BUILDS_EXPIRE_HEARTBEAT_URL")]
427427
pub builds_expire_url: Option<String>,
428428
/// The builds sweep's token.

‎app/src/console/mod.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! The schedule (`laravel schedule:run` every minute, in production a CronJob): the builds sweep every five minutes;
1+
//! The schedule (`laravel schedule:run` every minute, in production a CronJob): the builds sweep every minute;
22
//! retention, the groups check, the demo reset, the credit override reminders and the deleted projects' purge daily.
33
//! Each run pushes its result to a heartbeat, so a task that failed or stopped running alerts; the purge is watched
44
//! through `/status` instead (`oldest_deleted_project_unpurged_seconds`).
@@ -14,7 +14,7 @@ pub mod staff;
1414

1515
#[schedule::tasks]
1616
fn schedule(schedule: &mut Schedule) {
17-
schedule.call(sweep_builds).name("builds-sweep").every_five_minutes().without_overlapping().on_one_runner();
17+
schedule.call(sweep_builds).name("builds-sweep").every_minute().without_overlapping().on_one_runner();
1818
schedule.command("retention:prune").daily_at("03:00").without_overlapping().on_one_runner();
1919
schedule.command("groups:check").daily_at("04:00").without_overlapping().on_one_runner();
2020
schedule.command("demo:reset").name("demo-reset").daily_at("05:00").without_overlapping().on_one_runner();

‎app/src/domain/processing.rs‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ pub async fn lock(build_id: i64) -> laravel::Result<()> {
8585
/// [`HEARTBEAT_SECONDS`]; a run that ends releases it, and one whose worker was killed (a deploy, an out-of-memory
8686
/// kill) stops renewing it, so the build is free again within this time and the sweep runs it again. (A lock held for a
8787
/// whole run's timeout kept a killed run's build stuck for 20 minutes.)
88-
pub const HOLD_SECONDS: i64 = 120;
88+
pub const HOLD_SECONDS: i64 = 60;
8989

9090
/// How often a run renews its hold on the build.
91-
const HEARTBEAT_SECONDS: u64 = 30;
91+
const HEARTBEAT_SECONDS: u64 = 15;
9292

9393
/// How long the note to run a build again lasts ([`run`]): longer than any run.
9494
const AGAIN_MINUTES: i64 = 20;
@@ -127,6 +127,17 @@ pub struct Hold {
127127
pub heartbeat: std::time::Duration,
128128
}
129129

130+
/// The name of a build's processing lock ([`exclusive`]).
131+
fn processing_lock_name(build_id: i64) -> String {
132+
format!("builds.process:{build_id}")
133+
}
134+
135+
/// A build's processing lock, to see whether a run holds it.
136+
fn processing_lock(build_id: i64) -> laravel::Result<laravel::cache::Lock> {
137+
use laravel::time::ToSpan;
138+
laravel::cache::lock(&processing_lock_name(build_id), 1.seconds())
139+
}
140+
130141
/// [`exclusive`] with its timing given.
131142
///
132143
/// # Errors
@@ -137,7 +148,7 @@ where
137148
Fut: std::future::Future<Output = laravel::Result<T>>,
138149
{
139150
use laravel::time::ToSpan;
140-
let lock = laravel::cache::lock(&format!("builds.process:{build_id}"), hold.seconds.seconds())?;
151+
let lock = laravel::cache::lock(&processing_lock_name(build_id), hold.seconds.seconds())?;
141152
let out = lock
142153
.block(wait_seconds.seconds())
143154
.get(|| async {
@@ -1177,8 +1188,10 @@ pub async fn compare_now(
11771188
pub const LOCAL_IDLE: &str = "Closed without review: a local build, idle for 7 days.";
11781189
/// How long a local build waits for its own person's review before it leaves the waiting lists.
11791190
pub const LOCAL_IDLE_DAYS: i32 = 7;
1180-
/// A build still processing this long after its last change is run again: its job's dispatch may have been lost.
1181-
pub const RESTART_AFTER_MINUTES: i32 = 10;
1191+
/// A build still processing this long after its last change, with no run holding it, is run again: its job's dispatch
1192+
/// 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
1193+
/// 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.
1194+
pub const RESTART_AFTER_MINUTES: i32 = 3;
11821195
/// A build still processing this long after it was finalized fails: something keeps it from finishing.
11831196
pub const GIVE_UP_AFTER_HOURS: i32 = 6;
11841197
/// The error on a build that never finished processing.
@@ -1233,8 +1246,13 @@ pub async fn sweep() -> laravel::Result<Swept> {
12331246
for id in idle.iter().chain(&stuck) {
12341247
crate::events::BuildChanged { build_id: *id }.dispatch().await?;
12351248
}
1236-
for id in &restart {
1237-
crate::jobs::process_build::ProcessBuild { build_id: *id }.dispatch().await?;
1249+
let mut restarted = 0;
1250+
for id in restart {
1251+
if processing_lock(id)?.is_locked().await? {
1252+
continue;
1253+
}
1254+
crate::jobs::process_build::ProcessBuild { build_id: id }.dispatch().await?;
1255+
restarted += 1;
12381256
}
1239-
Ok(Swept { expired, idle_local_closed: idle.len(), restarted: restart.len(), stuck_failed: stuck.len() })
1257+
Ok(Swept { expired, idle_local_closed: idle.len(), restarted, stuck_failed: stuck.len() })
12401258
}

‎app/src/http/status.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ async fn numbers() -> laravel::Result<Json<Value>> {
213213
"renders_queued": get("renders_queued")?,
214214
"oldest_render_seconds": get("oldest_render_seconds")?,
215215
"builds_processing": get("builds_processing")?,
216-
// The builds sweep (every five minutes) runs a build again after 10 minutes without progress and fails it
216+
// The builds sweep (every minute) runs a build again 3 minutes after its last progress once nothing holds it, and fails it
217217
// after 6 hours: past that, the sweep has stopped.
218218
"oldest_processing_seconds": get("oldest_processing_seconds")?,
219219
"last_render_seconds_ago": get("last_render_seconds_ago")?,

‎app/src/jobs/process_build.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct ProcessBuild {
2323
max_wait = 30.seconds(),
2424
timeout = 15.minutes(),
2525
tries = 3,
26-
backoff = [3.minutes(), 5.minutes()]
26+
backoff = [90.seconds(), 5.minutes()]
2727
)]
2828
impl ProcessBuild {
2929
fn debounce_id(&self) -> i64 {

‎app/tests/sweep.rs‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! The builds sweep (every five minutes): abandoned builds expire, idle local builds close, builds that stopped
1+
//! The builds sweep (every minute): abandoned builds expire, idle local builds close, builds that stopped
22
//! progressing run again, and builds stuck for hours fail. `/status` reports how far behind the background work is.
33
44
use app::domain::builds::{self, CreateParams};
@@ -78,3 +78,28 @@ async fn the_sweep_expires_closes_restarts_and_gives_up(app: Application) -> lar
7878
assert_eq!(again, processing::Swept::default(), "a second sweep finds nothing");
7979
Ok(())
8080
}
81+
82+
/// A build whose run was killed (a deploy) after the job's next attempt found the dead run's hold and left: the sweep
83+
/// runs it again once the hold has lapsed, a few minutes after its last change, and never while a live run holds it
84+
/// (4 Dec 2026, build 1069 sat in processing with no job).
85+
#[laravel::test]
86+
async fn the_sweep_runs_a_stalled_build_again_once_nothing_holds_it(_app: Application) -> laravel::Result<()> {
87+
use laravel::time::ToSpan;
88+
let org = fixtures::organization("acme").await?;
89+
let project = fixtures::project(&org, "docs").await?;
90+
let stalled = build(&project, "b1", "ci").await?;
91+
db::query!(
92+
"UPDATE builds SET state = 'processing', finalized_at = now() - interval '5 minutes', \
93+
updated_at = now() - interval '4 minutes' WHERE id = {stalled}"
94+
)
95+
.update()
96+
.await?;
97+
let held = laravel::cache::lock(&format!("builds.process:{stalled}"), 120.seconds())?;
98+
assert!(held.acquire().await?, "the test takes the build's hold");
99+
assert_eq!(processing::sweep().await?.restarted, 0, "a run holds it");
100+
assert_eq!(Build::find_or_fail(stalled).await?.state, "processing");
101+
held.release().await?;
102+
assert_eq!(processing::sweep().await?.restarted, 1, "nothing holds it now");
103+
assert_ne!(Build::find_or_fail(stalled).await?.state, "processing", "run again: an empty build concludes");
104+
Ok(())
105+
}

‎frontend/content/docs/self-hosting/environment-variables.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ Each scheduled job pushes its result to a [Gatus](https://gatus.io) external end
194194
| `VISUALCI_RETENTION_HEARTBEAT_TOKEN` | string | | Secret. Image retention's token. |
195195
| `VISUALCI_DEMO_HEARTBEAT_URL` | string | | The demo organization's daily reset. |
196196
| `VISUALCI_DEMO_HEARTBEAT_TOKEN` | string | | Secret. The demo reset's token. |
197-
| `VISUALCI_BUILDS_EXPIRE_HEARTBEAT_URL` | string | | The sweep of abandoned builds (every five minutes). |
197+
| `VISUALCI_BUILDS_EXPIRE_HEARTBEAT_URL` | string | | The builds sweep (every minute). |
198198
| `VISUALCI_BUILDS_EXPIRE_HEARTBEAT_TOKEN` | string | | Secret. The builds sweep's token. |
199199
| `VISUALCI_GROUPS_CHECK_HEARTBEAT_URL` | string | | The daily check of snapshot groups. |
200200
| `VISUALCI_GROUPS_CHECK_HEARTBEAT_TOKEN` | string | | Secret. The groups check's token. |

0 commit comments

Comments
 (0)