]> git.ktnx.net Git - mpd-feeder.git/blobdiff - lib/App/MPD/Feeder.pm
do not crash when the playlist is cleared
[mpd-feeder.git] / lib / App / MPD / Feeder.pm
index 9cb1a2543f901751839e563a15ba0802f50dde8c..ed5d27658da5a27a6a9d2a88b9ca5b4372f059c8 100644 (file)
@@ -21,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';
@@ -61,6 +61,12 @@ use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf';
 
         $mpd = Net::Async::MPD->new(%conn);
 
+        $mpd->on(
+            close => sub {
+                die "Connection to MPD lost";
+            }
+        );
+
         my $int_signal_handler = sub {
             state $signal_count = 0;
             $signal_count++;
@@ -126,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;
@@ -136,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;
         }
@@ -160,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 );
@@ -241,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;
 
@@ -258,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=$$");