diff --git a/backend/src/main.rs b/backend/src/main.rs index 668ca7e..0bacb64 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -45,6 +45,7 @@ pub struct AppState { pub db: db::Db, #[cfg(feature = "push-notifications")] pub push_store: push::PushSubscriptionStore, + pub notify_poll: Arc, } async fn auth_middleware( @@ -336,6 +337,8 @@ async fn main() { #[cfg(not(feature = "push-notifications"))] let push_store = (); + let notify_poll = Arc::new(tokio::sync::Notify::new()); + let app_state = AppState { tx: tx.clone(), event_bus: event_bus.clone(), @@ -343,6 +346,7 @@ async fn main() { db: db.clone(), #[cfg(feature = "push-notifications")] push_store, + notify_poll: notify_poll.clone(), }; // Spawn background task to poll rTorrent @@ -351,6 +355,7 @@ async fn main() { let socket_path = args.socket.clone(); // Clone for background task #[cfg(feature = "push-notifications")] let push_store_clone = app_state.push_store.clone(); + let notify_poll_clone = notify_poll.clone(); tokio::spawn(async move { let client = xmlrpc::RtorrentClient::new(&socket_path); @@ -438,8 +443,13 @@ async fn main() { previous_torrents = new_torrents; - // Success case: sleep for the determined interval - tokio::time::sleep(loop_interval).await; + // Success case: wait for the determined interval OR a wakeup notification + tokio::select! { + _ = tokio::time::sleep(loop_interval) => {}, + _ = notify_poll_clone.notified() => { + tracing::debug!("Background loop awakened by new client connection"); + } + } } Err(e) => { tracing::error!("Error fetching torrents in background: {}", e); diff --git a/backend/src/sse.rs b/backend/src/sse.rs index 099f594..0d62c2c 100644 --- a/backend/src/sse.rs +++ b/backend/src/sse.rs @@ -195,6 +195,9 @@ pub async fn fetch_global_stats(client: &RtorrentClient) -> Result, ) -> Sse>> { + // Notify background worker to wake up and poll immediately + state.notify_poll.notify_one(); + // Get initial value synchronously (from the watch channel's current state) let initial_rx = state.tx.subscribe(); let initial_torrents = initial_rx.borrow().clone();