Skip to content

Commit

Permalink
(maint) - refactor retry_invoke_dsc_resource
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbreen28 committed Feb 8, 2024
1 parent 9721f08 commit afe7193
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
30 changes: 11 additions & 19 deletions lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'pathname'
require 'json'

class Puppet::Provider::DscBaseProvider # rubocop:disable Metrics/ClassLength
class Puppet::Provider::DscBaseProvider
# Initializes the provider, preparing the instance variables which cache:
# - the canonicalized resources across calls
# - query results
Expand Down Expand Up @@ -299,24 +299,16 @@ def retry_invoke_dsc_resource(context, max_retry_count, retry_wait_interval_secs
# notify and retry
context.notice("Retrying: attempt #{try} of #{max_retry_count}.")
data = JSON.parse(yield)
# if no error, break
if data['errormessage'].nil?
break
# check if error matches error matcher supplied
elsif data['errormessage'].match?(error_matcher)
# if last attempt, return error
if try == max_retry_count
context.notice("Attempt #{try} of #{max_retry_count} failed. No more retries.")
# all attempts failed, raise error
return context.err(data['errormessage'])
end
# if not last attempt, notify, continue and retry
context.notice("Attempt #{try} of #{max_retry_count} failed.")
next
else
# if we get an unexpected error, return
return context.err(data['errormessage'])
end
# if no error, assume successful invocation and break
break if data['errormessage'].nil?

# notify of failed retry
context.notice("Attempt #{try} of #{max_retry_count} failed.")
# return if error does not match expceted error, or all retries exhausted
return context.err(data['errormessage']) unless data['errormessage'].match?(error_matcher) && try < max_retry_count

# else, continue
next
end
data
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,9 +812,9 @@
.exactly(5).times
expect(context).to receive(:notice).with(/Invoke-DscResource collision detected: Please stagger the timing of your Puppet runs as this can lead to unexpected behaviour./).once
expect(context).to receive(:notice).with('Sleeping for 60 seconds.').exactly(5).times
expect(context).to receive(:notice).with(/Retrying: attempt [1-6] of 5/).exactly(5).times
expect(context).to receive(:notice).with(/Retrying: attempt [1-5] of 5/).exactly(5).times
expect(ps_manager).to receive(:execute).and_return({ stdout: '{"errormessage": "The Invoke-DscResource cmdlet is in progress and must return before Invoke-DscResource can be invoked"}' })
expect(context).to receive(:notice).with(/Attempt [1-6] of 5 failed/).exactly(5).times
expect(context).to receive(:notice).with(/Attempt [1-5] of 5 failed/).exactly(5).times
expect(context).to receive(:err).with(/The Invoke-DscResource cmdlet is in progress and must return before Invoke-DscResource can be invoked/)
allow(provider).to receive(:sleep)
expect(result).to be_nil
Expand All @@ -827,6 +827,7 @@
expect(context).to receive(:notice).with(/Retrying: attempt 1 of 5/).once
allow(provider).to receive(:sleep)
expect(ps_manager).to receive(:execute).and_return({ stdout: '{"errormessage": "Some unexpected error"}' }).once
expect(context).to receive(:notice).with(/Attempt 1 of 5 failed/).once
expect(context).to receive(:err).with(/Some unexpected error/)
expect(result).to be_nil
end
Expand Down

0 comments on commit afe7193

Please sign in to comment.