From cbce7ac600c7472872a0e4d042f2c8b4fdfefe6c Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Thu, 18 Nov 2021 06:17:19 +0000 Subject: [PATCH 1/1] periodically break out of idle mode to see if the connection to MPD is intact it may be hanging if for example the local system was suspended. in this situation mpd would detect the missing peer (us) when e.g. reporting idle changes and close the tcp socket. later, the local system resumes and the local tcp connection is still open and waiting for an idle result forever --- lib/App/MPD/Feeder.pm | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/App/MPD/Feeder.pm b/lib/App/MPD/Feeder.pm index 7da421a..2ce166f 100644 --- a/lib/App/MPD/Feeder.pm +++ b/lib/App/MPD/Feeder.pm @@ -11,6 +11,7 @@ use DBD::Pg; use DBI; use Getopt::Long; use IO::Async::Signal; +use IO::Async::Timer::Periodic; use Log::Any qw($log); use Net::Async::MPD; use Object::Pad; @@ -23,6 +24,7 @@ class App::MPD::Feeder { has $db :reader; has $db_needs_update :writer = 1; has $mpd :reader; + has $idler; use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf'; @@ -233,7 +235,7 @@ use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf'; method prepare_to_wait_idle { $log->trace('declaring idle mode'); - $mpd->send('idle database playlist')->on_done( + $idler = $mpd->send('idle database playlist')->on_done( sub { my $result = shift; @@ -273,6 +275,20 @@ use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf'; $self->connect_mpd; $self->connect_db; + $mpd->loop->add( + IO::Async::Timer::Periodic->new( + interval => 300, + on_tick => sub { + if ($idler) { + $log->trace('breaking idle to see if MPD is there'); + undef $idler; + $log->trace("> noidle (direct)"); + $mpd->{mpd_handle}->write("noidle\n"); + } + }, + )->start + ); + for ( ;; ) { $self->queue_songs( undef, sub { $self->prepare_to_wait_idle } ); -- 2.39.5