]> git.ktnx.net Git - mpd-feeder.git/commitdiff
make database update a synchronous operation
authorDamyan Ivanov <dmn@debian.org>
Thu, 11 Nov 2021 11:08:04 +0000 (11:08 +0000)
committerDamyan Ivanov <dmn@debian.org>
Thu, 11 Nov 2021 11:08:04 +0000 (11:08 +0000)
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

index 097a7a654f3110d2ca1048540285efc5241932eb..401e696d19700dc9836b1f7bcff49e689f8d2219 100755 (executable)
@@ -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) {