1 package App::MPD::Feeder;
7 use App::MPD::Feeder::Options;
8 use App::MPD::Feeder::DB;
12 use IO::Async::Signal;
13 use Log::Any qw($log);
16 use Syntax::Keyword::Try;
19 class App::MPD::Feeder {
20 has $cfg_file :reader;
23 has $db_needs_update = 1;
26 use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf';
29 Getopt::Long::Configure('pass_through');
30 Getopt::Long::GetOptions('cfg|config=s' => \$cfg_file);
31 Getopt::Long::Configure('no_pass_through');
33 $cfg_file //= DEFAULT_CONFIG_FILE if -e DEFAULT_CONFIG_FILE;
37 $db_needs_update = 0 if $opt->skip_db_update;
41 my $new_opt = App::MPD::Feeder::Options->new;
43 $new_opt->parse_config_file($cfg_file) if $cfg_file;
45 $new_opt->parse_command_line;
47 Log::Any::Adapter->set( Stderr => log_level => $new_opt->log_level );
51 $db = App::MPD::Feeder::DB->new( opt => $opt );
57 my %conn = ( auto_connect => 1 );
58 $conn{host} = $opt->mpd_host if $opt->mpd_host;
59 $conn{port} = $opt->mpd_port if $opt->mpd_port;
61 $mpd = Net::Async::MPD->new(%conn);
64 IO::Async::Signal->new(
67 $log->debug("SIGTERM received. Stopping loop");
68 $mpd->loop->stop('quit');
74 IO::Async::Signal->new(
77 $log->debug("SIGINT received. Stopping loop");
78 $mpd->loop->stop('quit');
84 IO::Async::Signal->new(
87 $log->debug("SIGHUP received. Stopping loop");
88 $mpd->loop->stop('reload');
94 IO::Async::Signal->new(
97 $log->debug("SIGUSR1 received. Dumping configuration to STDERR");
98 my $old = select \*STDERR;
115 method update_db($force = undef) {
116 if (!$db_needs_update and !$force) {
117 $log->debug("Skipping DB update");
121 $log->info('Updating song database');
124 my $rows = $mpd->send('listallinfo')->get;
130 foreach my $entry (@$rows) {
131 next unless exists $entry->{file};
133 $self->db->store_song( $entry->{file},
134 $entry->{AlbumArtist} // $entry->{Artist},
139 $log->info("Updated data about $song_count songs");
141 $self->db->remove_stale_entries;
143 $self->db->finish_update;
145 $db_needs_update = 0;
149 $self->db->cancel_update;
154 method queue_songs($num = undef, $callback = undef) {
158 $mpd->send('playlist')->on_done(
160 my $present = scalar @{ $_[0] };
162 $log->notice( "Playlist contains $present songs. Wanted: "
163 . $opt->target_queue_length );
164 if ( $present < $opt->target_queue_length ) {
166 $opt->target_queue_length - $present, $callback );
169 $callback->() if $callback;
177 my @list = $self->db->find_suitable_songs($num);
179 die "Found no suitable songs" unless @list;
181 if ( @list < $num ) {
184 'Found only %d suitable songs instead of %d',
190 $log->info("About to add $num songs to the playlist");
193 for my $song (@list) {
194 my $path = $song->{song};
199 $log->debug( "Adding " . join( ', ', map {"«$_»"} @paths ) );
200 # MPD needs raw bytes
201 utf8::encode($_) for @paths;
204 push @commands, [ add => "\"$_\"" ];
207 my $f = $mpd->send( \@commands );
208 $f->on_fail( sub { die @_ } );
211 $self->db->note_song_qeued($_) for @list;
212 $callback->(@_) if $callback;
217 method prepare_to_wait_idle {
218 $log->trace('declaring idle mode');
219 $mpd->send('idle database playlist')->on_done(
223 if ( $result->{changed} eq 'database' ) {
224 $db_needs_update = 1;
225 $self->prepare_to_wait_idle;
227 elsif ( $result->{changed} eq 'playlist' ) {
228 $self->queue_songs( undef,
229 sub { $self->prepare_to_wait_idle } );
234 "Unknown result from idle: " . to_json($result) );
235 $self->prepare_to_wait_idle;
244 die "Connection to MPD lost";
248 $self->prepare_to_wait_idle;
261 $self->queue_songs( undef, sub { $self->run } );
263 $log->debug("Entering event loop. PID=$$");
265 my $result = $mpd->loop->run;
266 $log->trace( "Got loop result of " . ( $result // 'undef' ) );
268 if ( 'reload' eq $result ) {
269 $log->notice("disconnecting");
272 my @exec = ( $0, '--config', $self->cfg_file, '--skip-db-update' );
273 if ( $log->is_trace ) {
275 . join( ' ', map { /\s/ ? "'$_'" : $_ } @exec ) );
280 if ( 'quit' eq $result ) {
281 $log->trace("quitting because of 'quit' loop result");