Fix hard crash on fresh start, add some more context to errors

This commit is contained in:
Gvidas Juknevičius 2025-03-11 21:34:57 +02:00
parent c2c18154b3
commit ad89b0c64d
Signed by: MCorange
GPG Key ID: 12B1346D720B7FBB
2 changed files with 32 additions and 5 deletions

View File

@ -10,6 +10,7 @@ lazy_static::lazy_static!(
pub enum SongStatus {
Downloading,
Converting,
Failed(String),
Done
}
@ -59,16 +60,27 @@ impl SongCacheDl {
let dl_child = dl_cmd.spawn()?;
self.jobs.insert(sid, SongStatus::Downloading);
std::thread::spawn(move || {
let mut err_line = String::new();
if let Ok(output) = dl_child.wait_with_output() {
for line in String::from_utf8(output.stdout).unwrap().lines() {
if line.contains("ERROR") {
err_line = line.to_string();
}
log::info!("CMD: {}", line);
}
for line in String::from_utf8(output.stderr).unwrap().lines() {
if line.contains("ERROR") {
err_line = line.to_string();
}
log::error!("CMD: {}", line);
}
}
let mut cache = SONG_CACHE_DL.lock().unwrap();
cache.jobs.insert(sid, SongStatus::Done);
if song_p.exists() {
cache.jobs.insert(sid, SongStatus::Done);
} else {
cache.jobs.insert(sid, SongStatus::Failed(err_line));
}
cache.current_jobs -= 1;
});
}
@ -116,9 +128,12 @@ impl SongCacheDl {
log::debug!("from: {from:?} to: {to:?}");
std::fs::copy(&from, &to).unwrap();
from.pop();
std::fs::remove_dir_all(from).unwrap();
let mut cache = SONG_CACHE_DL.lock().unwrap();
cache.jobs.insert(sid, SongStatus::Done);
if let Err(_) = std::fs::remove_dir_all(from) {
cache.jobs.insert(sid, SongStatus::Failed(String::from("Unknown")));
} else {
cache.jobs.insert(sid, SongStatus::Done);
}
cache.current_jobs -= 1;
});
}

View File

@ -78,8 +78,15 @@ impl Cache {
}
pub fn download_song_to_cache(&mut self, sid: uuid::Uuid, song: Song) {
self.song_queue.push((sid, song));
self.song_cache.insert(sid, DlStatus::Downloading);
let song_format = xmpd_settings::Settings::get().unwrap().tooling.song_format.clone();
let mut p = self.cache_dir.clone();
p.push("songs");
p.push(format!("{sid}.{song_format}"));
if !p.exists() {
log::info!("p: {p:?}");
self.song_queue.push((sid, song));
self.song_cache.insert(sid, DlStatus::Downloading);
}
}
pub fn download_icon_to_cache(&mut self, sid: uuid::Uuid, song: Song) {
self.icon_queue.push((sid, song));
@ -128,6 +135,11 @@ fn start_cache_mv_thread(tx: Sender<Message>) {
cache.song_cache.insert(sid.clone(), DlStatus::Done(Some(song_p.into())));
done_jobs.push(sid.clone());
}
} else if let SongStatus::Failed(e) = status {
let mut cache = he!(tx, CACHE.lock());
let _ = tx.send(Message::Error(std::file!(), std::line!() as usize, format!("Failed to download song {sid}: {e}")));
cache.song_cache.insert(sid.clone(), DlStatus::Error(std::file!(), std::line!() as usize, format!("Failed to download song {sid}: {e}")));
done_jobs.push(sid.clone());
}
}
for sid in done_jobs {