class Denouncer::Notifiers::AmqpNotifier

Constants

DEFAULT_PASSWORD
DEFAULT_PORT
DEFAULT_SERVER
DEFAULT_USERNAME
DEFAULT_VHOST

Public Instance Methods

info(info_message, metadata = nil) click to toggle source

Sends a info notification.

@param info_message [String] @param metadata [Hash]

# File lib/denouncer/notifiers/amqp_notifier.rb, line 44
def info(info_message, metadata = nil)
  msg = generate_info_hash(info_message, metadata).to_json
  send_message_via_amqp msg
end
name() click to toggle source

@return [String]

# File lib/denouncer/notifiers/amqp_notifier.rb, line 14
def name
  'amqp'
end
notify(error, metadata = nil) click to toggle source

Sends an error notification via amqp.

@param error [StandardError] @param metadata [Hash]

# File lib/denouncer/notifiers/amqp_notifier.rb, line 35
def notify(error, metadata = nil)
  msg = generate_error_hash(error, metadata).to_json
  send_message_via_amqp msg
end
set_configuration!(options) click to toggle source
# File lib/denouncer/notifiers/amqp_notifier.rb, line 18
def set_configuration!(options)
  raise "Configuration error: :application_name is not set!" if options[:application_name].nil?
  require 'bunny'

  options[:server] = DEFAULT_SERVER if options[:server].nil?
  options[:port] = DEFAULT_PORT if options[:port].nil?
  options[:vhost] = DEFAULT_VHOST if options[:vhost].nil?
  options[:username] = DEFAULT_USERNAME if options[:username].nil?
  options[:password] = DEFAULT_PASSWORD if options[:password].nil?
  options[:message_queue] = "#{options[:application_name]}.errors" if options[:message_queue].nil?
  return options
end

Private Instance Methods

generate_error_hash(error, metadata = nil) click to toggle source
# File lib/denouncer/notifiers/amqp_notifier.rb, line 51
def generate_error_hash(error, metadata = nil)
  hostname = Socket.gethostname
  time_now = get_current_timestamp
  {
    notification_time: time_now,
    application_name: config[:application_name],
    hostname: hostname,
    error_class: error.class.name,
    error_backtrace: error.backtrace,
    error_message: error.message,
    error_cause: get_error_cause(error),
    metadata: metadata
  }
end
generate_info_hash(message, metadata = nil) click to toggle source
# File lib/denouncer/notifiers/amqp_notifier.rb, line 66
def generate_info_hash(message, metadata = nil)
  hostname = Socket.gethostname
  time_now = get_current_timestamp
  {
    notification_time: time_now,
    application_name: config[:application_name],
    hostname: hostname,
    message: message,
    metadata: metadata
  }
end
send_message_via_amqp(message) click to toggle source
# File lib/denouncer/notifiers/amqp_notifier.rb, line 78
def send_message_via_amqp(message)
  # Start a communication session with RabbitMQ
  connection_hash = {
    host: config[:server],
    vhost: config[:vhost],
    port: config[:port],
    username: config[:username],
    password: config[:password],
    threaded: false
  }
  conn = Bunny.new connection_hash
  conn.start

  # open a channel
  ch = conn.create_channel

  # declare a queue
  q  = ch.queue(config[:message_queue])

  # publish a message to the default exchange which then gets routed to this queue
  q.publish(message)

  # close the connection
  conn.stop
end