Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to raise on failure #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libraries/choregraphie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def self.ensure_whyrun_supported(run_context, resource_name, ignore_missing_reso
begin
resource = run_context.resource_collection.find(resource_name)
rescue Chef::Exceptions::ResourceNotFound
if ignore_missing_resource # rubocop:disable Style/GuardClause
if ignore_missing_resource
# some resources are defined only when used
# so we ignore them
return
Expand Down
10 changes: 7 additions & 3 deletions libraries/primitive_consul_lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def initialize(options = {})
raise ArgumentError, "You can't set both concurrency and service" if @options[:concurrency] && @options[:service]

@options[:backoff] ||= 5 # seconds
@options[:raise_on_failures] = false

ConsulCommon.setup_consul(@options)

Expand Down Expand Up @@ -74,7 +75,9 @@ 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]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OPT: || Float::INFINITY could be added on this line

raise_on_failures = opts[:raise_on_failures].nil? ? @options[:raise_on_failures] : opts[:raise_on_failures]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: the .fetch method is great for that e.g. opts.fetch(:raise_on_failures, @options[:raise_on_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 +88,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 && raise_on_failures
end
end

Expand All @@ -101,7 +105,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, raise_on_failures: false) { semaphore.exit(name: @options[:id]) }
end
end

Expand Down
4 changes: 2 additions & 2 deletions libraries/primitive_consul_maintenance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def maintenance?
# we observed in very rare cases that maintenance status could be not up to date
# for ~5s after a restart
results = 3.times.map do
checks = Diplomat::Agent.checks()
checks = Diplomat::Agent.checks
maint_status = checks.dig(@maintenance_key, 'Status')
sleep(@options[:check_interval])
maint_status == 'critical' && !checks.dig(@maintenance_key, 'Notes').nil?
Expand All @@ -40,7 +40,7 @@ def maintenance?

# @return [String, nil] reason of the maintenance, if any. Nil otherwise
def maintenance_reason
checks = Diplomat::Agent.checks()
checks = Diplomat::Agent.checks
checks.dig(@maintenance_key, 'Notes')
end

Expand Down
2 changes: 1 addition & 1 deletion libraries/primitive_consul_rack_lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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[:rack], server: @options[:id]) }
wait_until(:exit, max_failures: 5, raise_on_failures: false) { semaphore.exit(name: @options[:rack], server: @options[:id]) }
end
end

Expand Down