From c7496814dc7afd9a1ba42ab722cf43d0488518af Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 30 Jan 2024 09:30:38 +0100 Subject: [PATCH] Bug 35819: Adjust es_indexer_daemon Signed-off-by: Tomas Cohen Arazi --- misc/workers/es_indexer_daemon.pl | 44 +++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/misc/workers/es_indexer_daemon.pl b/misc/workers/es_indexer_daemon.pl index 44bcdbf9a53..2c5ae34a1b8 100755 --- a/misc/workers/es_indexer_daemon.pl +++ b/misc/workers/es_indexer_daemon.pl @@ -65,6 +65,10 @@ use Koha::SearchEngine::Indexer; my ( $help, $batch_size ); + +my $not_found_retries = {}; +my $max_retries = $ENV{MAX_RETRIES} || 10; + GetOptions( 'h|help' => \$help, 'b|batch_size=s' => \$batch_size @@ -116,22 +120,46 @@ while (1) { my $body = $frame->body; decode_json($body); # TODO Should this be from_json? Check utf8 flag. } catch { - $logger->warn(sprintf "Frame not processed - %s", $_); + Koha::Logger->get({ interface => 'worker' })->warn(sprintf "Frame not processed - %s", $_); return; - } finally { - $conn->ack( { frame => $frame } ); }; - next unless $args; + unless ( $args ) { + Koha::Logger->get({ interface => 'worker' })->warn(sprintf "Frame does not have correct args, ignoring it"); + $conn->nack( { frame => $frame, requeue => 'false' } ); + next; + } + + my $job = Koha::BackgroundJobs->find( $args->{job_id} ); - # FIXME This means we need to have create the DB entry before - # 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->search( { id => $args->{job_id}, status => 'new' } )->next; + if ( $job && $job->status ne 'new' ) { + Koha::Logger->get( { interface => 'worker' } ) + ->warn( sprintf "Job %s has wrong status %s", $args->{job_id}, $job->status ); + + # nack without requeue, we do not want to process this frame again + $conn->nack( { frame => $frame, requeue => 'false' } ); + next; + } unless ($job) { - $logger->warn( sprintf "Job %s not found, or has wrong status", $args->{job_id} ); + if ( ++$not_found_retries->{$args->{job_id}} >= $max_retries ) { + Koha::Logger->get( { interface => 'worker' } ) + ->warn( sprintf "Job %s not found, no more retry", $args->{job_id} ); + + # nack without requeue, we do not want to process this frame again + $conn->nack( { frame => $frame, requeue => 'false' } ); + next; + } + + Koha::Logger->get( { interface => 'worker' } ) + ->debug( sprintf "Job %s not found, will retry later", $args->{job_id} ); + + # nack to force requeue + $conn->nack( { frame => $frame, requeue => 'true' } ); + Time::HiRes::sleep(0.5); next; } + $conn->ack( { frame => $frame } ); push @jobs, $job; if ( @jobs >= $batch_size || !$conn->can_read( { timeout => '0.1' } ) ) { -- 2.43.0