From 1fc8d0246c090476d30bfde6085c092fb607f7ae Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Mon, 2 Dec 2013 16:38:27 +0200 Subject: [PATCH] Use flock in ::Base instead of O_EXCL --- lib/Gruntmaster/Daemon.pm | 2 +- lib/Gruntmaster/Daemon/Base.pm | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/Gruntmaster/Daemon.pm b/lib/Gruntmaster/Daemon.pm index ebab95c..ccf9e4e 100644 --- a/lib/Gruntmaster/Daemon.pm +++ b/lib/Gruntmaster/Daemon.pm @@ -55,7 +55,7 @@ sub process{ chdir $dir; mkdir "in"; for (<*>) { - cp $_, "in" unless $_ eq 'in' || $_ eq 'pidfile'; + cp $_, "in" unless $_ eq 'in'; } my @results; diff --git a/lib/Gruntmaster/Daemon/Base.pm b/lib/Gruntmaster/Daemon/Base.pm index b7db789..d63ddee 100644 --- a/lib/Gruntmaster/Daemon/Base.pm +++ b/lib/Gruntmaster/Daemon/Base.pm @@ -7,7 +7,7 @@ use parent qw/Exporter/; our @EXPORT_OK = qw/watch/; our $VERSION = '0.001'; -use Fcntl qw/O_WRONLY O_EXCL O_CREAT/; +use Fcntl qw/:flock/; use Linux::Inotify2; use Log::Log4perl qw/get_logger/; @@ -17,12 +17,14 @@ sub process{ my ($name, $dir, $cb) = @_; my $logger = get_logger; $logger->debug("Taking job $name..."); - if (sysopen my $file, "$dir/$name/pidfile", O_WRONLY | O_EXCL | O_CREAT){ + open my $file, ">>$dir/$name/meta.yml"; + if (flock my $file, LOCK_EX|LOCK_NB){ $logger->debug("Successfully taken job $name, executing callback"); $cb->("$dir/$name"); } else { $logger->debug("Job $name already taken"); } + close $file; } sub watch{ -- 2.39.2