Skip to content
Merged
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
25 changes: 22 additions & 3 deletions app/src/live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub async fn ready(database_url: &str, within: Duration) -> bool {
#[must_use]
pub fn healthy() -> bool {
let last = LAST_PROBE_OK.load(Ordering::Relaxed);
last > 0 && laravel::now().timestamp().as_second() - last < 180
last > 0 && unix_seconds() - last < 180
}

/// This process's listener, for `/status`: `idle` before anything subscribed (nothing to hear yet), `starting` in its
Expand All @@ -113,7 +113,9 @@ pub fn listener_state() -> &'static str {
}
}

/// Now, in Unix seconds, from the system clock: the hub starts outside any request, where Laravel's clock isn't set.
/// Now, in Unix seconds, from the system clock: the hub and its probes run in tasks of their own, outside any request,
/// where Laravel's clock isn't set (calling Laravel's `now` there panics: on 3 Dec 2026 that killed the listener on
/// its first probe, and /status read `stale`). Everything about the probe uses this clock.
fn unix_seconds() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
Expand Down Expand Up @@ -154,7 +156,7 @@ async fn supervise(url: String, tx: broadcast::Sender<String>, ready: watch::Sen
break;
}
if probe(&url, &tx).await {
LAST_PROBE_OK.store(laravel::now().timestamp().as_second(), Ordering::Relaxed);
LAST_PROBE_OK.store(unix_seconds(), Ordering::Relaxed);
} else {
tracing::warn!("live updates: a probe didn't come back; restarting the listener");
break;
Expand Down Expand Up @@ -274,3 +276,20 @@ impl CheckLive {
fn schedule(schedule: &mut Schedule) {
schedule.job(CheckLive).every_minute().without_overlapping();
}

#[cfg(test)]
mod tests {
use super::*;

/// The probe's bookkeeping runs in the listener's own task, outside the application: it must not need Laravel's
/// clock (a plain test has no application, as that task has none).
#[test]
fn probe_health_needs_no_application_context() {
LAST_PROBE_OK.store(unix_seconds(), Ordering::Relaxed);
assert!(healthy());
LAST_PROBE_OK.store(unix_seconds() - 600, Ordering::Relaxed);
assert!(!healthy());
LAST_PROBE_OK.store(0, Ordering::Relaxed);
assert!(!healthy());
}
}
Loading