From 2f5f907d43b3263d085f71aefea51f5e1a78bfaf Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Tue, 4 Sep 2018 10:21:59 +1200 Subject: [PATCH] Bug 15529 - Locking process_message_queue.pl cron whilst it is running This amended patch is called by koha-common. koha-common runs this process_message_queue.pl cronjob foreach enabled Koha instance on the server. As this cronjob is run for every Koha instance the retrival of all Koha instance names from debian/scripts/koha-scripts in previous obsolete patches on this bug report and the looping through every name to check if the lock file existed is redundant. Instead this patch retrieves the 'logdir' of the Koha instance being iterated over and checks if the messagequeue.lock file in that directory is locked. If the aforementioned file is not locked then the SendQueuedMessages() in C4/Letters.pm is called, otherwise an email is sent (to the manually inputted email server, and to/from addresses) and duplicate execution of the cronjob is prevented. Test plan: 1. In two terminals open koha-shell and try to run ./process_message_queue.pl and notice you can run the script multiple times simulataneously without a warning/error being displayed 2. Apply patch and input the email server, to/from email addresses you would like the email to be sent to. Also confirm your Koha instance is enabled to send emails. 3. Repeat step 1 and notice that the second of the two scripts to be run will output text saying './process_message_queue.pl is already running. Exiting' In this case the aforementioned text is displayed and the body of the cronjob is not run. 4. The first cronjob script to run, runs without issues 5. At the end of the execution of the first script a 15 second pause is run 6. Notice you have received an email Sponsored-By: New Zealand Parliamentary Library --- misc/cronjobs/process_message_queue.pl | 49 +++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/misc/cronjobs/process_message_queue.pl b/misc/cronjobs/process_message_queue.pl index 93f5c35..0a6d8c9 100755 --- a/misc/cronjobs/process_message_queue.pl +++ b/misc/cronjobs/process_message_queue.pl @@ -28,6 +28,7 @@ BEGIN { use C4::Letters; use C4::Log; use Getopt::Long; +use C4::Context; my $username = undef; my $password = undef; @@ -37,6 +38,14 @@ my $help = 0; my $verbose = 0; my $type = q{}; +use MIME::Lite; +use Mail::Sendmail; +use Encode; +use Net::SMTP; + +use Fcntl ':flock'; + + GetOptions( 'u|username:s' => \$username, 'p|password:s' => \$password, @@ -68,14 +77,36 @@ die $usage if $help; cronlogaction(); -C4::Letters::SendQueuedMessages( - { - verbose => $verbose, - username => $username, - password => $password, - method => $method, - limit => $limit, - type => $type, - } +print "start of program\n"; +my $logdir = C4::Context->config('logdir'); + +chomp $logdir; +my $instancemessagequeuefile = $logdir . "/messagequeue.lock"; +open(FOO, ">> $instancemessagequeuefile") or die "Cannot write to $instancemessagequeuefile: $!"; + +print "Getting the lock\n"; +unless (flock ( FOO, LOCK_EX|LOCK_NB )) { + +print "$0 is already running. Exiting.\n"; +#Send email to inform administrator another process_message_queue.pl cron was attempted to run whilst previous execution of the file is taking place. +# Configure accordingly +MIME::Lite->send( 'smtp', ''); +my $email = MIME::Lite->new( +To => ' ', +From => ' ', +Type => 'TEXT', +Subject => 'process_message_queue.pl cron is already running', +Data => 'process_message_queue.pl cron is already running', +); + +$email->attach( +Type => 'Text', + Data => 'process_message_queue.pl cron is already running', ); +$email->send(); +exit(1); +} +C4::Letters::SendQueuedMessages( { verbose => $verbose, username => $username, password => $password, method => $method } ); +sleep(15); +print "End of process_message_queue.pl cron process"; -- 2.7.4