module Garcon::Recovery

Public Class Methods

included(descendant) click to toggle source
# 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

Public Instance Methods

notify(action, resource) click to toggle source
# 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) click to toggle source
# 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