From 3deb9be06f99e07a3d36448d87fe013f0210984f Mon Sep 17 00:00:00 2001 From: DaXcess Date: Fri, 11 Nov 2022 09:55:38 +0100 Subject: [PATCH] More logging to find issue, remove loop from disconnect timer --- src/player.rs | 25 ++++++++++++++++++++++++- src/session/mod.rs | 40 ++++++++++++++++++---------------------- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/player.rs b/src/player.rs index cb1c76a..693828c 100644 --- a/src/player.rs +++ b/src/player.rs @@ -11,7 +11,7 @@ use librespot::{ player::{Player, PlayerEvent}, }, }; -use log::{debug, error, warn}; +use log::{debug, error, trace, warn}; use serde_json::json; use crate::{ @@ -56,10 +56,14 @@ impl SpoticordPlayer { session.shutdown(); } + trace!("Creating Spotify session..."); + // Connect the session let (session, _) = match Session::connect(session_config, credentials, None, false).await { Ok((session, credentials)) => (session, credentials), Err(why) => { + error!("Failed to create Spotify session: {}", why); + self .client .send(IpcPacket::ConnectError(why.to_string())) @@ -102,10 +106,14 @@ impl SpoticordPlayer { let device_id = session.device_id().to_owned(); let ipc = self.client.clone(); + trace!("Successfully created Spotify session"); + // IPC Handler tokio::spawn(async move { let client = reqwest::Client::new(); + let mut retries = 10; + // Try to switch to the device loop { match client @@ -121,10 +129,25 @@ impl SpoticordPlayer { if resp.status() == 202 { debug!("Successfully switched to device"); break; + } else { + trace!("Device switch failed with status {}", resp.status()); + } + + retries -= 1; + + if retries == 0 { + error!("Failed to switch to device"); + ipc + .send(IpcPacket::ConnectError( + "Switch to Spoticord device timed out".to_string(), + )) + .unwrap(); + break; } } Err(why) => { error!("Failed to set device: {}", why); + ipc.send(IpcPacket::ConnectError(why.to_string())).unwrap(); break; } } diff --git a/src/session/mod.rs b/src/session/mod.rs index 932bd91..7905940 100644 --- a/src/session/mod.rs +++ b/src/session/mod.rs @@ -583,32 +583,28 @@ impl SpoticordSession { // Ignore first (immediate) tick timer.tick().await; + timer.tick().await; - loop { - timer.tick().await; + // Make sure this task has not been aborted, if it has this will automatically stop execution. + tokio::task::yield_now().await; - // Make sure this task has not been aborted, if it has this will automatically stop execution. - tokio::task::yield_now().await; + let is_playing = { + let pbi = pbi.read().await; - let is_playing = { - let pbi = pbi.read().await; - - if let Some(pbi) = &*pbi { - pbi.is_playing - } else { - false - } - }; - - if !is_playing { - info!("Player is not playing, disconnecting"); - instance - .disconnect_with_message( - "The player has been inactive for too long, and has been disconnected.", - ) - .await; - break; + if let Some(pbi) = &*pbi { + pbi.is_playing + } else { + false } + }; + + if !is_playing { + info!("Player is not playing, disconnecting"); + instance + .disconnect_with_message( + "The player has been inactive for too long, and has been disconnected.", + ) + .await; } })); }