From 78cedb48237c61f86c248068c0692de248584456 Mon Sep 17 00:00:00 2001 From: Mark Hofstetter Date: Sat, 14 Sep 2024 11:24:09 +0200 Subject: [PATCH] Bug 35655: Only use STOMP if JobsNotificationMethod set to do so This patch makes the background jobs (jobs from now on) connect step not try to connect to the STOMP broker, unless explicitly told to do so. This is achieved by using a newly introduced system preference `JobsNotificationMethod` which falls back to STOMP if undefined. The goal for this patch is to stop flooding the logs on very busy sites when Rabbit is not being used. This shouldn't predate any efforts on making the STOMP based jobs brokering mechanism solid and usable. To test: 1. On clean `main`, run: $ ktd --shell 2. Open the logs on a separate terminal: $ ktd --shell k$ tail -f /var/log/koha/kohadev/*.log 3. Stage some MARC for import => SUCCESS: No warnings about STOMP 4. Import the staged records => SUCCESS: No warnings about STOMP 5. Stop rabbitmq: k$ sudo service rabbitmq-server stop 6. Repeat 2-4 => FAIL: The logs say it couldn't connect to STOMP 7. Apply this patches and run: k$ restart_all 8. Repeat 2-4 => FAIL: You get the same errors, because it defaults to STOMP and it is not available 9. Start rabbit: k$ sudo service rabbitmq-server start k$ restart_all 10. Repeat 2-4 => SUCCESS: No warnings about STOMP 11. Change the `JobsNotificationMethod` system preference to *polling* and stop rabbit: k$ sudo service rabbitmq-server stop k$ restart_all 11a. check about page (/cgi-bin/koha/about.pl) says "Message broker: Using SQL polling" 12. Repeat 2-4 => SUCCESS: No warnings about STOMP, not even on startup 13. Sign off :-D Signed-off-by: Mark Hofstetter --- Koha/BackgroundJob.pm | 25 +++++++++++++------------ about.pl | 7 +++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index dfd3cd335c..c1e305e59f 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -63,6 +63,10 @@ Connect to the message broker using default guest/guest credential sub connect { my ( $self ); + my $notification_method = C4::Context->preference('JobsNotificationMethod') // 'STOMP'; + return undef + unless $notification_method eq 'STOMP'; + my $hostname = 'localhost'; my $port = '61613'; my $config = C4::Context->config('message_broker'); @@ -78,7 +82,13 @@ sub connect { $credentials->{host} = $config->{vhost} if $config->{vhost}; } my $stomp = Net::Stomp->new( { hostname => $hostname, port => $port } ); - $stomp->connect( $credentials ); + try { + $stomp = $self->connect; + } catch { + warn "Cannot connect to broker " . $_; + $stomp = undef; + }; + return $stomp; } @@ -121,19 +131,10 @@ sub enqueue { } )->store; - my $notification_method = C4::Context->preference('JobsNotificationMethod') // 'STOMP'; - - return $self->id - unless $notification_method eq 'STOMP'; - $job_args->{job_id} = $self->id; - my $conn; - try { - $conn = $self->connect; - } catch { - warn "Cannot connect to broker " . $_; - }; + my $conn = $self->connect; + return $self->id unless $conn; $json_args = $json->encode($job_args); diff --git a/about.pl b/about.pl index 6233d8be23..751ad0d395 100755 --- a/about.pl +++ b/about.pl @@ -906,10 +906,9 @@ sub message_broker_check { my $template = shift; { # BackgroundJob - test connection to message broker - eval { Koha::BackgroundJob->connect; }; - if ($@) { - warn $@; - $template->param( warnConnectBroker => $@ ); + my $conn = Koha::BackgroundJob->connect; + if (!$conn) { + $template->param( warnConnectBroker => C4::Context->preference('JobsNotificationMethod') ); } } } -- 2.39.2