1 package App::MPD::Feeder;
8 use App::MPD::Feeder::DB;
9 use App::MPD::Feeder::Options;
10 use App::MPD::Feeder::WorkQueue;
14 use IO::Async::Signal;
15 use IO::Async::Timer::Periodic;
16 use Log::Any qw($log);
19 use Syntax::Keyword::Try;
21 class App::MPD::Feeder {
22 has $cfg_file :reader;
25 has $db_needs_update :writer = 1;
28 has $work_queue = App::MPD::Feeder::WorkQueue->new;
31 use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf';
34 Getopt::Long::Configure('pass_through');
35 Getopt::Long::GetOptions('cfg|config=s' => \$cfg_file);
36 Getopt::Long::Configure('no_pass_through');
38 $cfg_file //= DEFAULT_CONFIG_FILE if -e DEFAULT_CONFIG_FILE;
42 $db_needs_update = 0 if $opt->skip_db_update;
46 my $new_opt = App::MPD::Feeder::Options->new;
48 $new_opt->parse_config_file($cfg_file) if $cfg_file;
50 $new_opt->parse_command_line;
52 Log::Any::Adapter->set( Stderr => log_level => $new_opt->log_level );
56 $db = App::MPD::Feeder::DB->new( opt => $opt );
62 my %conn = ( auto_connect => 1 );
63 $conn{host} = $opt->mpd_host if $opt->mpd_host;
64 $conn{port} = $opt->mpd_port if $opt->mpd_port;
66 $mpd = Net::Async::MPD->new(%conn);
70 die "Connection to MPD lost";
75 $work_queue->add('playlist');
80 $work_queue->add('database');
84 my $int_signal_handler = sub {
85 state $signal_count = 0;
87 $log->debug("Signal received. Stopping loop");
88 $work_queue->add('quit');
91 if ( $signal_count > 1 ) {
92 $log->warn("Another signal received (#$signal_count)");
93 $log->warn("Exiting abruptly");
100 IO::Async::Signal->new(
102 on_receipt => $int_signal_handler,
108 IO::Async::Signal->new(
111 $log->debug("SIGHUP received. Scheduling reload");
112 $work_queue->add('reload');
119 IO::Async::Signal->new(
122 $log->debug("SIGUSR1 received. Dumping configuration to STDERR");
123 my $old = select \*STDERR;
140 method update_db($force = undef) {
141 if (!$db_needs_update and !$force) {
142 $log->debug("Skipping DB update");
146 $log->info('Updating song database');
149 my $rows = $mpd->send('listallinfo')->get;
151 $log->trace('got all songs from MPD');
157 foreach my $entry (@$rows) {
158 next unless exists $entry->{file};
160 $self->db->store_song( $entry->{file},
161 $entry->{AlbumArtist} // $entry->{Artist},
167 my ($total_songs, $total_artists, $total_albums,
168 $new_songs, $new_artists, $new_albums
169 ) = $self->db->finish_update;
172 "Updated data about $song_count songs (including $new_songs new), "
173 . "$total_artists artists (including $new_artists new) "
175 . "and $total_albums albums (including $new_albums new)"
178 $db_needs_update = 0;
182 $self->db->cancel_update;
187 method queue_songs($num = undef) {
191 $log->trace("Requesting playlist");
192 my $present = $mpd->send('playlist')->get // [];
193 $present = scalar(@$present);
195 $log->notice( "Playlist contains $present songs. Wanted: "
196 . $opt->target_queue_length );
197 if ( $present < $opt->target_queue_length ) {
199 $opt->target_queue_length - $present );
205 my @list = $self->db->find_suitable_songs($num);
207 die "Found no suitable songs" unless @list;
209 if ( @list < $num ) {
212 'Found only %d suitable songs instead of %d',
218 $log->info("About to add $num songs to the playlist");
221 for my $song (@list) {
222 my $path = $song->{song};
227 $log->debug( "Adding " . join( ', ', map {"«$_»"} @paths ) );
228 # MPD needs raw bytes
229 utf8::encode($_) for @paths;
232 push @commands, [ add => "\"$_\"" ];
235 my $f = $mpd->send( \@commands );
236 $f->on_fail( sub { die @_ } );
239 $self->db->note_song_qeued($_) for @list;
251 method handle_work_queue {
252 while ( my $item = $work_queue->next ) {
253 if ( $item eq 'playlist' ) {
256 elsif ( $item eq 'database' ) {
257 $db_needs_update = 1;
260 elsif ( $item eq 'reload' ) {
261 $log->notice("disconnecting and re-starting");
265 ( $0, '--config', $self->cfg_file, '--skip-db-update' );
266 if ( $log->is_trace ) {
268 . join( ' ', map { /\s/ ? "'$_'" : $_ } @exec ) );
272 elsif ( $item eq 'quit' ) {
273 $log->trace("quitting");
278 die "Unknown work queue item '$item'";
284 if ($idler && !$idler->is_ready) {
285 $log->trace("hand-sending 'noidle'");
287 $mpd->{mpd_handle}->write("noidle\n");;
290 $log->trace("no idler found");
299 IO::Async::Timer::Periodic->new(
302 if ( time - $last_mpd_comm > 300 ) {
305 "no active MPD communication for more that 5 minutes"
307 $log->trace("forcing alive check");
311 $log->trace("contacted MPD less than 5 minutes ago. skipping alive check");
320 $log->debug("Waiting idle. PID=$$");
321 $last_mpd_comm = time;
322 $idler = $mpd->send("idle database playlist");
323 my $result = $idler->get;
326 if ($result and $result->{changed}){
327 my $changed = $result->{changed};
328 $changed = [ $changed ] unless ref $changed;
330 $mpd->emit($_) for @$changed;
333 $log->trace('got out of idle');
335 $self->handle_work_queue;