Created
April 30, 2013 19:13
-
-
Save yanick/5491146 to your computer and use it in GitHub Desktop.
Lock the lockfiles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package LockedLockFile; | |
use strict; | |
use warnings; | |
use Moose; | |
use Path::Tiny; | |
use File::Flock::Tiny; | |
has level => ( | |
is => 'ro', | |
isa => 'Int', | |
default => 0, | |
); | |
has lock => ( | |
is => 'ro', | |
lazy => 1, | |
isa => 'LockedLockFile', | |
default => sub { | |
my $self = shift; | |
LockedLockFile->new( level => $self->level +1 ); | |
}, | |
); | |
has _lock => ( | |
is => 'ro', | |
lazy => 1, | |
isa => 'File::Flock::Tiny::Lock', | |
default => sub { | |
my $self = shift; | |
my $file = "/tmp/" . join '-', path($0)->basename, $self->level; | |
warn "locking $file\n"; | |
return File::Flock::Tiny->write_pid($file) | |
|| die "lock already in use, aborting this run\n"; | |
}, | |
); | |
sub BUILD { | |
my $self = shift; | |
$self->_lock; # lock ourselves | |
$self->lock # lock our lock | |
} | |
package main; | |
my $lockmeup = LockedLockFile->new; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment