-
Notifications
You must be signed in to change notification settings - Fork 52
Making your own trigger
A Bluepill trigger is used to monitor the meta-state of a process (eg. flapping detection is built using a trigger). Triggers are notified whenever a process transitions from one state to another, and triggers can act on a process by dispatching events.
To create your own trigger, simply create a class and inherit from Bluepill::Trigger. You also need to implement at least one method: notify(transition)
class EmailNotifier < Bluepill::Trigger
def notify(transition)
#do something about this transition
end
end
The transition object passed has two methods you can call on it to get more information: to_name and from_name, the destination and origin states respectively. The states can be one of the following symbols:
[:unmonitored, :up, :down, :starting, :stopping, :restarting]
You can optionally implement an initialize method whose signature is as follows:
def initialize(process, options = {})
#do something with the options
super # be sure to call super so Bluepill::Trigger can capture the options it needs
end
The parent class will capture the process object and it will be accessible via the process accessor. It also provides an accessor for logger which you can call info(msg), debug(msg) or warning(msg) to log messages at their respective log levels.
Bluepill::Trigger also provides a few methods for acting on the process:
- dispatch!(event): Immediately dispatch one of the following events: [:start, :stop, :unmonitor, :restart]
- schedule_event(event, delay): Schedule an event to be dispatched in delay seconds.
- cancel_all_events: Cancel all scheduled events that have not fired yet.
To use a trigger you've created, you simply define it in your config file and then attach it to a process like you would a process condition:
process.checks :my_trigger, :option_1 => :value, :option_2 => :other_value
All options passed are passed to your initialize method.
Here is an example implementation of an EmailNotifier trigger:
class EmailNotifier < Bluepill::Trigger
def initialize(process, options = {})
@email = options.delete(:email)
@notify_on = options.delete(:notify_on)
super
end
def notify(transition)
if @notify_on.include?(transition.to_name)
IO.popen("sendmail -t", "w") do |x|
x.puts "To: #{@email}"
#your other mail headers
x.puts
x.puts "Your process #{self.process.name} has restarted at #{Time.now}"
end
end
end
end
Flapping detection causes your process to transition to :unmonitored
, so to notify on flapping you'll want to pass :notify_on => :unmonitored