-package App::MPD::Feeder;
-
-use strict;
-use warnings;
+use v5.28;
use utf8;
-use feature 'state';
+use Object::Pad;
+class App::MPD::Feeder;
use App::MPD::Feeder::DB;
use App::MPD::Feeder::Options;
use Object::Pad;
use Syntax::Keyword::Try;
-class App::MPD::Feeder {
- has $cfg_file :reader;
- has $opt :reader;
- has $db :reader;
- has $db_needs_update :writer = 1;
- has $mpd :reader;
- has $idler;
- has $work_queue = App::MPD::Feeder::WorkQueue->new;
- has $last_mpd_comm;
+has $cfg_file :reader;
+has $opt :reader;
+has $db :reader;
+has $db_needs_update :writer = 1;
+has $mpd :reader;
+has $idler;
+has $work_queue = App::MPD::Feeder::WorkQueue->new;
+has $last_mpd_comm;
use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf';
- ADJUST {
- Getopt::Long::Configure('pass_through');
- Getopt::Long::GetOptions('cfg|config=s' => \$cfg_file);
- Getopt::Long::Configure('no_pass_through');
+ADJUST {
+ Getopt::Long::Configure('pass_through');
+ Getopt::Long::GetOptions('cfg|config=s' => \$cfg_file);
+ Getopt::Long::Configure('no_pass_through');
- $cfg_file //= DEFAULT_CONFIG_FILE if -e DEFAULT_CONFIG_FILE;
+ $cfg_file //= DEFAULT_CONFIG_FILE if -e DEFAULT_CONFIG_FILE;
- $self->configure;
+ $self->configure;
- $db_needs_update = 0 if $opt->skip_db_update;
- }
-
- method configure {
- my $new_opt = App::MPD::Feeder::Options->new;
+ $db_needs_update = 0 if $opt->skip_db_update;
+}
- $new_opt->parse_config_file($cfg_file) if $cfg_file;
+method configure {
+ my $new_opt = App::MPD::Feeder::Options->new;
- $new_opt->parse_command_line;
+ $new_opt->parse_config_file($cfg_file) if $cfg_file;
- Log::Any::Adapter->set( Stderr => log_level => $new_opt->log_level );
+ $new_opt->parse_command_line;
- $opt = $new_opt;
+ Log::Any::Adapter->set( Stderr => log_level => $new_opt->log_level );
- $db = App::MPD::Feeder::DB->new( opt => $opt );
- }
+ $opt = $new_opt;
- method connect_mpd {
- return if $mpd;
+ $db = App::MPD::Feeder::DB->new( opt => $opt );
+}
- my %conn = ( auto_connect => 1 );
- $conn{host} = $opt->mpd_host if $opt->mpd_host;
- $conn{port} = $opt->mpd_port if $opt->mpd_port;
+method connect_mpd {
+ return if $mpd;
- $mpd = Net::Async::MPD->new(%conn);
+ my %conn = ( auto_connect => 1 );
+ $conn{host} = $opt->mpd_host if $opt->mpd_host;
+ $conn{port} = $opt->mpd_port if $opt->mpd_port;
- $mpd->on(
- close => sub {
- die "Connection to MPD lost";
- }
- );
- $mpd->on(
- playlist => sub {
- $work_queue->add('playlist');
- }
- );
- $mpd->on(
- database => sub {
- $work_queue->add('database');
- }
- );
+ $mpd = Net::Async::MPD->new(%conn);
- my $int_signal_handler = sub {
- state $signal_count = 0;
- $signal_count++;
- $log->debug("Signal received. Stopping loop");
- $work_queue->add('quit');
- $self->break_idle;
-
- if ( $signal_count > 1 ) {
- $log->warn("Another signal received (#$signal_count)");
- $log->warn("Exiting abruptly");
- exit 2;
- }
- };
-
- for (qw(TERM INT)) {
- $mpd->loop->add(
- IO::Async::Signal->new(
- name => $_,
- on_receipt => $int_signal_handler,
- )
- );
+ $mpd->on(
+ close => sub {
+ die "Connection to MPD lost";
}
+ );
+ $mpd->on(
+ playlist => sub {
+ $work_queue->add('playlist');
+ }
+ );
+ $mpd->on(
+ database => sub {
+ $work_queue->add('database');
+ }
+ );
+
+ my $int_signal_handler = sub {
+ state $signal_count = 0;
+ $signal_count++;
+ $log->debug("Signal received. Stopping loop");
+ $work_queue->add('quit');
+ $self->break_idle;
+
+ if ( $signal_count > 1 ) {
+ $log->warn("Another signal received (#$signal_count)");
+ $log->warn("Exiting abruptly");
+ exit 2;
+ }
+ };
+ for (qw(TERM INT)) {
$mpd->loop->add(
IO::Async::Signal->new(
- name => 'HUP',
- on_receipt => sub {
- $log->debug("SIGHUP received. Scheduling reload");
- $work_queue->add('reload');
- $self->break_idle;
- },
- )
- );
-
- $mpd->loop->add(
- IO::Async::Signal->new(
- name => 'USR1',
- on_receipt => sub {
- $log->debug("SIGUSR1 received. Dumping configuration to STDERR");
- my $old = select \*STDERR;
- try {
- $opt->dump;
- }
- finally {
- select $old;
- }
- },
+ name => $_,
+ on_receipt => $int_signal_handler,
)
);
}
- method connect_db {
- $db->connect($opt);
- $self->update_db;
- }
-
- method update_db($force = undef) {
- if (!$db_needs_update and !$force) {
- $log->debug("Skipping DB update");
- return;
- }
-
- $log->info('Updating song database');
- $self->connect_mpd;
-
- my $rows = $mpd->send('listallinfo')->get;
+ $mpd->loop->add(
+ IO::Async::Signal->new(
+ name => 'HUP',
+ on_receipt => sub {
+ $log->debug("SIGHUP received. Scheduling reload");
+ $work_queue->add('reload');
+ $self->break_idle;
+ },
+ )
+ );
+
+ $mpd->loop->add(
+ IO::Async::Signal->new(
+ name => 'USR1',
+ on_receipt => sub {
+ $log->debug("SIGUSR1 received. Dumping configuration to STDERR");
+ my $old = select \*STDERR;
+ try {
+ $opt->dump;
+ }
+ finally {
+ select $old;
+ }
+ },
+ )
+ );
+}
- $log->trace('got all songs from MPD');
+method connect_db {
+ $db->connect($opt);
+ $self->update_db;
+}
- $db->start_update;
- try {
- my $song_count;
+method update_db($force = undef) {
+ if (!$db_needs_update and !$force) {
+ $log->debug("Skipping DB update");
+ return;
+ }
- foreach my $entry (@$rows) {
- next unless exists $entry->{file};
+ $log->info('Updating song database');
+ $self->connect_mpd;
- $self->db->store_song( $entry->{file},
- $entry->{AlbumArtist} // $entry->{Artist},
- $entry->{Album} );
+ my $rows = $mpd->send('listallinfo')->get;
- $song_count++;
- }
+ $log->trace('got all songs from MPD');
- my ($total_songs, $total_artists, $total_albums,
- $new_songs, $new_artists, $new_albums
- ) = $self->db->finish_update;
+ $db->start_update;
+ try {
+ my $song_count;
- $log->info(
- "Updated data about $song_count songs (including $new_songs new), "
- . "$total_artists artists (including $new_artists new) "
+ foreach my $entry (@$rows) {
+ next unless exists $entry->{file};
- . "and $total_albums albums (including $new_albums new)"
- );
+ $self->db->store_song( $entry->{file},
+ $entry->{AlbumArtist} // $entry->{Artist},
+ $entry->{Album} );
- $db_needs_update = 0;
+ $song_count++;
}
- catch {
- my $err = $@;
- $self->db->cancel_update;
- die $err;
- }
- }
- method queue_songs($num = undef) {
- $self->connect_db;
- if (!defined $num) {
- $self->connect_mpd;
- $log->trace("Requesting playlist");
- 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 ) {
- $self->queue_songs(
- $opt->target_queue_length - $present );
- }
+ my ($total_songs, $total_artists, $total_albums,
+ $new_songs, $new_artists, $new_albums
+ ) = $self->db->finish_update;
- return;
- }
+ $log->info(
+ "Updated data about $song_count songs (including $new_songs new), "
+ . "$total_artists artists (including $new_artists new) "
- my @list = $self->db->find_suitable_songs($num);
+ . "and $total_albums albums (including $new_albums new)"
+ );
- die "Found no suitable songs" unless @list;
+ $db_needs_update = 0;
+ }
+ catch {
+ my $err = $@;
+ $self->db->cancel_update;
+ die $err;
+ }
+}
- if ( @list < $num ) {
- $log->warn(
- sprintf(
- 'Found only %d suitable songs instead of %d',
- scalar(@list), $num
- )
- );
+method queue_songs($num = undef) {
+ $self->connect_db;
+ if (!defined $num) {
+ $self->connect_mpd;
+ $log->trace("Requesting playlist");
+ 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 ) {
+ $self->queue_songs(
+ $opt->target_queue_length - $present );
}
- $log->info("About to add $num songs to the playlist");
+ return;
+ }
+
+ my @list = $self->db->find_suitable_songs($num);
- my @paths;
- for my $song (@list) {
- my $path = $song->{song};
- $path =~ s/"/\\"/g;
- push @paths, $path;
- }
+ die "Found no suitable songs" unless @list;
- $log->debug( "Adding " . join( ', ', map {"«$_»"} @paths ) );
- # MPD needs raw bytes
- utf8::encode($_) for @paths;
- my @commands;
- for (@paths) {
- push @commands, [ add => "\"$_\"" ];
- }
- $self->connect_mpd;
- my $f = $mpd->send( \@commands );
- $f->on_fail( sub { die @_ } );
- $f->on_done(
- sub {
- $self->db->note_song_qeued($_) for @list;
- }
+ if ( @list < $num ) {
+ $log->warn(
+ sprintf(
+ 'Found only %d suitable songs instead of %d',
+ scalar(@list), $num
+ )
);
- $f->get;
}
- method stop {
- undef $mpd;
+ $log->info("About to add $num songs to the playlist");
- $db->disconnect;
+ my @paths;
+ for my $song (@list) {
+ my $path = $song->{song};
+ $path =~ s/"/\\"/g;
+ push @paths, $path;
}
- method handle_work_queue {
- while ( my $item = $work_queue->next ) {
- if ( $item eq 'playlist' ) {
- $self->queue_songs;
- }
- elsif ( $item eq 'database' ) {
- $db_needs_update = 1;
- $self->update_db;
- }
- elsif ( $item eq 'reload' ) {
- $log->notice("disconnecting and re-starting");
- $self->stop;
-
- my @exec =
- ( $0, '--config', $self->cfg_file, '--skip-db-update' );
- if ( $log->is_trace ) {
- $log->trace( 'exec '
- . join( ' ', map { /\s/ ? "'$_'" : $_ } @exec ) );
- }
- exec(@exec);
- }
- elsif ( $item eq 'quit' ) {
- $log->trace("quitting");
- $self->stop;
- exit 0;
- }
- else {
- die "Unknown work queue item '$item'";
- }
- }
+ $log->debug( "Adding " . join( ', ', map {"«$_»"} @paths ) );
+ # MPD needs raw bytes
+ utf8::encode($_) for @paths;
+ my @commands;
+ for (@paths) {
+ push @commands, [ add => "\"$_\"" ];
}
+ $self->connect_mpd;
+ my $f = $mpd->send( \@commands );
+ $f->on_fail( sub { die @_ } );
+ $f->on_done(
+ sub {
+ $self->db->note_song_qeued($_) for @list;
+ }
+ );
+ $f->get;
+}
- method break_idle {
- if ($idler && !$idler->is_ready) {
- $log->trace("hand-sending 'noidle'");
- undef $idler;
- $mpd->{mpd_handle}->write("noidle\n");;
+method stop {
+ undef $mpd;
+
+ $db->disconnect;
+}
+
+method handle_work_queue {
+ while ( my $item = $work_queue->next ) {
+ if ( $item eq 'playlist' ) {
+ $self->queue_songs;
+ }
+ elsif ( $item eq 'database' ) {
+ $db_needs_update = 1;
+ $self->update_db;
+ }
+ elsif ( $item eq 'reload' ) {
+ $log->notice("disconnecting and re-starting");
+ $self->stop;
+
+ my @exec =
+ ( $0, '--config', $self->cfg_file, '--skip-db-update' );
+ if ( $log->is_trace ) {
+ $log->trace( 'exec '
+ . join( ' ', map { /\s/ ? "'$_'" : $_ } @exec ) );
+ }
+ exec(@exec);
+ }
+ elsif ( $item eq 'quit' ) {
+ $log->trace("quitting");
+ $self->stop;
+ exit 0;
}
else {
- $log->trace("no idler found");
+ die "Unknown work queue item '$item'";
}
}
+}
- method run_loop {
- $self->connect_mpd;
- $self->connect_db;
+method break_idle {
+ if ($idler && !$idler->is_ready) {
+ $log->trace("hand-sending 'noidle'");
+ undef $idler;
+ $mpd->{mpd_handle}->write("noidle\n");;
+ }
+ else {
+ $log->trace("no idler found");
+ }
+}
- $mpd->loop->add(
- IO::Async::Timer::Periodic->new(
- interval => 60,
- on_tick => sub {
- if ( time - $last_mpd_comm > 300 ) {
-
- $log->trace(
- "no active MPD communication for more that 5 minutes"
- );
- $log->trace("forcing alive check");
- $self->break_idle;
- }
- else {
- $log->trace("contacted MPD less than 5 minutes ago. skipping alive check");
- }
- },
- )->start
- );
+method run_loop {
+ $self->connect_mpd;
+ $self->connect_db;
- $self->queue_songs;
+ $mpd->loop->add(
+ IO::Async::Timer::Periodic->new(
+ interval => 60,
+ on_tick => sub {
+ if ( time - $last_mpd_comm > 300 ) {
- for ( ;; ) {
- $log->debug("Waiting idle. PID=$$");
- $last_mpd_comm = time;
- $idler = $mpd->send("idle database playlist");
- my $result = $idler->get;
- undef $idler;
+ $log->trace(
+ "no active MPD communication for more that 5 minutes"
+ );
+ $log->trace("forcing alive check");
+ $self->break_idle;
+ }
+ else {
+ $log->trace("contacted MPD less than 5 minutes ago. skipping alive check");
+ }
+ },
+ )->start
+ );
- if ($result and $result->{changed}){
- my $changed = $result->{changed};
- $changed = [ $changed ] unless ref $changed;
+ $self->queue_songs;
- $mpd->emit($_) for @$changed;
- }
+ for ( ;; ) {
+ $log->debug("Waiting idle. PID=$$");
+ $last_mpd_comm = time;
+ $idler = $mpd->send("idle database playlist");
+ my $result = $idler->get;
+ undef $idler;
- $log->trace('got out of idle');
+ if ($result and $result->{changed}){
+ my $changed = $result->{changed};
+ $changed = [ $changed ] unless ref $changed;
- $self->handle_work_queue;
+ $mpd->emit($_) for @$changed;
}
+
+ $log->trace('got out of idle');
+
+ $self->handle_work_queue;
}
}
+1;