Module: Garcon::Recovery

Defined in:
lib/garcon/chef/provider/recovery.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(descendant) ⇒ Object



50
51
52
53
54
55
# File 'lib/garcon/chef/provider/recovery.rb', line 50

def self.included(descendant)
  descendant.class_eval do
    alias_method :run_action_unrescued, :run_action
    alias_method :run_action, :run_action_rescued
  end
end

Instance Method Details

#notify(action, resource) ⇒ Object



46
47
48
# File 'lib/garcon/chef/provider/recovery.rb', line 46

def notify(action, resource)
  run_context.resource_collection.find(resource).run_action(action)
end

#run_action_rescued(action = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/garcon/chef/provider/recovery.rb', line 22

def run_action_rescued(action = nil)
  run_action_unrescued(action)
  Chef::Log.debug "Finished running #{new_resource.resource_name}" \
                  "[#{new_resource.name}] -- no exception"
rescue Exception => e
  Chef::Log.info "#{new_resource.resource_name}[#{new_resource.name}] " \
                 "failed with: #{e.inspect}"

  if new_resource.instance_variable_defined?('@recovery_handlers'.to_sym)
    new_resource.recovery_handlers.each do |recovery_struct|
      if recovery_struct.options[:retries] > 0 &&
        (recovery_struct.exceptions.any? { |klass| e.is_a?(klass) } ||
         recovery_struct.exceptions.empty?)

        recovery_struct.options[:retries] -= 1
        instance_exec(new_resource, &recovery_struct.block)
        run_action_rescued(action)
        return
      end
    end
  end
  raise exception
end