class Flapjack::Gateways::Jabber::Notifier

Attributes

siblings[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/flapjack/gateways/jabber.rb, line 37
def initialize(options = {})
  @lock = options[:lock]
  @config = options[:config]

  # TODO support for config reloading
  @queue = Flapjack::RecordQueue.new(@config['queue'] || 'jabber_notifications',
             Flapjack::Data::Alert)
end

Public Instance Methods

start() click to toggle source
# File lib/flapjack/gateways/jabber.rb, line 46
def start
  begin
    Zermelo.redis = Flapjack.redis

    loop do
      @lock.synchronize do
        @queue.foreach {|alert| handle_alert(alert) }
      end

      @queue.wait
    end
  ensure
    Flapjack.redis.quit
  end
end
stop_type() click to toggle source
# File lib/flapjack/gateways/jabber.rb, line 62
def stop_type
  :exception
end

Private Instance Methods

handle_alert(alert) click to toggle source
# File lib/flapjack/gateways/jabber.rb, line 68
def handle_alert(alert)
  @bot ||= @siblings && @siblings.detect {|sib| sib.respond_to?(:announce) }

  if @bot.nil?
    Flapjack.logger.warn("jabber bot not running, won't announce")
    return
  end

  check = alert.check
  check_name = check.name

  address = alert.address
  state = alert.state

  Flapjack.logger.debug("processing jabber notification address: #{address}, " +
                "check: '#{check_name}', state: #{state}, summary: #{alert.summary}")

  # event_count = alert.event_count

  @ack_str = if state.eql?('ok') || ['test', 'acknowledgement'].include?(alert.type)
    nil
  else
    "#{@bot.alias}: ACKID #{alert.event_hash}"
  end

  message_type = alert.rollup ? 'rollup' : 'alert'

  message_template_erb, message_template =
    load_template(@config['templates'], message_type,
      'text', File.join(File.dirname(__FILE__), 'jabber'))

  @alert = alert
  bnd = binding

  message = nil
  begin
    message = message_template_erb.result(bnd).chomp
  rescue
    Flapjack.logger.error "Error while executing the ERB for a jabber message, " +
      "ERB being executed: #{message_template}"
    raise
  end

  # FIXME: should also check if presence has been established in any group chat rooms that are
  # configured before starting to process events, otherwise the first few may get lost (send
  # before joining the group chat rooms)
  @bot.announce(address, message)
end