diff options
author | sersut <serdar@opscode.com> | 2013-10-07 16:29:10 -0700 |
---|---|---|
committer | sersut <serdar@opscode.com> | 2013-10-07 16:29:10 -0700 |
commit | 3c6fc13e24fef2b38d3fe017cfa54e9378ed4cf3 (patch) | |
tree | 1dc24df1817c277ec5b9c13476cae24bb4de51e0 | |
parent | 5be79cb1396c302bab88cdba36528e70047040cd (diff) | |
download | chef-3c6fc13e24fef2b38d3fe017cfa54e9378ed4cf3.tar.gz |
Make waiting for the release of the lock optional in RunLock class.
-rw-r--r-- | lib/chef/client.rb | 1 | ||||
-rw-r--r-- | lib/chef/run_lock.rb | 20 | ||||
-rw-r--r-- | spec/unit/client_spec.rb | 1 |
3 files changed, 16 insertions, 6 deletions
diff --git a/lib/chef/client.rb b/lib/chef/client.rb index 6863dc7691..04d6799988 100644 --- a/lib/chef/client.rb +++ b/lib/chef/client.rb @@ -475,6 +475,7 @@ class Chef runlock.acquire # don't add code that may fail before entering this section to be sure to release lock begin + runlock.save_pid run_context = nil @events.run_start(Chef::VERSION) Chef::Log.info("*** Chef #{Chef::VERSION} ***") 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 diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index a3b5c6ec08..67eb97f5c2 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -213,6 +213,7 @@ shared_examples_for Chef::Client do mock_chef_rest_for_node_save.should_receive(:put_rest).with("nodes/#{@fqdn}", @node).and_return(true) Chef::RunLock.any_instance.should_receive(:acquire) + Chef::RunLock.any_instance.should_receive(:save_pid) Chef::RunLock.any_instance.should_receive(:release) # Post conditions: check that node has been filled in correctly |