From d489211e3ee065af297bb185cfea00941a3d22d7 Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Sat, 25 Aug 2018 23:53:21 +1200 Subject: [PATCH] Bug 15529 - Using flock to lock new messagequeue.lock lockfile whilst process_message_queue.pl is being executed 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 3. Repeast 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 Sponsored-By: Parliament Library --- misc/cronjobs/process_message_queue.pl | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/misc/cronjobs/process_message_queue.pl b/misc/cronjobs/process_message_queue.pl index 93f5c35..fb3ead8 100755 --- a/misc/cronjobs/process_message_queue.pl +++ b/misc/cronjobs/process_message_queue.pl @@ -28,6 +28,11 @@ BEGIN { use C4::Letters; use C4::Log; use Getopt::Long; +use MIME::Lite; +use Mail::Sendmail; +use Encode; + +use Fcntl ':flock'; my $username = undef; my $password = undef; @@ -68,6 +73,29 @@ die $usage if $help; cronlogaction(); +print "start of program\n"; + +my $file = "/tmp/messagequeue.lock"; +open(FOO, ">> $file") or die "Cannot write to $file: $!"; +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 + my $email = MIME::Lite->build( + To => ' ', + From => ' ', + Type => 'TEXT', + Subject => 'process_message_queue.pl cron is already running', + Data => 'process_message_queue.pl cron is already running', + contenttype => 'charset="UTF-8"', + ); + $email->send('smpt', ' '); + exit(1); +} + C4::Letters::SendQueuedMessages( { verbose => $verbose, @@ -79,3 +107,6 @@ C4::Letters::SendQueuedMessages( } ); +print "sleeping 15..\n"; +sleep(15); +print "End of process_message_queue.pl cron process"; -- 2.1.4