}
class Feeder {
+ has $cfg_file :reader;
has $opt :reader;
has $db;
has $db_generation;
use DBD::Pg;
use DBI;
use Log::Any qw($log);
+use IO::Async::Signal;
use Net::Async::MPD;
ADJUST {
- $opt = Options->new;
+ Getopt::Long::Configure('pass_through');
+ Getopt::Long::GetOptions('cfg|config=s' => \$cfg_file);
+ Getopt::Long::Configure('no_pass_through');
- {
- my $cfg_file;
- Getopt::Long::Configure('pass_through');
- Getopt::Long::GetOptions('cfg|config=s' => \$cfg_file);
- Getopt::Long::Configure('no_pass_through');
+ $cfg_file //= DEFAULT_CONFIG_FILE if -e DEFAULT_CONFIG_FILE;
- $cfg_file //= DEFAULT_CONFIG_FILE if -e DEFAULT_CONFIG_FILE;
+ $self->configure;
- $opt->parse_config_file($cfg_file) if $cfg_file;
- }
+ $db_needs_update = 0 if $opt->skip_db_update;
+ }
- $opt->parse_command_line;
+ method configure {
+ my $new_opt = Options->new;
- $db_needs_update = 0 if $opt->skip_db_update;
+ $new_opt->parse_config_file($cfg_file) if $cfg_file;
+
+ $new_opt->parse_command_line;
+
+ Log::Any::Adapter->set( Stderr => log_level => $new_opt->log_level );
- Log::Any::Adapter->set( Stderr => log_level => $opt->log_level );
+ $opt = $new_opt;
}
method connect_mpd {
$conn{port} = $opt->mpd_port if $opt->mpd_port;
$mpd = Net::Async::MPD->new(%conn);
+
+ $mpd->loop->add(
+ IO::Async::Signal->new(
+ name => 'HUP',
+ on_receipt => sub {
+ $log->debug("SIGHUP received. Stopping loop");
+ $mpd->loop->stop('reload');
+ },
+ )
+ );
}
method connect_db {
$self->prepare_to_wait_idle;
}
+
+ method stop {
+ undef $mpd;
+
+ if ($db) {
+ if ($db->{ActiveKids}) {
+ $log->warn("$db->{ActiveKids} active DB statements");
+ for my $st ( @{ $db->{ChildHandles} } ) {
+ next unless $st->{Active};
+ while(my($k,$v) = each %$st) {
+ $log->debug("$k = ".($v//'<NULL>'));
+ }
+ }
+ }
+
+ $db->disconnect;
+ undef $db;
+ }
+ }
}
my $feeder = Feeder->new();
}
}
-$feeder->queue_songs( undef, sub { $feeder->run } );
+for ( ;; ) {
+ $feeder->queue_songs( undef, sub { $feeder->run } );
-$feeder->mpd->loop->run;
+ $log->debug("Entering event loop. PID=$$");
+
+ my $result = $feeder->mpd->loop->run;
+ $log->trace( "Got loop result of " . ( $result // 'undef' ) );
+
+ if ('reload' eq $result) {
+ $log->notice("disconnecting");
+ $feeder->stop;
+
+ exec( "$0", '--config', $feeder->cfg_file, '--skip-db-update' );
+ }
+}