From deded8c30f45d1e1ebdc1b70be2511ae4b891200 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Wed, 5 Jul 2017 10:33:16 -0400 Subject: [PATCH] Bug 18894 - Add ability to limit the number of messages sent by misc/cronjobs/process_message_queue.pl at a time Having the ability to limit the number of messages sent by process_message_queue.pl on a single run would be very useful for controlling home many messages are sent at a given time. This can help prevent too many messages being sent out at once and getting flagged as a spammer. Test Plan: 1) Apply this patch 2) Generate some number of messages in the message queue 3) Run process_message_queue.pl with the new --limit option, set limit to a number smaller than the number of pending messages 4) After the script has run, check the database and note that only a number of pending messages were sent, and that the remaining amount of pending messages is the original amount less the number specified as the limit --- C4/Letters.pm | 18 +++++++++++------- misc/cronjobs/process_message_queue.pl | 13 ++++++++++++- t/db_dependent/Letters.t | 7 +++++-- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index ada2794..c6435fb 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -1034,7 +1034,7 @@ returns number of messages sent. sub SendQueuedMessages { my $params = shift; - my $unsent_messages = _get_unsent_messages(); + my $unsent_messages = _get_unsent_messages( { limit => $params->{limit} } ); MESSAGE: foreach my $message ( @$unsent_messages ) { # warn Data::Dumper->Dump( [ $message ], [ 'message' ] ); warn sprintf( 'sending %s message to patron: %s', @@ -1267,13 +1267,17 @@ sub _add_attachments { sub _get_unsent_messages { my $params = shift; + my $limit = q{}; + $limit = "LIMIT $params->{limit}" if defined( $params->{limit} ) && $params->{limit} =~ /^\d+\z/; + my $dbh = C4::Context->dbh(); - my $statement = << 'ENDSQL'; -SELECT mq.message_id, mq.borrowernumber, mq.subject, mq.content, mq.message_transport_type, mq.status, mq.time_queued, mq.from_address, mq.to_address, mq.content_type, b.branchcode, mq.letter_code - FROM message_queue mq - LEFT JOIN borrowers b ON b.borrowernumber = mq.borrowernumber - WHERE status = ? -ENDSQL + my $statement = qq{ + SELECT mq.message_id, mq.borrowernumber, mq.subject, mq.content, mq.message_transport_type, mq.status, mq.time_queued, mq.from_address, mq.to_address, mq.content_type, b.branchcode, mq.letter_code + FROM message_queue mq + LEFT JOIN borrowers b ON b.borrowernumber = mq.borrowernumber + WHERE status = ? + $limit + }; my @query_params = ('pending'); if ( ref $params ) { diff --git a/misc/cronjobs/process_message_queue.pl b/misc/cronjobs/process_message_queue.pl index 3325c8a..adb11bd 100755 --- a/misc/cronjobs/process_message_queue.pl +++ b/misc/cronjobs/process_message_queue.pl @@ -31,6 +31,7 @@ use Getopt::Long; my $username = undef; my $password = undef; +my $limit = undef; my $method = 'LOGIN'; my $help = 0; my $verbose = 0; @@ -38,6 +39,7 @@ my $verbose = 0; GetOptions( 'u|username:s' => \$username, 'p|password:s' => \$password, + 'l|limit:s' => \$limit, 'm|method:s' => \$method, 'h|help|?' => \$help, 'v|verbose' => \$verbose, @@ -53,6 +55,7 @@ advance_notices.pl script. This script has the following parameters : -u --username: username of mail account -p --password: password of mail account + -l --limit: The maximum number of messages to process for this run -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 @@ -63,5 +66,13 @@ die $usage if $help; cronlogaction(); -C4::Letters::SendQueuedMessages( { verbose => $verbose, username => $username, password => $password, method => $method } ); +C4::Letters::SendQueuedMessages( + { + verbose => $verbose, + username => $username, + password => $password, + method => $method, + limit => $limit, + } +); diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index 7950750..e59e374 100644 --- 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 => 82; +use Test::More tests => 83; use Test::MockModule; use Test::Warn; @@ -132,7 +132,10 @@ is( $messages->[0]->{status}, 'pending', 'EnqueueLetter stores the status pendin # SendQueuedMessages -my $messages_processed = C4::Letters::SendQueuedMessages(); +my $messages_processed = C4::Letters::SendQueuedMessages( { limit => 0 } ); +is($messages_processed, 0, 'No queued messages processed with limit of 0 passed in'); + +$messages_processed = C4::Letters::SendQueuedMessages(); is($messages_processed, 1, 'all queued messages processed'); $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber }); is( -- 2.10.2