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