Skip to content

Instantly share code, notes, and snippets.

@yanick
Created April 30, 2013 19:13
Show Gist options
  • Save yanick/5491146 to your computer and use it in GitHub Desktop.
Save yanick/5491146 to your computer and use it in GitHub Desktop.
Lock the lockfiles
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