NAME
App::DistSync::LockFile - Lock File management package
SYNOPSIS
use App::DistSync::LockFile;
my $lf = App::DistSync::LockFile->new (
file => '/tmp/file.lock',
pid => $$,
auto => 0,
);
if ( my $pid = $lf->check ) {
warn $lf->error if $lf->error;
die "Already running: $pid";
}
$lf->lock;
die $lf->error if $lf->error;
# . . . do stuff . . .
$lf->unlock;
die $lf->error if $lf->error;
... or with auto-lock and auto-unlock:
my $lf = App::DistSync::LockFile->new (
file => '/tmp/file.lock',
pid => $$,
auto => 1,
);
die $lf->error if $lf->error;
die "Already running" if $lf->check;
# . . . do stuff . . .
DESCRIPTION
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 File::Pid, Lock::File, File::TinyLock, JIP::LockFile and LockFile::Simple.
METHODS
This module implements the following methods
new
my $lf = new App::DistSync::LockFile(
file => '/tmp/file.lock',
delay => 60,
retries => 5,
pid => $$,
auto => 1,
);
This constructor takes several optional attributes:
- auto
-
auto => 0If 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
- debug
-
debug => 0Print debugging messages to STDERR (0=Off (default), 1=On)
- delay
-
delay => 60Number of seconds to wait between retries to getting a lockfile
Default: 60
- 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 ./basename($0).lock.
- pid
-
pid => $$The pid to write to a new lockfile. If not specified,
$$is used when the lock file doesn't exist. When the lock file does exist, the pid inside it is used. - retries
-
retries => 5Number of times to retry getting a lockfile
Default: 5
check
if ( my $pid = $lf->check ) {
warn $lf->error if $lf->error;
die "Already running: $pid";
}
This method checks the lock file and returns the PID of the process that first acquired the lock. Otherwise returns 0 if no lock file found
error
my $error = $lf->error;
Returns current error message
file
my $file = $lf->file;
Accessor for the filename used as the lock file.
lock
$self = $self->lock;
This method creates a lock file and stores the current PID in it.
own
$lf->own(123);
my $owner_did = $lf->own;
Accessor/mutator for the pid being saved to the lock file.
pid
my $pid = $lf->pid;
Accessor for the pid being saved to the lock file.
unlock
$self = $self->unlock;
This method performs unlocking the lock file and removes it
HISTORY
See Changes file
TO DO
See TODO file
SEE ALSO
Acme::Ghost::FilePid, Lock::File, File::TinyLock, JIP::LockFile and LockFile::Simple
AUTHOR
Serż Minus (Sergey Lepenkov) https://www.serzik.com <abalama@cpan.org>
COPYRIGHT
Copyright (C) 1998-2025 D&D Corporation. All Rights Reserved
LICENSE
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See LICENSE file and https://dev.perl.org/licenses/