Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

6.3 KB
Raw Blame
use std::sync::{
    atomic::{AtomicBool, Ordering},
    Arc, OnceLock,
};

use async_trait::async_trait;

use crate::core::events::DomainEvent;
use tinybus::EventHandler;
use tinybus::SubscriptionHandle;

/// Holds the single process-lifetime subscription handle so it is never
/// double-registered and never dropped (which would abort the task).
static RESTART_HANDLE: OnceLock<SubscriptionHandle> = OnceLock::new();

/// Same idea as [`RESTART_HANDLE`] but for the shutdown subscriber.
static SHUTDOWN_HANDLE: OnceLock<SubscriptionHandle> = OnceLock::new();

/// Register the [`RestartSubscriber`] on the global event bus.
///
/// Idempotent: subsequent calls return immediately if the subscriber is already
/// registered. Owned by the service domain — called from the shared subscriber
/// bootstrap so the JSON-RPC transport stays transport-focused.
pub fn register_restart_subscriber() {
    if RESTART_HANDLE.get().is_some() {
        return;
    }

    match crate::core::bus::BUS.subscribe(Arc::new(RestartSubscriber)) {
        Some(handle) => {
            // Store the handle; OnceLock ensures at most one wins if there is a
            // race between two threads calling this function concurrently.
            let _ = RESTART_HANDLE.set(handle);
        }
        None => {
            log::warn!("[event_bus] failed to register restart subscriber — bus not initialized");
        }
    }
}

/// Register the [`ShutdownSubscriber`] on the global event bus.
///
/// Mirrors [`register_restart_subscriber`] — idempotent, owned by the service
/// domain, called from the shared subscriber bootstrap in `core/runtime/subscribers.rs`.
pub fn register_shutdown_subscriber() {
    if SHUTDOWN_HANDLE.get().is_some() {
        return;
    }

    match crate::core::bus::BUS.subscribe(Arc::new(ShutdownSubscriber)) {
        Some(handle) => {
            let _ = SHUTDOWN_HANDLE.set(handle);
        }
        None => {
            log::warn!("[event_bus] failed to register shutdown subscriber — bus not initialized");
        }
    }
}

/// One-shot gate so only the first restart event actually spawns a replacement
/// process. Subsequent events are ignored (the process is about to exit).
static RESTART_IN_PROGRESS: AtomicBool = AtomicBool::new(false);

/// Same one-shot gate but for shutdown — only the first request actually
/// schedules `process::exit`.
static SHUTDOWN_IN_PROGRESS: AtomicBool = AtomicBool::new(false);

/// Long-lived event-bus subscriber that turns restart requests into a real
/// process respawn.
///
/// This subscriber is registered during core bootstrap so any restart
/// request published from RPC, CLI, or another internal component goes through
/// the same execution path.
pub struct RestartSubscriber;

#[async_trait]
impl EventHandler<DomainEvent> for RestartSubscriber {
    fn name(&self) -> &str {
        "service::restart"
    }

    fn domains(&self) -> Option<&[&str]> {
        Some(&["system"])
    }

    async fn handle(&self, event: &DomainEvent) {
        let DomainEvent::SystemRestartRequested { source, reason } = event else {
            return;
        };

        // Atomically claim the restart slot — only the first caller proceeds.
        if RESTART_IN_PROGRESS
            .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
            .is_err()
        {
            log::debug!(
                "[service:restart] ignoring duplicate restart request source={} (restart already in progress)",
                source,
            );
            return;
        }

        log::warn!(
            "[service:restart] executing restart request source={}",
            source
        );

        match crate::platform::service::restart::trigger_self_restart_now(source, reason) {
            Ok(child_pid) => {
                log::warn!(
                    "[service:restart] replacement pid={} spawned; exiting current process",
                    child_pid
                );
                // Brief 150ms grace period before exit: allows in-flight log
                // flushes and the replacement process to bind its listener before
                // this process terminates. Empirically tuned — increase if logs
                // are truncated on shutdown.
                tokio::spawn(async move {
                    tokio::time::sleep(std::time::Duration::from_millis(150)).await;
                    std::process::exit(0);
                });
            }
            Err(err) => {
                log::error!("[service:restart] failed to restart current process: {err}");
                // Reset the gate so a subsequent attempt can try again.
                RESTART_IN_PROGRESS.store(false, Ordering::SeqCst);
            }
        }
    }
}

/// Long-lived event-bus subscriber that turns shutdown requests into a
/// graceful `process::exit(0)` after a short flush window.
///
/// Distinct from [`RestartSubscriber`]: no replacement process is spawned —
/// we just exit. Frontends that want the process back up are expected to
/// invoke `service.start` (or rely on a supervisor) after calling
/// `service.shutdown`.
pub struct ShutdownSubscriber;

#[async_trait]
impl EventHandler<DomainEvent> for ShutdownSubscriber {
    fn name(&self) -> &str {
        "service::shutdown"
    }

    fn domains(&self) -> Option<&[&str]> {
        Some(&["system"])
    }

    async fn handle(&self, event: &DomainEvent) {
        let DomainEvent::SystemShutdownRequested { source, reason } = event else {
            return;
        };

        if SHUTDOWN_IN_PROGRESS
            .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
            .is_err()
        {
            log::debug!(
                "[service:shutdown] ignoring duplicate shutdown request source={} (already in progress)",
                source,
            );
            return;
        }

        log::warn!(
            "[service:shutdown] executing shutdown source={} reason={}",
            source,
            reason
        );

        // Brief 150ms grace period before exit, mirroring the restart path,
        // so in-flight RPC responses and log writes can flush before the
        // tokio runtime is torn down.
        tokio::spawn(async {
            tokio::time::sleep(std::time::Duration::from_millis(150)).await;
            std::process::exit(0);
        });
    }
}

#[cfg(test)]
#[path = "bus_tests.rs"]
mod tests;