From d7a552bd9cecc5e72ce96bdd96692f1918d5b992 Mon Sep 17 00:00:00 2001 From: David Cook Date: Fri, 7 Mar 2025 01:43:25 +0000 Subject: [PATCH] Bug 34070: Add error handling for ERROR frames for background worker This change adds error handling for ERROR frames to background_jobs_worker.pl. A previous patch added it to es_indexer_daemon.pl which is a different background jobs worker. To test ERROR handling for background_jobs_worker.pl: 1. Change your RabbitMQ connection details in koha-conf.xml or the hard-coded ones in Koha/BackgroundJob.pm to include an invalid password. For example, if you don't have a message_broker already defined, use the following: bad 2. sudo koha-worker --restart --quiet kohadev 3. View the errors in the logs: tail -f /var/log/koha/kohadev/worker-output.log tail -f /var/log/koha/kohadev/worker-error.log To test MESSAGE handling for background_jobs_worker.pl: 1. Ensure RabbitMQ connection details are correct 2. Go to Batch item modification and add the following barcodes: 39999000001310 39999000004571 3. Add a z - Public Note of the following and click Save: THEBACKGROUNDWORKERWORKED 4. Search the catalogue using THEBACKGROUNDWORKERWORKED in the top catalogue search 5. Note that you get 2 results in your search 6. Look in the logs just to make sure there are no new messages there Signed-off-by: Jonathan Druart --- misc/workers/background_jobs_worker.pl | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/misc/workers/background_jobs_worker.pl b/misc/workers/background_jobs_worker.pl index f070c3cf642..5958f70fd2f 100755 --- a/misc/workers/background_jobs_worker.pl +++ b/misc/workers/background_jobs_worker.pl @@ -124,8 +124,27 @@ while (1) { } my $args = try { - my $body = $frame->body; - decode_json($body); # TODO Should this be from_json? Check utf8 flag. + my $command = $frame->command; + my $body = $frame->body; + my $content_type = $frame->content_type; + if ( $command && $command eq 'MESSAGE' ) { + if ( $content_type && $content_type eq 'application/json' ) { + decode_json($body); # TODO Should this be from_json? Check utf8 flag. + } else { + + #TODO: This is a fallback for older enqueued messages which are missing the content-type header + #TODO: Please replace this decode with a die in the future once content-type is well established + decode_json($body); # TODO Should this be from_json? Check utf8 flag. + } + } elsif ( $command && $command eq 'ERROR' ) { + + #Known errors: + #You must log in using CONNECT first + #"NACK" must include a valid "message-id" header + Koha::Logger->get( { interface => 'worker' } ) + ->warn( sprintf "Shutting down after receiving ERROR frame:\n%s\n", $frame->as_string ); + exit 1; + } } catch { Koha::Logger->get( { interface => 'worker' } )->warn( sprintf "Frame not processed - %s", $_ ); return; -- 2.34.1