Class: Chef::Handler
- Inherits:
-
Object
- Object
- Chef::Handler
- Extended by:
- Forwardable
- Defined in:
- lib/chef/handler.rb,
lib/chef/handler/json_file.rb,
lib/chef/handler/error_report.rb
Overview
The base class for an Exception or Notification Handler. Create your own handler by subclassing Chef::Handler. When a Chef run fails with an uncaught Exception, Chef will set the run_status
on your handler and call report
Direct Known Subclasses
Defined Under Namespace
Classes: ErrorReport, JsonFile
Instance Attribute Summary collapse
-
#run_status ⇒ Object
readonly
The Chef::RunStatus object containing data about the Chef run.
Class Method Summary collapse
-
.exception_handlers ⇒ Object
The list of currently configured exception handlers.
- .handler_for(*args) ⇒ Object
-
.report_handlers ⇒ Object
The list of currently configured report handlers.
- .resolve_handler_instance(handler) ⇒ Object
-
.run_exception_handlers(run_status) ⇒ Object
Run the exception handlers.
-
.run_report_handlers(run_status) ⇒ Object
Run the report handlers.
-
.run_start_handlers(run_status) ⇒ Object
Run the start handlers.
-
.start_handlers ⇒ Object
The list of currently configured start handlers.
Instance Method Summary collapse
-
#data ⇒ Object
Return the Hash representation of the run_status.
-
#report ⇒ Object
The main entry point for report handling.
-
#run_report_safely(run_status) ⇒ Object
Runs the report handler, rescuing and logging any errors it may cause.
-
#run_report_unsafe(run_status) ⇒ Object
Runs the report handler without any error handling.
Instance Attribute Details
#run_status ⇒ Object (readonly)
The Chef::RunStatus object containing data about the Chef run.
164 165 166 |
# File 'lib/chef/handler.rb', line 164 def run_status @run_status end |
Class Method Details
.exception_handlers ⇒ Object
The list of currently configured exception handlers
137 138 139 |
# File 'lib/chef/handler.rb', line 137 def self.exception_handlers Array(Chef::Config[:exception_handlers]) end |
.handler_for(*args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/chef/handler.rb', line 58 def self.handler_for(*args) if args.include?(:start) Chef::Config[:start_handlers] ||= [] Chef::Config[:start_handlers] |= [self] end if args.include?(:report) Chef::Config[:report_handlers] ||= [] Chef::Config[:report_handlers] |= [self] end if args.include?(:exception) Chef::Config[:exception_handlers] ||= [] Chef::Config[:exception_handlers] |= [self] end end |
.report_handlers ⇒ Object
The list of currently configured report handlers
111 112 113 |
# File 'lib/chef/handler.rb', line 111 def self.report_handlers Array(Chef::Config[:report_handlers]) end |
.resolve_handler_instance(handler) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/chef/handler.rb', line 78 def self.resolve_handler_instance(handler) if handler.is_a?(Class) if handler.respond_to?(:instance) # support retrieving a Singleton reporting object handler.instance else # just a class with no way to insert data handler.new end else # the Chef::Config array contains an instance, not a class handler end end |
.run_exception_handlers(run_status) ⇒ Object
Run the exception handlers. Usually will be called by a notification from Chef::Client when the run fails.
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/chef/handler.rb', line 143 def self.run_exception_handlers(run_status) events = run_status.events events.handlers_start(exception_handlers.size) Chef::Log.error("Running exception handlers") exception_handlers.each do |handler| handler = resolve_handler_instance(handler) handler.run_report_safely(run_status) events.handler_executed(handler) end events.handlers_completed Chef::Log.error("Exception handlers complete") end |
.run_report_handlers(run_status) ⇒ Object
Run the report handlers. This will usually be called by a notification from Chef::Client
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/chef/handler.rb', line 117 def self.run_report_handlers(run_status) events = run_status.events events.handlers_start(report_handlers.size) Chef::Log.info("Running report handlers") report_handlers.each do |handler| handler = resolve_handler_instance(handler) handler.run_report_safely(run_status) events.handler_executed(handler) end events.handlers_completed Chef::Log.info("Report handlers complete") end |
.run_start_handlers(run_status) ⇒ Object
Run the start handlers. This will usually be called by a notification from Chef::Client
95 96 97 98 99 100 101 102 |
# File 'lib/chef/handler.rb', line 95 def self.run_start_handlers(run_status) Chef::Log.info("Running start handlers") start_handlers.each do |handler| handler = resolve_handler_instance(handler) handler.run_report_safely(run_status) end Chef::Log.info("Start handlers complete.") end |
Instance Method Details
#data ⇒ Object
Return the Hash representation of the run_status
260 261 262 |
# File 'lib/chef/handler.rb', line 260 def data @run_status.to_h end |
#report ⇒ Object
The main entry point for report handling. Subclasses should override this method with their own report handling logic.
237 |
# File 'lib/chef/handler.rb', line 237 def report; end |
#run_report_safely(run_status) ⇒ Object
Runs the report handler, rescuing and logging any errors it may cause. This ensures that all handlers get a chance to run even if one fails. This method should not be overridden by subclasses unless you know what you’re doing.
243 244 245 246 247 248 249 250 |
# File 'lib/chef/handler.rb', line 243 def run_report_safely(run_status) run_report_unsafe(run_status) rescue Exception => e Chef::Log.error("Report handler #{self.class.name} raised #{e.inspect}") Array(e.backtrace).each { |line| Chef::Log.error(line) } ensure @run_status = nil end |
#run_report_unsafe(run_status) ⇒ Object
Runs the report handler without any error handling. This method should not be used directly except in testing.
254 255 256 257 |
# File 'lib/chef/handler.rb', line 254 def run_report_unsafe(run_status) @run_status = run_status report end |