From ce225c8af3b29069f9b17dc3279981893f1d7cc5 Mon Sep 17 00:00:00 2001 From: Yorhel Date: Tue, 28 Oct 2014 10:55:23 +0100 Subject: Multi: More convenient error handling for one-shot queries --- lib/Multi/Anime.pm | 6 ++---- lib/Multi/Core.pm | 24 ++++++++++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/Multi/Anime.pm b/lib/Multi/Anime.pm index 13d3b6f1..17b9a3b0 100644 --- a/lib/Multi/Anime.pm +++ b/lib/Multi/Anime.pm @@ -205,8 +205,7 @@ sub handlemsg { # we now know something about the anime we requested, update DB elsif($code == NO_SUCH_ANIME) { AE::log info => "No anime found with id = $C{aid}"; - pg_cmd 'UPDATE anime SET lastfetch = NOW() WHERE id = ?', - [ $C{aid} ], sub { pg_expect $_[0], 0 }; + pg_cmd 'UPDATE anime SET lastfetch = NOW() WHERE id = ?', [ $C{aid} ]; $f = \&check_anime; $C{aid} = 0; @@ -240,8 +239,7 @@ sub update_anime { SET id = $1, ann_id = $2, nfo_id = $3, year = $4, type = $5, title_romaji = $6, title_kanji = $7, lastfetch = NOW() WHERE id = $8', - [ @col, $C{aid} ], - sub { pg_expect $_[0], 0 }; + [ @col, $C{aid} ]; AE::log info => "Fetched anime info for a$C{aid}"; AE::log warn => "a$C{aid} doesn't have a title or year!" if !$col[3] || !$col[5]; diff --git a/lib/Multi/Core.pm b/lib/Multi/Core.pm index 478b6b28..5340abf7 100644 --- a/lib/Multi/Core.pm +++ b/lib/Multi/Core.pm @@ -121,9 +121,13 @@ sub run { die "PID file already exists\n" if -e $pidfile; $stopcv = AE::cv; - AnyEvent::Log::ctx('Multi')->attach( - AnyEvent::Log::Ctx->new(log_to_file => "$VNDB::M{log_dir}/multi.log", level => 'trace') - ); + AnyEvent::Log::ctx('Multi')->attach(AnyEvent::Log::Ctx->new(level => 'trace', log_to_file => $VNDB::M{log_dir}.'/multi.log')); + #log_cb => sub { + # open(my $F, '>>:utf8', $VNDB::M{log_dir}.'/multi.log'); + # print $F $_[0]; + # close $F; + # } + #)); $AnyEvent::Log::FILTER->level('fatal'); daemon_init; @@ -169,29 +173,37 @@ sub pg_expect { # The sub will be called on either on_error or on_done, and has two args: The # result and the running time. Only a single on_result is expected. The result # argument is undef on error. +# If no sub is provided or the sub argument is a string, a default sub will be +# used that just calls pg_expect and logs any errors. # Unlike most AE watchers, this function does not return a watcher object and # can not be cancelled. sub pg_cmd { my($q, $a, $s) = @_; my $r; + + my $sub = !$s || !ref $s ? do { + my $loc = sprintf '%s:%d%s', (caller)[0,2], $s ? ":$s" : ''; + sub { pg_expect $_[0], undef, $loc } + } : $s; + my $w; $w = pg->push_query( query => $q, $a ? (args => $a) : (), on_error => sub { undef $w; - $s->(undef, 0); + $sub->(undef, 0); }, on_result => sub { if($r) { AE::log warn => "Received more than one result for query: $q"; - $s->(undef, 0); + $sub->(undef, 0); } else { $r = $_[2]; } }, on_done => sub { undef $w; - $s->($r, $_[2]); + $sub->($r, $_[2]); }, ); } -- cgit v1.2.3