From 4a27777b350218cfdc794e7390f5ae1b54e56c25 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Thu, 11 Nov 2021 10:57:20 +0000 Subject: [PATCH] more on-demand connections --- bin/mpd-feeder | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/bin/mpd-feeder b/bin/mpd-feeder index 5760c75..097a7a6 100755 --- a/bin/mpd-feeder +++ b/bin/mpd-feeder @@ -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' ) { -- 2.39.5