From cbb7568af929af12fd8cd3dca708427068c40114 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 16 Jan 2024 16:36:05 +0100 Subject: [PATCH] Bug 35819: Retry to fetch job if does not enqueued yet If a job has been enqueued in the middle of a txn, the worker will fetch the job but fail because it's not in the DB yet. The uglier but most effective (as a quick fix and easily backportable fix) is to... wait a bit. This patch suggests to sleep 1sec and retry, 10 times. It should be enough for the majority of the jobs. --- misc/workers/background_jobs_worker.pl | 7 ++++++- misc/workers/es_indexer_daemon.pl | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/misc/workers/background_jobs_worker.pl b/misc/workers/background_jobs_worker.pl index fde3432143a..29647dd3cb4 100755 --- a/misc/workers/background_jobs_worker.pl +++ b/misc/workers/background_jobs_worker.pl @@ -129,7 +129,12 @@ while (1) { # 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; + my ( $job, $i ); + while ( !$job ) { + $job = Koha::BackgroundJobs->search( { id => $args->{job_id}, status => 'new' } )->next; + sleep 1 unless $job; + last if ++$i >= 10; + } unless( $job ) { Koha::Logger->get( { interface => 'worker' } ) diff --git a/misc/workers/es_indexer_daemon.pl b/misc/workers/es_indexer_daemon.pl index 44bcdbf9a53..800f85ec2f4 100755 --- a/misc/workers/es_indexer_daemon.pl +++ b/misc/workers/es_indexer_daemon.pl @@ -126,7 +126,12 @@ while (1) { # 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; + my ( $job, $i ); + while ( !$job ) { + $job = Koha::BackgroundJobs->search( { id => $args->{job_id}, status => 'new' } )->next; + sleep 1 unless $job; + last if ++$i >= 10; + } unless ($job) { $logger->warn( sprintf "Job %s not found, or has wrong status", $args->{job_id} ); -- 2.34.1