summaryrefslogtreecommitdiff
path: root/lib/Multi
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2014-10-28 10:55:23 +0100
committerYorhel <git@yorhel.nl>2014-10-28 10:55:23 +0100
commitce225c8af3b29069f9b17dc3279981893f1d7cc5 (patch)
treeaac923e9cc5f12fe8619a14e78d552f586135674 /lib/Multi
parent6619ab4d30627be5da3591b1c1fe81b684d1d8c8 (diff)
Multi: More convenient error handling for one-shot queries
Diffstat (limited to 'lib/Multi')
-rw-r--r--lib/Multi/Anime.pm6
-rw-r--r--lib/Multi/Core.pm24
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]);
},
);
}