From: Damyan Ivanov Date: Sat, 27 Nov 2021 07:36:48 +0000 (+0000) Subject: add systemd integration (optional) X-Git-Url: https://git.ktnx.net/?p=mpd-feeder.git;a=commitdiff_plain;h=13f4e9d07015eaccbc5524b8d1513ff7c79922bd add systemd integration (optional) --- diff --git a/dist.ini b/dist.ini index 68e4b22..864a4ec 100644 --- a/dist.ini +++ b/dist.ini @@ -20,6 +20,9 @@ Syntax::Keyword::Try = 0 Time::Duration = 0 Time::Duration::Parse = 0 utf8::all = 0 +-phase = runtime +-relationship = recommends +Linux::Systemd::Daemon [Test::Compile] ;skip = Test$ diff --git a/eg/mpd-feeder.service b/eg/mpd-feeder.service index d98a43d..1b762d0 100644 --- a/eg/mpd-feeder.service +++ b/eg/mpd-feeder.service @@ -3,7 +3,7 @@ Description=MPD playlist manager ConditionPathExists=/etc/mpd-feeder/mpd-feeder.conf [Service] -Type=exec +Type=notify User=mpd-feeder ExecStart=/usr/bin/mpd-feeder StandardError=journal diff --git a/lib/App/MPD/Feeder.pm b/lib/App/MPD/Feeder.pm index b6c8203..7211fab 100644 --- a/lib/App/MPD/Feeder.pm +++ b/lib/App/MPD/Feeder.pm @@ -17,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; @@ -53,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; @@ -60,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; @@ -157,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; @@ -201,7 +212,7 @@ 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); @@ -218,6 +229,7 @@ method queue_songs( $num = undef ) { 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; @@ -241,6 +253,7 @@ method queue_songs( $num = undef ) { } $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->info("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,7 +356,7 @@ 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; @@ -391,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 { @@ -405,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; @@ -419,6 +437,7 @@ method run_loop { $mpd->loop->later( sub { $self->pulse } ); + Linux::Systemd::Daemon::sd_ready() if UNDER_SYSTEMD; $mpd->loop->run; } }