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;
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;
$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;
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;
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);
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;
}
$log->notice( "Adding " . join( ', ', map {"«$_»"} @paths ) );
+ $self->status('Adding songs to the playlist');
# MPD needs raw bytes
utf8::encode($_) for @paths;
}
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;
}
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(
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 ) {
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(
"no active MPD communication for more that 5 minutes");
- $log->debug("forcing alive check");
+ $self->status("checking connection", 'debug');
$self->break_idle;
}
else {
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;
$mpd->loop->later( sub { $self->pulse } );
+ Linux::Systemd::Daemon::sd_ready() if UNDER_SYSTEMD;
$mpd->loop->run;
}
}