Skip to content

Instantly share code, notes, and snippets.

@arangamani
Last active August 29, 2015 13:57
Show Gist options
  • Save arangamani/9490082 to your computer and use it in GitHub Desktop.
Save arangamani/9490082 to your computer and use it in GitHub Desktop.
Chef Handler Example Exception Handling
# attributes
default['cron']['service_name'] = 'cron'
# recipe
include_recipe 'chef_handler'
cookbook_file "#{node['chef_handler']['handler_path']}/blah.rb" do
source 'blah.rb'
action :create
end
chef_handler 'Rightscale::BlahHandler' do
source "#{node['chef_handler']['handler_path']}/blah.rb"
action :enable
end
service node['cron']['service_name'] do
action :stop
end
# The failing resource
execute 'blah'
service node['cron']['service_name'] do
action :start
end
# The handler ruby code (files/default/blah.rb)
module Rightscale
class BlahHandler < Chef::Handler
def report
# Node can be accessed by `run_context.node`
cron_service_name = run_context.node['cron']['service_name']
# Find the resource from the resource collection
cron_resource = run_context.resource_collection.lookup("service[#{cron_service_name}]")
# Run the :start action of the resource found.
cron_resource.run_action(:start) if cron_resource
Chef::Log.error 'Cron service started.'
end
end
end
@caryp
Copy link

caryp commented Mar 11, 2014

cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment