Skip to content

Commit 9203b8d

Browse files
authored
Merge pull request #289 from puppetlabs/maint-refactor_retry_method
(maint) - refactor retry_invoke_dsc_resource
2 parents 054967c + af92848 commit 9203b8d

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -310,24 +310,16 @@ def retry_invoke_dsc_resource(context, max_retry_count, retry_wait_interval_secs
310310
# notify and retry
311311
context.notice("Retrying: attempt #{try} of #{max_retry_count}.")
312312
data = JSON.parse(yield)
313-
# if no error, break
314-
if data['errormessage'].nil?
315-
break
316-
# check if error matches error matcher supplied
317-
elsif data['errormessage'].match?(error_matcher)
318-
# if last attempt, return error
319-
if try == max_retry_count
320-
context.notice("Attempt #{try} of #{max_retry_count} failed. No more retries.")
321-
# all attempts failed, raise error
322-
return context.err(data['errormessage'])
323-
end
324-
# if not last attempt, notify, continue and retry
325-
context.notice("Attempt #{try} of #{max_retry_count} failed.")
326-
next
327-
else
328-
# if we get an unexpected error, return
329-
return context.err(data['errormessage'])
330-
end
313+
# if no error, assume successful invocation and break
314+
break if data['errormessage'].nil?
315+
316+
# notify of failed retry
317+
context.notice("Attempt #{try} of #{max_retry_count} failed.")
318+
# return if error does not match expceted error, or all retries exhausted
319+
return context.err(data['errormessage']) unless data['errormessage'].match?(error_matcher) && try < max_retry_count
320+
321+
# else, retry
322+
next
331323
end
332324
data
333325
end

spec/unit/puppet/provider/dsc_base_provider/dsc_base_provider_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,9 +814,9 @@
814814
.exactly(5).times
815815
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
816816
expect(context).to receive(:notice).with('Sleeping for 60 seconds.').exactly(5).times
817-
expect(context).to receive(:notice).with(/Retrying: attempt [1-6] of 5/).exactly(5).times
817+
expect(context).to receive(:notice).with(/Retrying: attempt [1-5] of 5/).exactly(5).times
818818
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"}' })
819-
expect(context).to receive(:notice).with(/Attempt [1-6] of 5 failed/).exactly(5).times
819+
expect(context).to receive(:notice).with(/Attempt [1-5] of 5 failed/).exactly(5).times
820820
expect(context).to receive(:err).with(/The Invoke-DscResource cmdlet is in progress and must return before Invoke-DscResource can be invoked/)
821821
allow(provider).to receive(:sleep)
822822
expect(result).to be_nil
@@ -829,6 +829,7 @@
829829
expect(context).to receive(:notice).with(/Retrying: attempt 1 of 5/).once
830830
allow(provider).to receive(:sleep)
831831
expect(ps_manager).to receive(:execute).and_return({ stdout: '{"errormessage": "Some unexpected error"}' }).once
832+
expect(context).to receive(:notice).with(/Attempt 1 of 5 failed/).once
832833
expect(context).to receive(:err).with(/Some unexpected error/)
833834
expect(result).to be_nil
834835
end

0 commit comments

Comments
 (0)