1 package OpenILS::Utils::Lockfile;
3 # ---------------------------------------------------------------
4 # Copyright (C) 2010 Equinox Software, Inc
5 # Author: Joe Atzberger <jatzberger@esilibrary.com>
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 # ---------------------------------------------------------------
18 # The purpose of this module is to consolidate
19 # non-duplicative processing, i.e. lockfiles and lockfile checking
25 use File::Basename qw/fileparse/;
28 return $ENV{TEMP} || $ENV{TMP} || '/tmp';
33 sub default_filename {
34 my $tempdir = _tempdir;
35 my $filename = fileparse($0, '.pl');
36 return "$tempdir/$filename-LOCK";
41 my $lockfile = @_ ? shift : default_filename;
43 croak "Script already running with lockfile $lockfile" if -e $lockfile;
44 $debug and print "Writing lockfile $lockfile (PID: $$)\n";
46 open (F, ">$lockfile") or croak "Cannot write to lockfile '$lockfile': $!";
51 filename => $lockfile,
54 return bless ($self, $class);
59 return $self->{filename};
63 return $self->{contents};
69 if (-e $self->{filename}) {
70 open LF, $self->{filename};
73 $debug and print "deleting lockfile $self->{filename}\n";
74 if ($contents == $self->{contents}) {
75 unlink $self->{filename} or carp "Failed to remove lockfile '$self->{filename}'";
77 carp "Lockfile contents '$contents' no longer match '$self->{contents}'. Cannot remove $self->{filename}";