From 579a6f829af3ec07feb0b88eb67d1a8acb23b145 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Wed, 24 Aug 2022 13:54:49 -0400 Subject: [PATCH] Bug 31453: Add ability to skip processing messages in message queue based on message contents It would be very useful to be able to tell process_message_queue.pl to skip processing some messages. This is particularly useful where a plugin handles sending some message using the before_send_messages hook, but while that plugin is processing, more messages meant for the plugin might be queued. At that point, control moves back to the script and SendQueuedMessages is called, and those messages end up being processed there instead of by the plugin. Test Plan: 1) Apply this patch 2) Queue two messages, each with a unique word 3) Run process_message_queue --content-not-like "WORD" where WORD is a unique word in one of the two message 4) Note the message containing "WORD" was not processed 5) prove t/db_dependent/Letters.t --- C4/Letters.pm | 17 +++++---- misc/cronjobs/process_message_queue.pl | 4 +++ t/db_dependent/Letters.t | 49 +++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index 7c136f32f9..9a8c30bc73 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -971,12 +971,13 @@ Returns number of messages sent. sub SendQueuedMessages { my $params = shift; - my $which_unsent_messages = { - 'message_id' => $params->{'message_id'}, - 'limit' => $params->{'limit'} // 0, - 'borrowernumber' => $params->{'borrowernumber'} // q{}, - 'letter_code' => $params->{'letter_code'} // q{}, - 'type' => $params->{'type'} // q{}, + my $which_unsent_messages = { + 'message_id' => $params->{'message_id'}, + 'limit' => $params->{'limit'} // 0, + 'borrowernumber' => $params->{'borrowernumber'} // q{}, + 'letter_code' => $params->{'letter_code'} // q{}, + 'type' => $params->{'type'} // q{}, + 'content_not_like' => $params->{'content_not_like'}, }; my $unsent_messages = _get_unsent_messages( $which_unsent_messages ); MESSAGE: foreach my $message ( @$unsent_messages ) { @@ -1274,6 +1275,10 @@ sub _get_unsent_messages { $statement .= ' AND message_id = ?'; push @query_params, $params->{message_id}; } + if ( $params->{content_not_like} ) { + $statement .= ' AND content LIKE ?'; + push @query_params, "%$params->{content_not_like}%"; + } if ( $params->{'limit'} ) { $statement .= ' limit ? '; push @query_params, $params->{'limit'}; diff --git a/misc/cronjobs/process_message_queue.pl b/misc/cronjobs/process_message_queue.pl index d8918be607..002acc6059 100755 --- a/misc/cronjobs/process_message_queue.pl +++ b/misc/cronjobs/process_message_queue.pl @@ -34,6 +34,7 @@ my $help = 0; my $verbose = 0; my $type = q{}; my $letter_code; +my $content_not_like; GetOptions( 'u|username:s' => \$username, @@ -44,6 +45,7 @@ GetOptions( 'v|verbose' => \$verbose, 't|type:s' => \$type, 'c|code:s' => \$letter_code, + 'content-not-like:s'=> \$content_not_like, ); my $usage = << 'ENDUSAGE'; @@ -62,6 +64,7 @@ This script has the following parameters : -m --method: authentication method required by SMTP server (See perldoc Sendmail.pm for supported authentication types.) -h --help: this message -v --verbose: provides verbose output to STDOUT + --content-not-like: Message will not be processed if it contains this value ENDUSAGE die $usage if $help; @@ -101,6 +104,7 @@ C4::Letters::SendQueuedMessages( limit => $limit, type => $type, letter_code => $letter_code, + content_not_like => $content_not_like, } ); diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index 426df4aea2..74401afcb6 100755 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -18,7 +18,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 85; +use Test::More tests => 86; use Test::MockModule; use Test::Warn; use Test::Exception; @@ -905,6 +905,53 @@ EOF is( $items_content, $expected_items_content, 'get_item_content should return correct items info without time (if dateonly => 1)' ); }; +subtest 'Test content_not_like parameter for SendQueuedMessages' => sub { + plan tests => 1; + + my $dbh = C4::Context->dbh; + + my $borrowernumber = Koha::Patron->new({ + firstname => 'Jane', + surname => 'Smith', + categorycode => $patron_category, + branchcode => $library->{branchcode}, + dateofbirth => $date, + smsalertnumber => undef, + })->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 $my_message2 = { + 'letter' => { + 'content' => 'a skipped 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' + }; + C4::Letters::EnqueueLetter($my_message); + C4::Letters::EnqueueLetter($my_message2); + my $messages_processed = C4::Letters::SendQueuedMessages( { content_not_like => 'skipped' } ); + is( $messages_processed, 1, "Correctly skipped processing one message containing the work 'skipped' in contents" ); +}; + subtest 'Test limit parameter for SendQueuedMessages' => sub { plan tests => 3; -- 2.30.2