summaryrefslogtreecommitdiff
path: root/lib/chef/run_lock.rb
diff options
context:
space:
mode:
authorsersut <serdar@opscode.com>2013-10-07 16:29:10 -0700
committersersut <serdar@opscode.com>2013-10-07 16:29:10 -0700
commit3c6fc13e24fef2b38d3fe017cfa54e9378ed4cf3 (patch)
tree1dc24df1817c277ec5b9c13476cae24bb4de51e0 /lib/chef/run_lock.rb
parent5be79cb1396c302bab88cdba36528e70047040cd (diff)
downloadchef-3c6fc13e24fef2b38d3fe017cfa54e9378ed4cf3.tar.gz
Make waiting for the release of the lock optional in RunLock class.
Diffstat (limited to 'lib/chef/run_lock.rb')
-rw-r--r--lib/chef/run_lock.rb20
1 files changed, 14 insertions, 6 deletions
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