Skip to content

Commit

Permalink
Raise on user defined max_failures
Browse files Browse the repository at this point in the history
* When the max attempt of max_failures is reached, we usually do nothing
  and move on
* We might want to fail the run instead of waiting forever or ignore the
  failure
* The max_failures is not user defined, we let them define the limit and
  raising if they do

Change-Id: I32719795edb5fbbd081442f54419ff3450e8c5f8
  • Loading branch information
achamo committed Apr 26, 2023
1 parent 90fcf9a commit 2fed84f
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions libraries/primitive_consul_lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def wait_until(action, opts = {})
dc = "(in #{@options[:datacenter]})" if @options[:datacenter]
Chef::Log.info "Will #{action} the lock #{path} #{dc}"
start = Time.now
success = 0.upto(opts[:max_failures] || Float::INFINITY).any? do |tries|
max_failures = opts[:max_failures] || @options[:max_failures]
success = 0.upto(max_failures || Float::INFINITY).any? do |tries|
yield(start, tries) || backoff(start, tries)
rescue StandardError => e
Chef::Log.warn "Error while #{action}-ing lock"
Expand All @@ -85,7 +86,8 @@ def wait_until(action, opts = {})
if success
Chef::Log.info "#{action.to_s.capitalize}ed the lock #{path}"
else
Chef::Log.warn "Will ignore errors and since we've reached #{opts[:max_failures]} errors"
Chef::Log.warn "Will ignore errors and since we've reached #{max_failures} errors"
raise "Max attempt #{max_failures} reached" if max_failures && !opts[:continue]
end
end

Expand All @@ -101,7 +103,7 @@ def register(choregraphie)
# The reason we have to be a bit more relaxed here, is that all
# chef run including a choregraphie with this primitive try to
# release the lock at the end of a successful run
wait_until(:exit, max_failures: 5) { semaphore.exit(name: @options[:id]) }
wait_until(:exit, max_failures: 5, continue: true) { semaphore.exit(name: @options[:id]) }
end
end

Expand Down

0 comments on commit 2fed84f

Please sign in to comment.