]> git.ktnx.net Git - mpd-feeder.git/commitdiff
more on-demand connections
authorDamyan Ivanov <dmn@debian.org>
Thu, 11 Nov 2021 10:57:20 +0000 (10:57 +0000)
committerDamyan Ivanov <dmn@debian.org>
Thu, 11 Nov 2021 10:57:20 +0000 (10:57 +0000)
bin/mpd-feeder

index 5760c751633483dd6f02fe95fe908ae6876e42c1..097a7a654f3110d2ca1048540285efc5241932eb 100755 (executable)
@@ -113,6 +113,7 @@ class Feeder {
     has $opt :reader;
     has $db;
     has $db_generation;
+    has $db_needs_update = 1;
     has $mpd :reader;
 
 use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf';
@@ -138,6 +139,8 @@ use Net::Async::MPD;
 
         $opt->parse_command_line;
 
+        $db_needs_update = 0 if $opt->skip_db_update;
+
         Log::Any::Adapter->set( Stderr => log_level => $opt->log_level );
     }
 
@@ -161,7 +164,7 @@ use Net::Async::MPD;
         $log->info( "Connected to database " . $opt->db_path );
         $db_generation = $self->db_get_option('generation');
         $log->debug("DB generation is $db_generation");
-        $self->update_db unless $opt->skip_db_update;
+        $self->update_db;
     }
 
     method db_get_option($name) {
@@ -231,8 +234,16 @@ SQL
         )->execute( $item->{artist}, $item->{album} );
     }
 
-    method update_db() {
+    method update_db($force = undef) {
+        if (!$db_needs_update and !$force) {
+            $log->debug("Skipping DB update");
+            return;
+        }
+
         $log->info('Updating song database');
+        $self->connect_mpd;
+        $self->connect_db;
+
         $mpd->send('listallinfo')->on_done(
             sub {
                 try {
@@ -303,8 +314,8 @@ SQL
     }
 
     method queue_songs($num = undef, $callback = undef) {
-        $self->connect_mpd;
         if (!defined $num) {
+            $self->connect_mpd;
             $mpd->send('playlist')->on_done(
                 sub {
                     my $present = scalar @{ $_[0] };
@@ -350,6 +361,7 @@ SQL
         for (@paths) {
             push @commands, [ add => "\"$_\"" ];
         }
+        $self->connect_mpd;
         my $f = $mpd->send( \@commands );
         warn "here";
         $f->on_fail( sub { die @_ } );
@@ -373,7 +385,7 @@ SQL
                 use JSON; warn to_json($result);
 
                 if ( $result->{changed} eq 'database' ) {
-                    $self->update_db;
+                    $self->update_db(1);
                     $self->prepare_to_wait_idle;
                 }
                 elsif ( $result->{changed} eq 'playlist' ) {