From fd8fbe24fc3dc2af8488d42eff4034149755c050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nyl=C3=A9n?= Date: Mon, 26 Sep 2022 10:08:08 +0200 Subject: [PATCH] Requeue bg-job in if not found in db. Job may be available through RabbitMQ sooner than db. Only try to requeue 100 times to avoid infinite loop --- misc/background_jobs_worker.pl | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/misc/background_jobs_worker.pl b/misc/background_jobs_worker.pl index eb36f34f96..78af3a1895 100755 --- a/misc/background_jobs_worker.pl +++ b/misc/background_jobs_worker.pl @@ -48,7 +48,7 @@ The different values available are: =cut use Modern::Perl; -use JSON qw( decode_json ); +use JSON qw( decode_json encode_json ); use Try::Tiny qw( catch try ); use Pod::Usage; use Getopt::Long; @@ -73,7 +73,6 @@ try { } catch { warn sprintf "Cannot connect to the message broker, the jobs will be processed anyway (%s)", $_; }; - if ( $conn ) { # FIXME cf note in Koha::BackgroundJob about $namespace my $namespace = C4::Context->config('memcached_namespace'); @@ -96,8 +95,19 @@ while (1) { # It could work in a first step, but then we will want to handle job that will be created from the message received my $job = Koha::BackgroundJobs->find($args->{job_id}); - process_job( $job, $args ); - $conn->ack( { frame => $frame } ); # FIXME depending on success? + if ( $job ) { + process_job( $job, $args ); + $conn->ack( { frame => $frame } ); # FIXME depending on success? + } else { + # FIXME if job not found in db, ack this job and requeue as new to retry later + $conn->ack( { frame => $frame } ); + $args->{retries} = exists $args->{retries} ? $args->{retries} + 1 : 1; + if ( $args->{retries} < 100 ) { + $conn->send( { destination => $frame->destination , body => encode_json($args) } ); + } else { + warn "too many attempts, job id: " . $args->{job_id} ; + } + } } else { my $jobs = Koha::BackgroundJobs->search({ status => 'new', queue => \@queues }); -- 2.34.1