Sha256: 228d2b6aafc3612fcce7d0c5e83e72160aea81f496a820183ede87c5b749240f

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

require 'bundler/setup'
require 'jsonical'
require 'datadog/statsd'
require 'bugsnag'

# For display log
# http://stackoverflow.com/questions/8717198/foreman-only-shows-line-with-started-wit-pid-and-nothing-else
$stdout.sync = true

statsd = Datadog::Statsd.new(
  'localhost',
  8125,
  namespace: 'jsonical',
  tag: 'production:true'
)

def symbolize_keys(object)
  return object unless object.is_a?(Hash)

  object.inject({}) do |memo, (key, value)|
    memo[key.to_sym] = symbolize_keys(value)
    memo
  end
end

begin
  conn = JSONiCal.broker

  channel = conn.create_channel
  x = channel.direct('jsonical.vevent')
  queue = channel.queue('jsonical.vevent', durable: true, auto_delete: false)

  JSONiCal::BINDED_EVENT.each do |event_name|
    queue.bind(x, routing_key: event_name)
  end

  queue.subscribe(manual_ack: true, block: true) do |di, _, message|
    begin
      JSONiCal
        .logger
        .info("Event recived on #{di.routing_key}, with message: #{message}")

      statsd.increment("vevent.#{di.routing_key}")

      statsd.time('vevent.treatment') do
        JSONiCal::VEVENTDispatcher.new(
          key: di.routing_key,
          message: symbolize_keys(Oj.load(message))
        ).call
      end
    rescue Exception => e
      statsd.increment('vevent.treatment_error')

      JSONiCal.logger.error([e.class, e.message, e.backtrace].join("\n"))

      Bugsnag.notify(e) do |notification|
        notification.severity = 'error'
        notification.context = 'message treatment'
        notification.add_tab(:payload, raw: message)
      end

      raise e
    ensure
      channel.ack(di.delivery_tag)
    end
  end
ensure
  conn.close
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
JSONiCal-1.0.0 app/listener.rb