From e69cd83287a8a78d386e1019bea45a78a22a3cd2 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Thu, 11 Nov 2021 11:08:04 +0000 Subject: [PATCH] make database update a synchronous operation when someone calls for it, they rely that after the call to ->update_db returns the database is fresh there are tree callers - 'single', 'one-shot' and the database update notification --- bin/mpd-feeder | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/bin/mpd-feeder b/bin/mpd-feeder index 097a7a6..401e696 100755 --- a/bin/mpd-feeder +++ b/bin/mpd-feeder @@ -244,42 +244,38 @@ SQL $self->connect_mpd; $self->connect_db; - $mpd->send('listallinfo')->on_done( - sub { - try { - my $rows = shift; - $db->begin_work; + my $rows = $mpd->send('listallinfo')->get; + try { + $db->begin_work; - $db_generation++; + $db_generation++; - my $song_count; + my $song_count; - foreach my $entry (@$rows) { - next unless exists $entry->{file}; - $self->db_store_song( $entry->{file}, - $entry->{Artist}, $entry->{Album} ); - $song_count++; - } + foreach my $entry (@$rows) { + next unless exists $entry->{file}; + $self->db_store_song( $entry->{file}, + $entry->{Artist}, $entry->{Album} ); + $song_count++; + } - $log->info("Updated data about $song_count songs"); + $log->info("Updated data about $song_count songs"); - $self->db_remove_stale_entries; + $self->db_remove_stale_entries; - $self->db_set_option( generation => $db_generation ); + $self->db_set_option( generation => $db_generation ); - $db->commit; - } - catch { - my $err = $@; + $db->commit; + } + catch { + my $err = $@; - $db_generation--; + $db_generation--; - $db->rollback; + $db->rollback; - die $err; - } - } - ); + die $err; + } } method db_find_suitable_songs($num) { -- 2.39.5