From 54f4f0441bafdc49fe4d1ae978bc28383a6aa10e Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Tue, 9 Oct 2018 10:43:23 +0000 Subject: [PATCH] Bug 15529: Altered logic for lockfile creation Implemented logic for creating lockfile preventing a lock file being added to a logging directory. This patch adds logic (outlined below) similar to what is in use in rebuild_zebra.pl. The logic is: Create lockfile in either: 1. Location defined in config (cron_lockdir) OR 2. A standard location (/var/koha/cron_) OR 3. /tmp/cron_ Sponsored-By: New Zealand Parliamentary Library --- Makefile.PL | 7 ++++++ debian/templates/koha-conf-site.xml.in | 1 + etc/koha-conf.xml | 1 + misc/cronjobs/process_message_queue.pl | 42 ++++++++++++++++++++++++++++------ rewrite-config.PL | 1 + 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/Makefile.PL b/Makefile.PL index 924782f..2907f24 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -220,6 +220,10 @@ Directory for Zebra's lock files. This includes subdirs for authorities, biblios, and the zebra rebuild function. Any activity to reindex zebra from koha should interlock here with rebuild_zebra.pl. +=item CRON_LOCK_DIR + +Directory for cronjob lock files. + =item ZEBRA_DATA_DIR Directory for Zebra's data files. @@ -358,6 +362,7 @@ my $target_map = { './skel/var/lib/koha/zebradb/biblios/register' => { target => 'ZEBRA_DATA_DIR', trimdir => 6 }, './skel/var/lib/koha/zebradb/biblios/shadow' => { target => 'ZEBRA_DATA_DIR', trimdir => 6 }, './skel/var/lib/koha/zebradb/biblios/tmp' => { target => 'ZEBRA_DATA_DIR', trimdir => 6 }, + './skel/var/lock/koha/cron' => { target => 'CRON_LOCK_DIR', trimdir => 6 }, './skel/var/lock/koha/zebradb/rebuild' => { target => 'ZEBRA_LOCK_DIR', trimdir => 6 }, './skel/var/lib/koha/plugins' => { target => 'PLUGINS_DIR', trimdir => 6 }, './sms' => 'INTRANET_CGI_DIR', @@ -649,6 +654,7 @@ my %test_suite_override_dirs = ( BACKUP_DIR => ['var', 'spool'], SCRIPT_DIR => ['bin'], ZEBRA_LOCK_DIR => ['var', 'lock', 'zebradb'], + CRON_LOCK_DIR => ['var', 'lock', 'cron'], ZEBRA_DATA_DIR => ['var', 'lib', 'zebradb'], ZEBRA_RUN_DIR => ['var', 'run', 'zebradb'], ); @@ -1350,6 +1356,7 @@ sub get_target_directories { $dirmap{'MAN_DIR'} = File::Spec->catdir(@basedir, $package, 'man'); $dirmap{'DOC_DIR'} = File::Spec->catdir(@basedir, $package, 'doc'); $dirmap{'ZEBRA_LOCK_DIR'} = File::Spec->catdir(@basedir, $package, 'var', 'lock', 'zebradb'); + $dirmap{'CRON_LOCK_DIR'} = File::Spec->catdir(@basedir, $package, 'var', 'lock', 'cron'); $dirmap{'LOG_DIR'} = File::Spec->catdir(@basedir, $package, 'var', 'log'); $dirmap{'BACKUP_DIR'} = File::Spec->catdir(@basedir, $package, 'var', 'spool'); $dirmap{'PLUGINS_DIR'} = File::Spec->catdir(@basedir, $package, 'var', 'lib', 'koha', 'plugins'); diff --git a/debian/templates/koha-conf-site.xml.in b/debian/templates/koha-conf-site.xml.in index e6982bb..65797cc 100644 --- a/debian/templates/koha-conf-site.xml.in +++ b/debian/templates/koha-conf-site.xml.in @@ -295,6 +295,7 @@ __END_SRU_PUBLICSERVER__ 0 0 /var/lock/koha/__KOHASITE__ + /var/lock/koha/__KOHASITE__ 1 /etc/koha/searchengine/queryparser.yaml __KOHA_CONF_DIR__/log4perl.conf diff --git a/etc/koha-conf.xml b/etc/koha-conf.xml index 4af100e..508e28d 100644 --- a/etc/koha-conf.xml +++ b/etc/koha-conf.xml @@ -116,6 +116,7 @@ __PAZPAR2_TOGGLE_XML_POST__ 0 0 __ZEBRA_LOCK_DIR__ + __CRON_LOCK_DIR__ 1 __KOHA_CONF_DIR__/searchengine/queryparser.yaml __KOHA_CONF_DIR__/log4perl.conf diff --git a/misc/cronjobs/process_message_queue.pl b/misc/cronjobs/process_message_queue.pl index 6a0d760..cb91643 100755 --- a/misc/cronjobs/process_message_queue.pl +++ b/misc/cronjobs/process_message_queue.pl @@ -29,6 +29,9 @@ use C4::Letters; use C4::Log; use Getopt::Long; use C4::Context; +use File::Path; + +use constant LOCK_FILENAME => 'message_queue..LCK'; my $username = undef; my $password = undef; @@ -37,10 +40,10 @@ my $method = 'LOGIN'; my $help = 0; my $verbose = 0; my $type = q{}; +my $use_flock; use Fcntl ':flock'; - GetOptions( 'u|username:s' => \$username, 'p|password:s' => \$password, @@ -80,16 +83,31 @@ die $usage if $help; cronlogaction(); if ($verbose) { print "start of program\n"; } -my $logdir = C4::Context->config('logdir'); -chomp $logdir; -my $instancemessagequeuefile = $logdir . "/messagequeue.lock"; -my $fh; -open $fh, '>>', $instancemessagequeuefile or die "Cannot write to $instancemessagequeuefile: $!"; +#Create a lock file in either in cron_lockdir (config line), a standard location (/var/lock/cron_) or /tmp/cron_) +my ($lockfile, $LockFH); +foreach ( + C4::Context->config("cron_lockdir"), + '/var/lock/cron_' . C4::Context->config('database'), + '/tmp/cron_' . C4::Context->config('database') +) { + next if !$_; + ($LockFH, $lockfile) = _create_lockfile($_.'/message_queue'); + last if defined $LockFH; +} +if( !defined $LockFH ) { + print "WARNING: Could not create lock file $lockfile: $!\n"; + print "Please check your koha-conf.xml for ZEBRA_LOCKDIR.\n"; + print "Verify file permissions for it too.\n"; + $use_flock = 0; # we disable file locking now and will continue + # without it + # note that this mimics old behavior (before we used + # the lockfile) +}; my $letter_code = ''; if ($verbose) { print "Getting the lock\n"; } -unless (flock ( $fh, LOCK_EX|LOCK_NB )) { +unless (flock ( $LockFH, LOCK_EX|LOCK_NB )) { print STDERR "$0 is already running!\n"; #If verbose is set send email to inform administrator another process_message_queue.pl cron was attempted to run whilst previous execution of the file is taking place. @@ -127,3 +145,13 @@ if ($verbose) { sleep(15); print "End of process_message_queue.pl cron process"; } + +sub _create_lockfile { #returns undef on failure + my $dir= shift; + unless (-d $dir) { + eval { mkpath($dir, 0, oct(755)) }; + return if $@; + } + return if !open my $fh, q{>}, $dir.'/'.LOCK_FILENAME; + return ( $fh, $dir.'/'.LOCK_FILENAME ); +} diff --git a/rewrite-config.PL b/rewrite-config.PL index 8b88684..53144be 100644 --- a/rewrite-config.PL +++ b/rewrite-config.PL @@ -124,6 +124,7 @@ $prefix = $ENV{'INSTALL_BASE'} || "/usr"; '__MAN_DIR__' => "$prefix/man", '__DOC_DIR__' => "$prefix/doc", '__ZEBRA_LOCK_DIR__' => "$prefix/var/lock/zebradb", + '__CRON_LOCK_DIR__' => "$prefix/var/lock/cron", '__ZEBRA_DATA_DIR__' => "$prefix/var/lib/zebradb", '__ZEBRA_RUN_DIR__' => "$prefix/var/run/zebradb", '__ZEBRA_MARC_FORMAT__' => 'marc21', -- 2.1.4