Skip to content

Commit

Permalink
(CAT-1869) - Add configurable dsc_timeout
Browse files Browse the repository at this point in the history
This commit introduces a new instance variable @timeout, set
to nil by default. A new method set_dsc_timeout is added to
handle the setting of @timeout based on the name_hash[:dsc_timeout]
value. This timeout specifies the number of seconds the ps_manager will
await the dsc invocation to return, before moving on.

The ps_manager.execute method call now includes the timeout value.
The error message for when output is nil has been updated to include
information about the timeout, if it is set.

Does not affect previous behaviour, therefore is completely backwards
compatible with previous versions.
  • Loading branch information
jordanbreen28 committed Jun 10, 2024
1 parent b78625a commit 9a4a837
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def initialize
@cached_query_results = []
@cached_test_results = []
@logon_failures = []
@timeout = nil # default timeout, ps_manager.execute is expecting nil by default..
super
end

Expand Down Expand Up @@ -251,13 +252,17 @@ def invoke_dsc_resource(context, name_hash, props, method)
context.err('Logon credentials are invalid')
return nil
end
specify_dsc_timeout(name_hash)
resource = invocable_resource(props, context, method)
script_content = ps_script_content(resource)
context.debug("Invoke-DSC Timeout: #{@timeout} milliseconds") if @timeout
context.debug("Script:\n #{redact_secrets(script_content)}")
output = ps_manager.execute(remove_secret_identifiers(script_content))[:stdout]
output = ps_manager.execute(remove_secret_identifiers(script_content), @timeout)[:stdout]

if output.nil?
context.err('Nothing returned')
message = 'Nothing returned.'
message += " There is a timeout of #{@timeout} milliseconds set, ensure the DSC resource has enough time to apply." unless @timeout.nil?
context.err(message)
return nil
end

Expand Down Expand Up @@ -295,6 +300,18 @@ def invoke_dsc_resource(context, name_hash, props, method)
data
end

# Sets the @timeout instance variable.
# @param name_hash [Hash] the hash of namevars to be passed as properties to `Invoke-DscResource`
# The @timeout variable is set to the value of name_hash[:dsc_timeout] in milliseconds
# If name_hash[:dsc_timeout] is nil, @timeout is not changed.
# If @timeout is already set to a value other than nil,
# it is changed only if it's different from name_hash[:dsc_timeout]..
def specify_dsc_timeout(name_hash)
return unless name_hash[:dsc_timeout] && (@timeout.nil? || @timeout != name_hash[:dsc_timeout])

@timeout = name_hash[:dsc_timeout] * 1000
end

# Retries Invoke-DscResource when returned error matches error regex supplied as param.
# @param context [Object] the Puppet runtime context to operate in and send feedback to
# @param max_retry_count [Int] max number of times to retry Invoke-DscResource
Expand Down

0 comments on commit 9a4a837

Please sign in to comment.