X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;ds=sidebyside;f=lib%2FApp%2FMPD%2FFeeder.pm;h=ed5d27658da5a27a6a9d2a88b9ca5b4372f059c8;hb=e72d5851c56146ac78be91b720ed7d7451fcc0d0;hp=1b5ed30e5bb36f82a8a39c33b664c8f2838d2ae4;hpb=b4924aa496dece6edca9d4c3eb598b1850b6499f;p=mpd-feeder.git diff --git a/lib/App/MPD/Feeder.pm b/lib/App/MPD/Feeder.pm index 1b5ed30..ed5d276 100644 --- a/lib/App/MPD/Feeder.pm +++ b/lib/App/MPD/Feeder.pm @@ -3,6 +3,7 @@ package App::MPD::Feeder; use strict; use warnings; use utf8; +use feature 'state'; use App::MPD::Feeder::Options; use App::MPD::Feeder::DB; @@ -20,7 +21,7 @@ class App::MPD::Feeder { has $cfg_file :reader; has $opt :reader; has $db :reader; - has $db_needs_update = 1; + has $db_needs_update :writer = 1; has $mpd :reader; use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf'; @@ -60,31 +61,39 @@ use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf'; $mpd = Net::Async::MPD->new(%conn); - $mpd->loop->add( - IO::Async::Signal->new( - name => 'TERM', - on_receipt => sub { - $log->debug("SIGTERM received. Stopping loop"); - $mpd->loop->stop('quit'); - }, - ) + $mpd->on( + close => sub { + die "Connection to MPD lost"; + } ); - $mpd->loop->add( - IO::Async::Signal->new( - name => 'INT', - on_receipt => sub { - $log->debug("SIGINT received. Stopping loop"); - $mpd->loop->stop('quit'); - }, - ) - ); + my $int_signal_handler = sub { + state $signal_count = 0; + $signal_count++; + $log->debug("Signal received. Stopping loop"); + $mpd->loop->stop('quit'); + + if ( $signal_count > 1 ) { + $log->warn("Another signal received (#$signal_count)"); + $log->warn("Exiting abruptly"); + exit 2; + } + }; + + for (qw(TERM INT)) { + $mpd->loop->add( + IO::Async::Signal->new( + name => $_, + on_receipt => $int_signal_handler, + ) + ); + } $mpd->loop->add( IO::Async::Signal->new( name => 'HUP', on_receipt => sub { - $log->debug("SIGHUP received. Stopping loop"); + $log->debug("SIGHUP received. Scheduling reload"); $mpd->loop->stop('reload'); }, ) @@ -123,6 +132,8 @@ use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf'; my $rows = $mpd->send('listallinfo')->get; + $log->trace('got all songs from MPD'); + $db->start_update; try { my $song_count; @@ -133,14 +144,20 @@ use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf'; $self->db->store_song( $entry->{file}, $entry->{AlbumArtist} // $entry->{Artist}, $entry->{Album} ); + $song_count++; } - $log->info("Updated data about $song_count songs"); + my ($total_songs, $total_artists, $total_albums, + $new_songs, $new_artists, $new_albums + ) = $self->db->finish_update; - $self->db->remove_stale_entries; + $log->info( + "Updated data about $song_count songs (including $new_songs new), " + . "$total_artists artists (including $new_artists new) " - $self->db->finish_update; + . "and $total_albums albums (including $new_albums new)" + ); $db_needs_update = 0; } @@ -157,7 +174,7 @@ use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf'; $self->connect_mpd; $mpd->send('playlist')->on_done( sub { - my $present = scalar @{ $_[0] }; + my $present = scalar @{ $_[0] // [] }; $log->notice( "Playlist contains $present songs. Wanted: " . $opt->target_queue_length ); @@ -238,16 +255,6 @@ use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf'; ); } - method run { - $mpd->on( - close => sub { - die "Connection to MPD lost"; - } - ); - - $self->prepare_to_wait_idle; - } - method stop { undef $mpd; @@ -255,10 +262,11 @@ use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf'; } method run_loop { + $self->connect_mpd; $self->connect_db; for ( ;; ) { - $self->queue_songs( undef, sub { $self->run } ); + $self->queue_songs( undef, sub { $self->prepare_to_wait_idle } ); $log->debug("Entering event loop. PID=$$");