module Serfx::Utils::Handler

helper module to for serf custom handlers

serf agents can be configured to invoke an executable script when an user event is received.

Serfx::Utils::Handler

module provides a set of helper methods to ease

writing ruby based serf event handlers

For example, following script will respond to any qury event named ‘upcase’ and return the uppercase version of the original query event’s payload @example

require 'serfx/utils/handler'
include Serfx::Utils::Handler
on :query, 'upcase' do |event|
  unless event.payload.nil?
    STDOUT.write(event.payload.upcase)
  end
end
run

Constants

SerfCallback

Public Instance Methods

on(type, name = nil, &block) click to toggle source

register a callback against an event

@param type [Symbol] event type for which this handler will be invoked @param name [String, Regex] match against name of the event

# File lib/serfx/utils/handler.rb, line 62
def on(type, name = nil,  &block)
  callbacks[type] << SerfCallback.new(name, block)
  nil
end
run() click to toggle source

execute callbacks registerd using ‘on`

# File lib/serfx/utils/handler.rb, line 68
def run
  event = SerfEvent.new
  callbacks[event.type.downcase.to_sym].each do |cbk|
    if cbk.name
      cbk.block.call(event) if cbk.name === event.name
    else
      cbk.block.call(event)
    end
  end
end

Private Instance Methods

callbacks() click to toggle source
# File lib/serfx/utils/handler.rb, line 83
def callbacks
  @_callbacks ||= Hash.new { |h, k| h[k] = [] }
end