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

Add an option to raise on failure #71

wants to merge 2 commits into from

Conversation

achamo
Copy link
Contributor

@achamo achamo commented Apr 21, 2023

  • 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

* Please the linter

Change-Id: Iff1fca422e80b7d4a9c451ebcd92b0299d8cae7e
@achamo achamo force-pushed the raise_on_failure branch 3 times, most recently from 923326d to d19b5b3 Compare April 21, 2023 16:06
@achamo achamo requested a review from Annih April 25, 2023 16:30
@@ -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]
Copy link
Contributor

Choose a reason for hiding this comment

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

You are updating the users' input, this is not super great in term of side effect. Could you store it in a local variable?

@@ -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]
Copy link
Contributor

Choose a reason for hiding this comment

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

You are mixing @options & options here (use a local variable :D)

@@ -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]
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

@@ -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]
Copy link
Contributor

Choose a reason for hiding this comment

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

OPT: For max_failures you now have a global option (@options) but you didn't do the same for continue is it expected? Maybe add a global option.

Mandatory: the implementation used to not raise, now it will fail except if the option is specified, this is a breaking change, for a not super critical feature.
I think you should either invert the flag fail_on_max_failures (default false) or configure a default value of true to your new continue option.

OPT: document this new feature somehow

NIT: continue is not super explicit continue_on_failure would longer but be more explicit.

Tip: If you add a global option you could define a local variable as you did for max_failures, it would also give you a trivial opportunity to define the default value to true

* 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
@@ -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]
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])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants