From 3010b2391fbabd29686158aa4d95a6690d78eaf5 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 19 Sep 2024 09:29:56 -0400 Subject: [PATCH] Bug 37885: Add ability to disable message queue processing There exist services that libraries utilize as an alternative to processing the message queue. These services may utilize the queued messages and send them from outside of Koha. Sometimes these services utilize the API or reports while other times they utilize a plugin. It would be beneficial to be able to disable running the message queue processor altogether, or to only execute the plugin hook related to message queue processing ( before_send_messages ) to limit the processing of messages to those handled by plugins. Test Plan: 1) Apply this patch 2) Run updatedatabase.pl 3) Install the kitchen sink plugin, enable it 4) Enqueue a notice 5) Set MessageQueueMode to "process nothing" 6) Run process_message_queue.pl 7) Note nothing happens, 0 message reported 8) Set MessageQueueMode to "process plugin hooks only" 9) Run process_message_queue.pl 10) Note nothing happens, 0 message reported 11) Set MessageQueueMode to "on" 12) Run process_message_queue.pl 13) Note the message fails to send, assuming you have not set up anything to actually send messages on your test instance --- C4/Letters.pm | 6 +++ .../data/mysql/atomicupdate/bug_37885.pl | 21 +++++++++ installer/data/mysql/mandatory/sysprefs.sql | 1 + .../en/modules/admin/preferences/admin.pref | 9 ++++ t/db_dependent/Letters.t | 47 ++++++++++++++++++- 5 files changed, 83 insertions(+), 1 deletion(-) create mode 100755 installer/data/mysql/atomicupdate/bug_37885.pl diff --git a/C4/Letters.pm b/C4/Letters.pm index ce8ffda7e17..a93303d55de 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -1021,6 +1021,10 @@ sub SendQueuedMessages { Koha::Exceptions::BadParameter->throw("Parameter message_id cannot be empty if passed.") if ( exists( $params->{message_id} ) && !$params->{message_id} ); + my $MessageQueueMode = C4::Context->preference('MessageQueueMode'); + + return 0 unless ( $MessageQueueMode eq 'on' || $MessageQueueMode eq 'plugin' ); + if ( C4::Context->config("enable_plugins") ) { my @plugins = Koha::Plugins->new->GetPlugins({ method => 'before_send_messages', @@ -1038,6 +1042,8 @@ sub SendQueuedMessages { } } + return 0 unless ( $MessageQueueMode eq 'on' ); + my $smtp_transports = {}; my $count_messages = 0; diff --git a/installer/data/mysql/atomicupdate/bug_37885.pl b/installer/data/mysql/atomicupdate/bug_37885.pl new file mode 100755 index 00000000000..664580fc124 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_37885.pl @@ -0,0 +1,21 @@ +use Modern::Perl; +use Koha::Installer::Output qw(say_warning say_failure say_success say_info); + +return { + bug_number => "37885", + description => "Add ability to disable message queue processing", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + try { + $dbh->do(q{ + INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type` ) VALUES + ('MessageQueueMode','on','on|off|plugins','Search Engine','Choice'); + }); + say_success( $out, "Added new system preference 'MessageQueueMode'" ); + } catch { + say_failure( $out, "Failed to add new system preference 'MessageQueueMode'" ); + } + }, +}; diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index 20d11d36ab3..9893b9b5cdc 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -411,6 +411,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('MaxTotalSuggestions','',NULL,'Number of total suggestions used for time limit with NumberOfSuggestionDays','Free'), ('MembershipExpiryDaysNotice','',NULL,'Send an account expiration notice that a patron\'s card is about to expire after','Integer'), ('MergeReportFields','',NULL,'Displayed fields for deleted MARC records after merge','Free'), +('MessageQueueMode','on','on|off|plugins','Search Engine','Choice'), ('minPasswordLength','8',NULL,'Specify the minimum length of a patron/staff password','free'), ('NewItemsDefaultLocation','','','If set, all new items will have a location of the given Location Code ( Authorized Value type LOC )',''), ('NewsAuthorDisplay','none','none|opac|staff|both','Display the author name for news items.','Choice'), diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref index 511341f0444..10856577d0d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref @@ -301,3 +301,12 @@ Administration: "ARRAY": "Searchable array" -
ISO2709 format is recommended as it is faster and takes less space, whereas array format makes the full MARC record searchable. -
NOTE: Making the full record searchable may have a negative effect on relevance ranking of search results. + Message processing: + - + - "When running the message queue processor" + - pref: MessageQueueMode + default: on + choices: + on: process all messages and plugin hooks + plugins: process plugin hooks only + off: process nothing diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index fbe496d7ac6..c603bae62a8 100755 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -19,7 +19,7 @@ use Modern::Perl; use File::Basename qw(dirname); -use Test::More tests => 103; +use Test::More tests => 104; use Test::MockModule; use Test::Warn; @@ -1582,3 +1582,48 @@ subtest 'Quote user params in GetPreparedLetter' => sub { $exec_time ); }; + +subtest 'Test MessageQueueMode' => sub { + plan tests => 3; + + my $dbh = C4::Context->dbh; + + my $borrowernumber = Koha::Patron->new( + { + firstname => 'Jane', + surname => 'Smith', + categorycode => $patron_category, + branchcode => $library->{branchcode}, + dateofbirth => $date, + } + )->store->borrowernumber; + + $dbh->do(q|DELETE FROM message_queue|); + $my_message = { + 'letter' => { + 'content' => 'a message', + 'metadata' => 'metadata', + 'code' => 'TEST_MESSAGE', + 'content_type' => 'text/plain', + 'title' => 'message title' + }, + 'borrowernumber' => $borrowernumber, + 'to_address' => undef, + 'message_transport_type' => 'sms', + 'from_address' => 'from@example.com' + }; + + my $id = C4::Letters::EnqueueLetter($my_message); + + t::lib::Mocks::mock_preference( 'MessageQueueMode', 'off' ); + C4::Letters::SendQueuedMessages(); + is( Koha::Notice::Messages->find($id)->status, 'pending', 'Message is pending for off' ); + + t::lib::Mocks::mock_preference( 'MessageQueueMode', 'plugin' ); + C4::Letters::SendQueuedMessages(); + is( Koha::Notice::Messages->find($id)->status, 'pending', 'Message is pending for plugin' ); + + t::lib::Mocks::mock_preference( 'MessageQueueMode', 'on' ); + C4::Letters::SendQueuedMessages(); + is( Koha::Notice::Messages->find($id)->status, 'failed', 'Message is failed for on' ); +}; -- 2.39.5 (Apple Git-154)