4 class App::MPD::Feeder;
6 use App::MPD::Feeder::DB;
7 use App::MPD::Feeder::Options;
8 use App::MPD::Feeder::WorkQueue;
12 use IO::Async::Signal;
13 use IO::Async::Timer::Periodic;
14 use Log::Any qw($log);
17 use Syntax::Keyword::Try;
19 has $cfg_file :reader;
22 has $db_needs_update :writer = 1;
25 has $work_queue = App::MPD::Feeder::WorkQueue->new;
28 use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf';
31 Getopt::Long::Configure('pass_through');
32 Getopt::Long::GetOptions( 'cfg|config=s' => \$cfg_file );
33 Getopt::Long::Configure('no_pass_through');
35 $cfg_file //= DEFAULT_CONFIG_FILE if -e DEFAULT_CONFIG_FILE;
39 $db_needs_update = 0 if $opt->skip_db_update;
43 my $new_opt = App::MPD::Feeder::Options->new;
45 $new_opt->parse_config_file($cfg_file) if $cfg_file;
47 $new_opt->parse_command_line;
49 Log::Any::Adapter->set( Stderr => log_level => $new_opt->log_level );
53 $db = App::MPD::Feeder::DB->new( opt => $opt );
59 my %conn = ( auto_connect => 1 );
60 $conn{host} = $opt->mpd_host if $opt->mpd_host;
61 $conn{port} = $opt->mpd_port if $opt->mpd_port;
63 $mpd = Net::Async::MPD->new(%conn);
67 die "Connection to MPD lost";
72 $work_queue->add('playlist');
77 $work_queue->add('database');
81 my $int_signal_handler = sub {
82 state $signal_count = 0;
84 $log->debug("Signal received. Stopping loop");
85 $work_queue->add('quit');
88 if ( $signal_count > 1 ) {
89 $log->warn("Another signal received (#$signal_count)");
90 $log->warn("Exiting abruptly");
97 IO::Async::Signal->new(
99 on_receipt => $int_signal_handler,
105 IO::Async::Signal->new(
108 $log->debug("SIGHUP received. Scheduling reload");
109 $work_queue->add('reload');
116 IO::Async::Signal->new(
120 "SIGUSR1 received. Dumping configuration to STDERR");
121 my $old = select \*STDERR;
138 method update_db( $force = undef ) {
139 if ( !$db_needs_update and !$force ) {
140 $log->debug("Skipping DB update");
144 $log->info('Updating song database');
147 my $rows = $mpd->send('listallinfo')->get;
149 $log->trace('got all songs from MPD');
155 foreach my $entry (@$rows) {
156 next unless exists $entry->{file};
158 $self->db->store_song( $entry->{file},
159 $entry->{AlbumArtist} // $entry->{Artist},
165 my ($total_songs, $total_artists, $total_albums,
166 $new_songs, $new_artists, $new_albums
167 ) = $self->db->finish_update;
170 "Updated data about $song_count songs (including $new_songs new), "
171 . "$total_artists artists (including $new_artists new) "
173 . "and $total_albums albums (including $new_albums new)"
176 $db_needs_update = 0;
180 $self->db->cancel_update;
185 method queue_songs( $num = undef ) {
187 if ( !defined $num ) {
189 $log->trace("Requesting playlist");
190 my $present = $mpd->send('playlist')->get // [];
191 $present = scalar(@$present);
193 $log->notice( "Playlist contains $present songs. Wanted: "
194 . $opt->target_queue_length );
195 if ( $present < $opt->target_queue_length ) {
196 $self->queue_songs( $opt->target_queue_length - $present );
202 my @list = $self->db->find_suitable_songs($num);
204 die "Found no suitable songs" unless @list;
206 if ( @list < $num ) {
209 'Found only %d suitable songs instead of %d',
215 $log->info("About to add $num songs to the playlist");
218 for my $song (@list) {
219 my $path = $song->{song};
224 $log->debug( "Adding " . join( ', ', map {"«$_»"} @paths ) );
226 # MPD needs raw bytes
227 utf8::encode($_) for @paths;
230 push @commands, [ add => "\"$_\"" ];
233 my $f = $mpd->send( \@commands );
234 $f->on_fail( sub { die @_ } );
237 $self->db->note_song_qeued($_) for @list;
249 method handle_work_queue {
250 while ( my $item = $work_queue->next ) {
251 if ( $item eq 'playlist' ) {
254 elsif ( $item eq 'database' ) {
255 $db_needs_update = 1;
258 elsif ( $item eq 'reload' ) {
259 $log->notice("disconnecting and re-starting");
262 my @exec = ( $0, '--config', $self->cfg_file, '--skip-db-update' );
263 if ( $log->is_trace ) {
265 'exec ' . join( ' ', map { /\s/ ? "'$_'" : $_ } @exec ) );
269 elsif ( $item eq 'quit' ) {
270 $log->trace("quitting");
275 die "Unknown work queue item '$item'";
281 if ( $idler && !$idler->is_ready ) {
282 $log->trace("hand-sending 'noidle'");
284 $mpd->{mpd_handle}->write("noidle\n");
287 $log->trace("no idler found");
296 IO::Async::Timer::Periodic->new(
299 if ( time - $last_mpd_comm > 300 ) {
302 "no active MPD communication for more that 5 minutes");
303 $log->trace("forcing alive check");
308 "contacted MPD less than 5 minutes ago. skipping alive check"
318 $log->debug("Waiting idle. PID=$$");
319 $last_mpd_comm = time;
320 $idler = $mpd->send("idle database playlist");
321 my $result = $idler->get;
324 if ( $result and $result->{changed} ) {
325 my $changed = $result->{changed};
326 $changed = [$changed] unless ref $changed;
328 $mpd->emit($_) for @$changed;
331 $log->trace('got out of idle');
333 $self->handle_work_queue;