LockFile-Simple
view release on metacpan or search on metacpan
listed in alphabetical order:
=over 4
=item I<autoclean>
When true, all locks are remembered and pending ones are automatically
released when the process exits normally (i.e. whenever Perl calls the
END routines).
=item I<delay>
The amount of seconds to wait between locking attempts when the file appears
to be already locked. Default is 2 seconds.
=item I<efunc>
A function pointer to dereference when an error is to be reported. By default,
it redirects to the logerr() routine if you have Log::Agent installed,
to Perl's warn() function otherwise.
You may set it explicitely to C<\&LockFile::Simple::core_warn> to force the
use of Perl's warn() function, or to C<undef> to suppress logging.
=item I<ext>
The locking extension that must be added to the file path to be locked to
compute the I<lockfile> path. Default is C<.lock> (note that C<.> is part
of the extension and can therefore be changed). Ignored when I<format> is
also used.
=item I<format>
Using this parmeter supersedes the I<ext> parmeter. The formatting string
specified is run through a rudimentary macro expansion to derive the
I<lockfile> path from the file to be locked. The following macros are
available:
%% A real % sign
%f The full file path name
%D The directory where the file resides
%F The base name of the file
%p The process ID (PID)
The default is to use the locking extension, which itself is C<.lock>, so
it is as if the format used was C<%f.lock>, but one could imagine things
like C</var/run/%F.%p>, i.e. the I<lockfile> does not necessarily lie besides
the locked file (which could even be missing).
When locking, the locking format can be specified to supersede the object
configuration itself.
=item I<hold>
Maximum amount of seconds we may hold a lock. Past that amount of time,
an existing I<lockfile> is removed, being taken for a stale lock. Default
is 3600 seconds. Specifying 0 prevents any forced unlocking.
=item I<max>
Amount of times we retry locking when the file is busy, sleeping I<delay>
seconds between attempts. Defaults to 30.
=item I<nfs>
A boolean flag, false by default. Setting it to true means we could lock
over NFS and therefore the hostname must be included along with the process
ID in the stamp written to the lockfile.
=item I<stale>
A boolean flag, false by default. When set to true, we attempt to detect
stale locks and break them if necessary.
=item I<wafter>
Stands for I<warn after>. It is the number of seconds past the first
warning during locking time after which a new warning should be emitted.
See I<warn> and I<wmin> below. Default is 20.
=item I<warn>
A boolean flag, true by default. To suppress any warning, set it to false.
=item I<wfunc>
A function pointer to dereference when a warning is to be issued. By default,
it redirects to the logwarn() routine if you have Log::Agent installed,
to Perl's warn() function otherwise.
You may set it explicitely to C<\&LockFile::Simple::core_warn> to force the
use of Perl's warn() function, or to C<undef> to suppress logging.
=item I<wmin>
The minimal amount of time when waiting for a lock after which a first
warning must be emitted, if I<warn> is true. After that, a warning will
be emitted every I<wafter> seconds. Defaults to 15.
=back
Each of those configuration attributes can be queried on the object directly:
$obj = LockFile::Simple->make(-nfs => 1);
$on_nfs = $obj->nfs;
Those are pure query routines, i.e. you cannot say:
$obj->nfs(0); # WRONG
$obj->configure(-nfs => 0); # Right
to turn of the NFS attribute. That is because my OO background chokes
at having querying functions with side effects.
=head1 INTERFACE
The OO interface documented below specifies the signature and the
semantics of the operations. Only the C<lock>, C<trylock> and
C<unlock> operation can be imported and used via a non-OO interface,
with the exact same signature nonetheless.
above, plus, in alphabetical order:
=over 4
=item configure(I<-key =E<gt> value, -key2 =E<gt> value2, ...>)
Change the specified configuration parameters and silently ignore
the invalid ones.
=item lock(I<file>, I<format>)
Attempt to lock the file, using the optional locking I<format> if
specified, otherwise using the default I<format> scheme configured
in the object, or by simply appending the I<ext> extension to the file.
If the file is already locked, sleep I<delay> seconds before retrying,
repeating try/sleep at most I<max> times. If warning is configured,
a first warning is emitted after waiting for I<wmin> seconds, and
then once every I<wafter> seconds, via the I<wfunc> routine.
Before the first attempt, and if I<hold> is non-zero, any existing
I<lockfile> is checked for being too old, and it is removed if found
to be stale. A warning is emitted via the I<wfunc> routine in that
case, if allowed.
Likewise, if I<stale> is non-zero, a check is made to see whether
any locking process is still around (only if the lock holder is on the
same machine when NFS locking is configured). Should the locking
process be dead, the I<lockfile> is declared stale and removed.
Returns a lock handle if the file has been successfully locked, which
does not necessarily needs to be kept around. For instance:
$obj->lock('ppp', '/var/run/ppp.%p');
<do some work>
$obj->unlock('ppp');
or, using OO programming:
my $lock = $obj->lock('ppp', '/var/run/ppp.%p') ||;
die "Can't lock for ppp\n";
<do some work>
$lock->relase; # The only method defined for a lock handle
i.e. you don't even have to know which file was locked to release it, since
there is a lock handle right there that knows enough about the lock parameters.
=item lockfile(I<file>, I<format>)
Simply compute the path of the I<lockfile> that would be used by the
I<lock> procedure if it were passed the same parameters.
=item make(I<-key =E<gt> value, -key2 =E<gt> value2, ...>)
The creation routine for the simple lock object. Returns a blessed hash
reference.
=item trylock(I<file>, I<format>)
Same as I<lock> except that it immediately returns false and does not
sleep if the to-be-locked file is busy, i.e. already locked. Any
stale locking file is removed, as I<lock> would do anyway.
Returns a lock hande if the file has been successfully locked.
=item unlock(I<file>)
Unlock the I<file>.
=back
=head1 BUGS
The algorithm is not bullet proof. It's only reasonably safe. Don't bet
the integrity of a mission-critical database on it though.
The sysopen() call should probably be used with the C<O_EXCL|O_CREAT> flags
to be on the safer side. Still, over NFS, this is not an atomic operation
anyway.
B<BEWARE>: there is a race condition between the time we decide a lock is
stale or too old and the time we unlink it. Don't use C<-stale> and set
C<-hold> to 0 if you can't bear with that idea, but recall that this race
only happens when something is already wrong. That does not make it right,
nonetheless. ;-)
=head1 AUTHOR
Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>>
=head1 SEE ALSO
File::Flock(3).
=cut
( run in 1.456 second using v1.01-cache-2.11-cpan-39bf76dae61 )