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 21, 2023
1 parent 90fcf9a commit d19b5b3
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion libraries/primitive_consul_lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def human_duration(duration_in_secs)

def wait_until(action, opts = {})
dc = "(in #{@options[:datacenter]})" if @options[:datacenter]
opts[:max_failures] ||= @options[:max_failures]
Chef::Log.info "Will #{action} the lock #{path} #{dc}"
start = Time.now
success = 0.upto(opts[:max_failures] || Float::INFINITY).any? do |tries|
Expand All @@ -86,6 +87,7 @@ def wait_until(action, opts = {})
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"
raise "Max attempt #{options[:max_failures]} reached" if @options[: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 d19b5b3

Please sign in to comment.