connect then retry
This commit is contained in:
parent
18a56f4454
commit
1563e98985
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mpdyn"
|
||||
version = "0.1.3"
|
||||
version = "0.2.0"
|
||||
authors = ["Julien Rabier <taziden@flexiden.org>"]
|
||||
edition = "2024"
|
||||
|
||||
|
||||
9
justfile
Normal file
9
justfile
Normal file
@ -0,0 +1,9 @@
|
||||
alias b:= build
|
||||
alias i:= install
|
||||
|
||||
build:
|
||||
cargo build --release
|
||||
|
||||
install:
|
||||
cargo install --path .
|
||||
systemctl --user restart mpdyn
|
||||
31
src/main.rs
31
src/main.rs
@ -3,14 +3,15 @@ use mpdrs::{Client, Song};
|
||||
use rand::{prelude::*, rng};
|
||||
use std::{error::Error, thread, time};
|
||||
|
||||
const CON: &str = "127.0.0.1:6600";
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
// We enable consume mode here
|
||||
Client::default().consume(true)?;
|
||||
connect_retry().consume(true)?;
|
||||
|
||||
// We launch an infinite loop
|
||||
loop {
|
||||
//println!("{} checking playlist ", Local::now().format("%T"));
|
||||
let ql = Client::default().status()?.queue_len;
|
||||
let ql = connect_retry().status()?.queue_len;
|
||||
if ql < 10u32 {
|
||||
populate_playlist(10u32 - ql)?;
|
||||
};
|
||||
@ -19,6 +20,18 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
}
|
||||
|
||||
fn connect_retry() -> Client {
|
||||
loop {
|
||||
if let Ok(res) = Client::connect(CON) {
|
||||
break res;
|
||||
} else {
|
||||
println!("Unable to connect to MPD, retrying in 5 seconds");
|
||||
thread::sleep(time::Duration::from_secs(5));
|
||||
connect_retry()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn populate_playlist(a: u32) -> Result<(), Box<dyn Error>> {
|
||||
if a == 1 {
|
||||
println!("need to add one song");
|
||||
@ -27,11 +40,11 @@ fn populate_playlist(a: u32) -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
|
||||
for song in get_random_tracks(a)? {
|
||||
match Client::default().push(&song.file) {
|
||||
match connect_retry().push(&song.file) {
|
||||
Ok(_) => println!(
|
||||
"adding: {:} by {:}",
|
||||
song.title.unwrap(),
|
||||
song.artist.unwrap()
|
||||
"adding: \"{}\" by \"{}\"",
|
||||
song.title.unwrap_or_default(),
|
||||
song.artist.unwrap_or_default()
|
||||
),
|
||||
Err(_) => println!("unable to add {:?}", song.title),
|
||||
}
|
||||
@ -42,7 +55,7 @@ fn populate_playlist(a: u32) -> Result<(), Box<dyn Error>> {
|
||||
fn get_random_tracks(n: u32) -> Result<Vec<Song>, Box<dyn Error>> {
|
||||
let mut rng = rng();
|
||||
|
||||
let query_albums = Client::default().lsinfo("/")?;
|
||||
let query_albums = connect_retry().lsinfo("/").unwrap_or_default();
|
||||
|
||||
let tracks: Vec<Song> = Iterator::filter_map(
|
||||
query_albums
|
||||
@ -69,7 +82,7 @@ fn match_directory(response: &LsInfoResponse) -> Result<i32, i32> {
|
||||
fn get_random_track(path: &str) -> Option<Song> {
|
||||
let mut rng = rng();
|
||||
|
||||
match Client::default().lsinfo(path) {
|
||||
match connect_retry().lsinfo(path) {
|
||||
Ok(songs) => match songs.choose(&mut rng) {
|
||||
Some(LsInfoResponse::Song(song)) => Some(song.clone()),
|
||||
_ => None,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user