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(
119 $log->debug("SIGUSR1 received. Dumping configuration to STDERR");
120 my $old = select \*STDERR;
137 method update_db($force = undef) {
138 if (!$db_needs_update and !$force) {
139 $log->debug("Skipping DB update");
143 $log->info('Updating song database');
146 my $rows = $mpd->send('listallinfo')->get;
148 $log->trace('got all songs from MPD');
154 foreach my $entry (@$rows) {
155 next unless exists $entry->{file};
157 $self->db->store_song( $entry->{file},
158 $entry->{AlbumArtist} // $entry->{Artist},
164 my ($total_songs, $total_artists, $total_albums,
165 $new_songs, $new_artists, $new_albums
166 ) = $self->db->finish_update;
169 "Updated data about $song_count songs (including $new_songs new), "
170 . "$total_artists artists (including $new_artists new) "
172 . "and $total_albums albums (including $new_albums new)"
175 $db_needs_update = 0;
179 $self->db->cancel_update;
184 method queue_songs($num = undef) {
188 $log->trace("Requesting playlist");
189 my $present = $mpd->send('playlist')->get // [];
190 $present = scalar(@$present);
192 $log->notice( "Playlist contains $present songs. Wanted: "
193 . $opt->target_queue_length );
194 if ( $present < $opt->target_queue_length ) {
196 $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 ) );
225 # MPD needs raw bytes
226 utf8::encode($_) for @paths;
229 push @commands, [ add => "\"$_\"" ];
232 my $f = $mpd->send( \@commands );
233 $f->on_fail( sub { die @_ } );
236 $self->db->note_song_qeued($_) for @list;
248 method handle_work_queue {
249 while ( my $item = $work_queue->next ) {
250 if ( $item eq 'playlist' ) {
253 elsif ( $item eq 'database' ) {
254 $db_needs_update = 1;
257 elsif ( $item eq 'reload' ) {
258 $log->notice("disconnecting and re-starting");
262 ( $0, '--config', $self->cfg_file, '--skip-db-update' );
263 if ( $log->is_trace ) {
265 . 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"
304 $log->trace("forcing alive check");
308 $log->trace("contacted MPD less than 5 minutes ago. skipping alive check");
317 $log->debug("Waiting idle. PID=$$");
318 $last_mpd_comm = time;
319 $idler = $mpd->send("idle database playlist");
320 my $result = $idler->get;
323 if ($result and $result->{changed}){
324 my $changed = $result->{changed};
325 $changed = [ $changed ] unless ref $changed;
327 $mpd->emit($_) for @$changed;
330 $log->trace('got out of idle');
332 $self->handle_work_queue;