use App::MPD::Feeder::DB;
use App::MPD::Feeder::Options;
-use App::MPD::Feeder::WorkQueue;
use DBD::Pg;
use DBI;
use Getopt::Long;
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;
$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) "
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;
);
}
- $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) {
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;
}
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;
}
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('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;
$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;
}
}