Acrux
view release on metacpan or search on metacpan
lib/Acrux/FileLock.pm view on Meta::CPAN
flock => 0,
);
if ( $fl->check ) {
warn $fl->error if $fl->error;
die "Already running: $fl->own";
}
$fl->lock;
die $fl->error if $fl->error;
# . . . do stuff . . .
$fl->unlock;
die $fl->error if $fl->error;
... or with auto-lock and auto-unlock:
my $fl = Acrux::FileLock->new(
file => '/tmp/file.lock',
pid => $$,
auto => 1,
);
die $fl->error if $fl->error;
die "Already running" if $fl->check;
# . . . do stuff . . .
=head1 DESCRIPTION
The Lock File simple interface
This package manages a lock files. It will create a lock file,
query the process within to discover if it's still running, and remove
the lock file. This module based on L<Lock::File>, L<File::TinyLock>,
L<JIP::LockFile>, L<LockFile::Simple> and L<Acrux::FilePid>.
=head1 METHODS
This module implements the following methods
=head2 new
my $fl = Acrux::FileLock->new(
file => '/tmp/file.lock',
delay => 60,
retries => 5,
pid => $$,
auto => 1,
);
This constructor takes several optional attributes:
=over 4
=item auto
auto => 0
If this flag specified as true, then
will be saved the lock file automatically while instance create and
removed the lock file automatically on DESTROY phase. Default: false
=item debug
debug => 0
Print debugging messages to STDERR (0=Off (default), 1=On)
=item delay
delay => 60
Number of seconds to wait between retries to getting a lockfile
Default: 60
=item file
file => '/tmp/test.lock'
The name of the lock file to work on. If not specified, a lock
file located in current directory will be created that matches F<./basename($0).lock>.
=item flock
flock => 1
If this flag is set to true then lockfile will be lock by system with flock
Default: 0
=item pid
pid => $$
The pid to write to a new lockfile. If not specified, C<$$> is
used when the lock file doesn't exist. When the lock file does exist, the
pid inside it is used.
=item retries
retries => 5
Number of times to retry getting a lockfile
Default: 5
=back
=head2 check
if ( $fl->check ) {
warn $fl->error if $fl->error;
die "Already running: $fl->own";
}
This method checks whether the lock is currently considered active.
Returns C<1> if the lock is active or C<0> if the lock is free.
The return value is always boolean and does not contain the PID of the
lock owner.
Before checking the lock state, this method reads the owner information
from the lock file and updates the C<own> and C<uid> attributes when the
file contains valid owner data. These values represent the metadata
stored in the lock file at the time of the call and must not be
considered proof that the corresponding process currently owns the
lock.
When C<flock> mode is enabled, the lock state is determined exclusively
by the system file lock. The C<own> and C<uid> attributes are still
updated from the lock file, but they do not participate in determining
the lock state.
In the regular mode, the owner PID and UID are additionally used to
determine whether the lock is still valid. A stale lock file is removed
automatically when possible.
If a stale lock file cannot be removed by the operating system, the
lock is considered active and C<check> returns C<1>. This condition is
reported through the debug facility and does not set C<error>.
=head2 error
my $error = $fl->error;
Returns current error message
( run in 0.617 second using v1.01-cache-2.11-cpan-5c0b1e786e0 )