From 167711086420a258f19e535526e07a1877178a49 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 | 10 +++- .../data/mysql/atomicupdate/bug_37885.pl | 23 +++++++++ installer/data/mysql/mandatory/sysprefs.sql | 3 +- .../en/modules/admin/preferences/admin.pref | 9 ++++ t/db_dependent/Letters.t | 47 ++++++++++++++++++- 5 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_37885.pl diff --git a/C4/Letters.pm b/C4/Letters.pm index 7ddb89b5..f6672071 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -217,8 +217,8 @@ sub GetLettersAvailableForALibrary { } return [ - map { $letters{$_} } - sort { $letters{$a}->{name} cmp $letters{$b}->{name} } + map { $letters{$_} } + sort { $letters{$a}->{name} cmp $letters{$b}->{name} } keys %letters ]; @@ -1029,6 +1029,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( { @@ -1048,6 +1052,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 100644 index 00000000..e54e6531 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_37885.pl @@ -0,0 +1,23 @@ +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 c4bb3ca9..31797177 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -417,6 +417,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'), @@ -874,4 +875,4 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('z3950AuthorAuthFields','701,702,700',NULL,'Define the MARC biblio fields for Personal Name Authorities to fill biblio.author','free'), ('z3950NormalizeAuthor','0','','If ON, Personal Name Authorities will replace authors in biblio.author','YesNo'), ('z3950Status','','','This syspref allows to define custom YAML based rules for marking items unavailable in z3950 results.','Textarea') -; +; \ No newline at end of file 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 18d21a95..b249fcfd 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 @@ -310,3 +310,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 \ No newline at end of file diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index 70dfaf51..b7315077 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 => 104; +use Test::More tests => 105; use Test::MockModule; use Test::Warn; @@ -1825,3 +1825,48 @@ subtest 'Virtual method ->strftime in notices' => sub { }; $schema->storage->txn_rollback; + +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.30.2