]> git.ktnx.net Git - mpd-feeder.git/blobdiff - lib/App/MPD/Feeder.pm
add systemd integration (optional)
[mpd-feeder.git] / lib / App / MPD / Feeder.pm
index 8561cf8206eeae69dc02596cb73c5da7cb18d7a8..7211fabdf53c0022a38eb311aadfe6c9ea51b601 100644 (file)
@@ -5,7 +5,6 @@ class App::MPD::Feeder;
 
 use App::MPD::Feeder::DB;
 use App::MPD::Feeder::Options;
-use App::MPD::Feeder::WorkQueue;
 use DBD::Pg;
 use DBI;
 use Getopt::Long;
@@ -18,6 +17,8 @@ use Object::Pad;
 use Syntax::Keyword::Try;
 use Time::Duration qw(duration_exact);
 
+use constant UNDER_SYSTEMD => eval { require Linux::Systemd::Daemon };
+
 has $cfg_file :reader;
 has $opt :reader;
 has $db :reader;
@@ -54,6 +55,9 @@ method configure {
 
     Log::Any::Adapter->set( Stderr => log_level => $new_opt->log_level );
 
+    $log->debug( "Systemd integration "
+            . ( UNDER_SYSTEMD ? "available" : "not available" ) );
+
     $opt = $new_opt;
 
     $reconnect_delay = $opt->initial_reconnect_delay;
@@ -61,6 +65,11 @@ method configure {
     $db = App::MPD::Feeder::DB->new( opt => $opt );
 }
 
+method status($text, $log_level = undef) {
+    Linux::Systemd::Daemon::sd_notify( status => $text ) if UNDER_SYSTEMD;
+    $log->$log_level($text) if $log_level;
+}
+
 method init_mpd {
     return if $mpd;
 
@@ -158,12 +167,13 @@ method update_db( $force = undef ) {
         return;
     }
 
-    $log->info('Updating song database');
+    $self->status('Updating song database', 'info');
 
     my $rows = $mpd->send('listallinfo')->get;
 
     $log->trace('got all songs from MPD');
 
+    $self->status('Updating local song database', 'debug');
     $db->start_update;
     try {
         my $song_count;
@@ -182,7 +192,7 @@ method update_db( $force = undef ) {
             $new_songs,   $new_artists,   $new_albums
         ) = $self->db->finish_update;
 
-        $log->info(
+        $log->notice(
             "Updated data about $song_count songs (including $new_songs new), "
                 . "$total_artists artists (including $new_artists new) "
 
@@ -202,22 +212,24 @@ method queue_songs( $num = undef ) {
     if ( !defined $num ) {
         return unless $playlist_needs_filling;
 
-        $log->trace("Requesting playlist");
+        $self->status("Requesting playlist", 'trace');
         my $present = $mpd->send('playlist')->get // [];
         $present = scalar(@$present);
 
-        $log->notice( "Playlist contains $present songs. Wanted: "
-                . $opt->target_queue_length );
         if ( $present < $opt->target_queue_length ) {
+            $log->notice( "Playlist contains $present songs. Wanted: "
+                    . $opt->target_queue_length );
             $self->queue_songs( $opt->target_queue_length - $present );
         }
         else {
+            $log->info("Playlist contains $present songs");
             $playlist_needs_filling = 0;
         }
 
         return;
     }
 
+    $self->status("Looking for suitable songs to queue", 'debug');
     my @list = $self->db->find_suitable_songs($num);
 
     die "Found no suitable songs" unless @list;
@@ -231,7 +243,7 @@ method queue_songs( $num = undef ) {
         );
     }
 
-    $log->info("About to add $num songs to the playlist");
+    $log->debug("About to add $num songs to the playlist");
 
     my @paths;
     for my $song (@list) {
@@ -240,7 +252,8 @@ method queue_songs( $num = undef ) {
         push @paths, $path;
     }
 
-    $log->debug( "Adding " . join( ', ', map {"«$_»"} @paths ) );
+    $log->notice( "Adding " . join( ', ', map {"«$_»"} @paths ) );
+    $self->status('Adding songs to the playlist');
 
     # MPD needs raw bytes
     utf8::encode($_) for @paths;
@@ -260,7 +273,8 @@ method queue_songs( $num = undef ) {
 }
 
 method reexec {
-    $log->notice("disconnecting and re-starting");
+    $self->status("disconnecting and re-starting", 'info');
+    Linux::Systemd::Daemon::sd_reloading() if UNDER_SYSTEMD;
     $db->disconnect;
     undef $mpd;
 
@@ -288,9 +302,12 @@ method break_idle {
 }
 
 method sleep_before_reconnection {
-    $log->debug( "Waiting for "
+    $self->status(
+        "Waiting for "
             . duration_exact($reconnect_delay)
-            . " before re-connecting" );
+            . " before re-connecting",
+        'debug'
+    );
 
     $mpd->loop->add(
         IO::Async::Timer::Countdown->new(
@@ -306,7 +323,7 @@ method sleep_before_reconnection {
 
 method pulse {
     unless ($mpd_connected) {
-        $log->trace("Connecting to MPD...");
+        $self->status("Connecting to MPD", 'trace');
         my $f = $mpd->connect->await;
 
         if ( $f->is_done ) {
@@ -339,30 +356,32 @@ method pulse {
         return;
     }
 
-    $log->debug("Waiting idle. PID=$$");
+    $self->status("Waiting for playlist/database changes", 'debug');
     $last_mpd_comm = time;
     $idler         = $mpd->send("idle database playlist");
     $idler->await;
 
     $log->trace('got out of idle');
 
-    if ( $idler->is_done ) {
-        my $result = $idler->get;
-        undef $idler;
-        if ( ref $result and $result->{changed} ) {
-            my $changed = $result->{changed};
-            $changed = [$changed] unless ref $changed;
+    if ($idler) {
+        if ( $idler->is_done ) {
+            my $result = $idler->get;
+            undef $idler;
+            if ( ref $result and $result->{changed} ) {
+                my $changed = $result->{changed};
+                $changed = [$changed] unless ref $changed;
 
-            $mpd->emit($_) for @$changed;
+                $mpd->emit($_) for @$changed;
+            }
+        }
+        elsif ( $idler->is_cancelled ) {
+            $log->trace("idle was cancelled");
+            undef $idler;
+        }
+        elsif ( $idler->is_failed ) {
+            $log->warn("idle failed: ".$idler->failure);
+            undef $idler;
         }
-    }
-    elsif ( $idler->is_cancelled ) {
-        $log->trace("idle was cancelled");
-        undef $idler;
-    }
-    elsif ( $idler->is_failed ) {
-        $log->warn("idle failed: ".$idler->failure);
-        undef $idler;
     }
 
     $mpd->loop->stop;
@@ -389,7 +408,7 @@ method run_loop {
 
                     $log->trace(
                         "no active MPD communication for more that 5 minutes");
-                    $log->debug("forcing alive check");
+                    $self->status("checking connection", 'debug');
                     $self->break_idle;
                 }
                 else {
@@ -403,7 +422,8 @@ method run_loop {
 
     for ( ;; ) {
         if ( $quit_requested ) {
-            $log->trace("about to quit");
+            $self->status("about to quit", 'debug');
+            Linux::Systemd::Daemon::sd_stopping() if UNDER_SYSTEMD;
             undef $mpd;
             $db->disconnect;
             last;
@@ -417,6 +437,7 @@ method run_loop {
 
         $mpd->loop->later( sub { $self->pulse } );
 
+        Linux::Systemd::Daemon::sd_ready() if UNDER_SYSTEMD;
         $mpd->loop->run;
     }
 }