Class: Terraspace::Terraform::Runner::Retryer
Instance Method Summary
collapse
#pretty_path, #pretty_time
#logger
Constructor Details
#initialize(mod, options, command_name, exception) ⇒ Retryer
Returns a new instance of Retryer.
6
7
8
9
|
# File 'lib/terraspace/terraform/runner/retryer.rb', line 6
def initialize(mod, options, command_name, exception)
@mod, @options, @command_name, @exception = mod, options, command_name, exception
@retries = 1
end
|
Instance Method Details
#init_required_error ⇒ Object
48
49
50
51
52
53
|
# File 'lib/terraspace/terraform/runner/retryer.rb', line 48
def init_required_error
logger.info "Terraform reinitialization required detected. Will run `terraform init` and try again."
logger.debug "Retry attempt: #{@retries}"
logger.debug "#{@exception.class}"
reinit
end
|
#purge_caches ⇒ Object
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/terraspace/terraform/runner/retryer.rb', line 59
def purge_caches
dir = "#{@mod.cache_dir}/.terraform"
logger.info "Purging #{pretty_path(dir)}"
FileUtils.rm_rf(dir)
dir = "#{Terraspace.config.terraform.plugin_cache.dir}"
logger.info "Purging #{pretty_path(dir)}"
FileUtils.rm_rf(dir)
FileUtils.mkdir_p(dir) end
|
#reinit ⇒ Object
55
56
57
|
# File 'lib/terraspace/terraform/runner/retryer.rb', line 55
def reinit
Terraspace::Terraform::Runner.new("init", @options).run unless @command_name == "init"
end
|
#retry? ⇒ Boolean
11
12
13
14
15
16
17
18
19
|
# File 'lib/terraspace/terraform/runner/retryer.rb', line 11
def retry?
max_retries = ENV['TS_MAX_RETRIES'] ? ENV['TS_MAX_RETRIES'].to_i : 1
if @retries <= max_retries && !@stop_retrying
true else
logger.info "ERROR after max retries #{max_retries}: #{@exception.message}"
false end
end
|
#run ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/terraspace/terraform/runner/retryer.rb', line 21
def run
backoff = 2 ** @retries logger.debug "Waiting #{backoff}s before retrying"
sleep(backoff)
@retries += 1
case @exception
when Terraspace::SharedCacheError
shared_cache_error
when Terraspace::InitRequiredError
init_required_error
end
end
|
#shared_cache_error ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/terraspace/terraform/runner/retryer.rb', line 35
def shared_cache_error
logger.info "Terraform Shared Cache error detected. Will purge caches and run `terraform init` to try again."
logger.debug "Retry attempt: #{@retries}"
logger.debug "#{@exception.class}"
logger.debug "#{@exception.message}"
if Terraspace.config.terraform.plugin_cache.purge_on_error purge_caches
reinit
else
@stop_retrying = true
end
end
|