From 3c6fc13e24fef2b38d3fe017cfa54e9378ed4cf3 Mon Sep 17 00:00:00 2001 From: sersut Date: Mon, 7 Oct 2013 16:29:10 -0700 Subject: Make waiting for the release of the lock optional in RunLock class. --- lib/chef/run_lock.rb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'lib/chef/run_lock.rb') diff --git a/lib/chef/run_lock.rb b/lib/chef/run_lock.rb index 4bde09de54..0e1f24a9c5 100644 --- a/lib/chef/run_lock.rb +++ b/lib/chef/run_lock.rb @@ -35,8 +35,11 @@ class Chef # Create a new instance of RunLock # === Arguments # * :lockfile::: the full path to the lockfile. - def initialize(lockfile) + # * :wait::: should wait for the release of the lock if it can't + # be acquired + def initialize(lockfile, wait = true) @runlock_file = lockfile + @wait = wait @runlock = nil end @@ -57,16 +60,21 @@ class Chef end unless runlock.flock(File::LOCK_EX|File::LOCK_NB) # Another chef client running... - runpid = runlock.read.strip.chomp - Chef::Log.warn("Chef client #{runpid} is running, will wait for it to finish and then run.") - runlock.flock(File::LOCK_EX) + if @wait + runpid = runlock.read.strip.chomp + Chef::Log.warn("Chef client #{runpid} is running, will wait for it to finish and then run.") + runlock.flock(File::LOCK_EX) + end end - # We grabbed the run lock. Save the pid. + end + + def save_pid runlock.truncate(0) runlock.rewind # truncate doesn't reset position to 0. runlock.write(Process.pid.to_s) + runlock.flush # flush the file end - + # Release the system-wide lock. def release if runlock -- cgit v1.2.1