1 package App::MPD::Feeder;
8 use App::MPD::Feeder::Options;
9 use App::MPD::Feeder::DB;
13 use IO::Async::Signal;
14 use Log::Any qw($log);
17 use Syntax::Keyword::Try;
20 class App::MPD::Feeder {
21 has $cfg_file :reader;
24 has $db_needs_update = 1;
27 use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf';
30 Getopt::Long::Configure('pass_through');
31 Getopt::Long::GetOptions('cfg|config=s' => \$cfg_file);
32 Getopt::Long::Configure('no_pass_through');
34 $cfg_file //= DEFAULT_CONFIG_FILE if -e DEFAULT_CONFIG_FILE;
38 $db_needs_update = 0 if $opt->skip_db_update;
42 my $new_opt = App::MPD::Feeder::Options->new;
44 $new_opt->parse_config_file($cfg_file) if $cfg_file;
46 $new_opt->parse_command_line;
48 Log::Any::Adapter->set( Stderr => log_level => $new_opt->log_level );
52 $db = App::MPD::Feeder::DB->new( opt => $opt );
58 my %conn = ( auto_connect => 1 );
59 $conn{host} = $opt->mpd_host if $opt->mpd_host;
60 $conn{port} = $opt->mpd_port if $opt->mpd_port;
62 $mpd = Net::Async::MPD->new(%conn);
64 my $int_signal_handler = sub {
65 state $signal_count = 0;
67 $log->debug("Signal received. Stopping loop");
68 $mpd->loop->stop('quit');
70 if ( $signal_count > 1 ) {
71 $log->warn("Another signal received (#$signal_count)");
72 $log->warn("Exiting abruptly");
79 IO::Async::Signal->new(
81 on_receipt => $int_signal_handler,
87 IO::Async::Signal->new(
90 $log->debug("SIGHUP received. Scheduling reload");
91 $mpd->loop->stop('reload');
97 IO::Async::Signal->new(
100 $log->debug("SIGUSR1 received. Dumping configuration to STDERR");
101 my $old = select \*STDERR;
118 method update_db($force = undef) {
119 if (!$db_needs_update and !$force) {
120 $log->debug("Skipping DB update");
124 $log->info('Updating song database');
127 my $rows = $mpd->send('listallinfo')->get;
133 foreach my $entry (@$rows) {
134 next unless exists $entry->{file};
136 $self->db->store_song( $entry->{file},
137 $entry->{AlbumArtist} // $entry->{Artist},
142 $log->info("Updated data about $song_count songs");
144 $self->db->remove_stale_entries;
146 $self->db->finish_update;
148 $db_needs_update = 0;
152 $self->db->cancel_update;
157 method queue_songs($num = undef, $callback = undef) {
161 $mpd->send('playlist')->on_done(
163 my $present = scalar @{ $_[0] };
165 $log->notice( "Playlist contains $present songs. Wanted: "
166 . $opt->target_queue_length );
167 if ( $present < $opt->target_queue_length ) {
169 $opt->target_queue_length - $present, $callback );
172 $callback->() if $callback;
180 my @list = $self->db->find_suitable_songs($num);
182 die "Found no suitable songs" unless @list;
184 if ( @list < $num ) {
187 'Found only %d suitable songs instead of %d',
193 $log->info("About to add $num songs to the playlist");
196 for my $song (@list) {
197 my $path = $song->{song};
202 $log->debug( "Adding " . join( ', ', map {"«$_»"} @paths ) );
203 # MPD needs raw bytes
204 utf8::encode($_) for @paths;
207 push @commands, [ add => "\"$_\"" ];
210 my $f = $mpd->send( \@commands );
211 $f->on_fail( sub { die @_ } );
214 $self->db->note_song_qeued($_) for @list;
215 $callback->(@_) if $callback;
220 method prepare_to_wait_idle {
221 $log->trace('declaring idle mode');
222 $mpd->send('idle database playlist')->on_done(
226 if ( $result->{changed} eq 'database' ) {
227 $db_needs_update = 1;
228 $self->prepare_to_wait_idle;
230 elsif ( $result->{changed} eq 'playlist' ) {
231 $self->queue_songs( undef,
232 sub { $self->prepare_to_wait_idle } );
237 "Unknown result from idle: " . to_json($result) );
238 $self->prepare_to_wait_idle;
247 die "Connection to MPD lost";
251 $self->prepare_to_wait_idle;
264 $self->queue_songs( undef, sub { $self->run } );
266 $log->debug("Entering event loop. PID=$$");
268 my $result = $mpd->loop->run;
269 $log->trace( "Got loop result of " . ( $result // 'undef' ) );
271 if ( 'reload' eq $result ) {
272 $log->notice("disconnecting");
275 my @exec = ( $0, '--config', $self->cfg_file, '--skip-db-update' );
276 if ( $log->is_trace ) {
278 . join( ' ', map { /\s/ ? "'$_'" : $_ } @exec ) );
283 if ( 'quit' eq $result ) {
284 $log->trace("quitting because of 'quit' loop result");