Chapter 5. Creating Handlers
As we learned when exploring the different stages of a Chef run in Anatomy of a Chef Run, Chef executes handlers as specific situations arise during the run to allow us to respond to these events. As we’ll see in this chapter, handlers are special Ruby classes that we incorporate into our Chef runs. Three types of handler are supported:
- Start handlers
- These run before the Chef run has fully begun, during stage 3 (see Load and Build Node Object) and are typically used to notify monitoring systems and the like about the start of a Chef run or to initialize reporting systems that will be used throughout the rest of the run.
- Report handlers
- These run during the last stage of the run (see Finalize) and are triggered when the Chef run has been successful. Report handlers can be created for a variety of tasks, and are typically used to report statistics and data on the successful Chef run to monitoring systems.
- Exception handlers
- These also run during the last stage of the run, but are triggered when the Chef run has failed. Exception handlers are typically used to generate alerts or notifications to inform people that Chef has failed, and provide data on the nature of the failure.
In this chapter, we’ll create a test environment to allow us to safely run code examples before examining the three handler types and how to implement and run each one. We’ll also look at the various Chef classes we’ll make use of to power and enhance the usefulness of our handlers, ...