Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added the amqp_subscriber daemon

  • Loading branch information...
commit fda659c8f3b6daad06caad21b659cdc053e20fc4 1 parent 6839f96
Paul Smith elandesign authored
1  .gitignore
View
@@ -1,6 +1,7 @@
.bundle
db/*.sqlite3
log/*.log
+log/*.output
tmp/**/*
tmp/*
config/config.yml
4 Gemfile
View
@@ -4,8 +4,12 @@ gem 'rails', '3.2.6'
gem 'nokogiri'
gem 'mongoid', '~> 2.4.10'
+
gem 'tire'
+gem 'bunny'
+gem 'daemons'
+
gem 'haml'
gem 'htmlentities', "~> 4.3.0"
3  Gemfile.lock
View
@@ -39,6 +39,7 @@ GEM
bson (1.3.1)
bson_ext (1.3.1)
builder (3.0.0)
+ bunny (0.8.0)
capybara (1.1.2)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
@@ -287,7 +288,9 @@ DEPENDENCIES
actionmailer_inline_css (~> 1.3.0)
bson (= 1.3.1)
bson_ext (= 1.3.1)
+ bunny
capybara
+ daemons
database_cleaner (~> 0.6.0)
debugger
devise (~> 1.5.3)
4 config/config.example.yml
View
@@ -43,6 +43,10 @@ allow_comments_with_issue_tracker: true
elasticsearch_enabled: true
elasticsearch_url: http://localhost:9200
+# Receive notices via AMQP and push them to the endpoint
+amqp_uri: amqp://guest:guest@localhost:5672/airbrake
+amqp_endpoint: http://errbit.example.com/notifier_api/v2/notices
+
# Setup your deploy options for capistrano.
deployment:
hosts:
58 lib/amqp/subscriber.rb
View
@@ -0,0 +1,58 @@
+require 'uri'
+require 'bunny'
+require 'net/http'
+
+module Amqp
+ class Subscriber
+ attr_reader :user, :password, :host, :port, :exchange_name, :queue_name
+
+ def initialize(options = {})
+ uri = URI.parse(options[:uri].to_s)
+ @user = uri.user || 'guest'
+ @password = uri.password || 'guest'
+ @host = uri.host || 'localhost'
+ @port = uri.port || 5672
+ @exchange_name = uri.path.gsub(/^\//, '')
+ @exchange_name = 'airbrake' if @exchange_name == ''
+ @queue_name = options[:queue] || 'errbit'
+ end
+
+ def subscribe(endpoint)
+ uri = URI.parse(endpoint)
+
+ queue.subscribe do |message|
+ # In an ideal world, we'd load the app environment and
+ # handle the message here, but Mongoid doesn't seem to like
+ # what Bunny does to its thread. Instead, let's push it
+ # onto the http endpoint.
+ # App.report_error!(message[:payload])
+ begin
+ request = Net::HTTP::Post.new(uri.path)
+ request.body = message[:payload]
+ response = Net::HTTP.start(uri.host, uri.port) {|http| http.request(request)}
+ rescue
+ end
+ end
+ end
+
+ private
+
+ def bunny
+ return @bunny if defined?(@bunny)
+ @bunny = Bunny.new(:host => host, :port => port, :user => user, :password => password)
+ @bunny.start
+ @bunny
+ end
+
+ def exchange
+ @exchange ||= bunny.exchange(exchange_name)
+ end
+
+ def queue
+ return @queue if defined?(@queue)
+ @queue = bunny.queue(queue_name)
+ @queue.bind(exchange)
+ @queue
+ end
+ end
+end
23 script/amqp_subscriber
View
@@ -0,0 +1,23 @@
+#!/usr/bin/env ruby
+
+require 'rubygems'
+require 'daemons'
+require 'yaml'
+
+ROOT_PATH = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+require File.join(ROOT_PATH, 'lib', 'amqp', 'subscriber')
+
+daemon_settings = {
+ :monitor => true,
+ :dir_mode => :normal,
+ :dir => File.join(ROOT_PATH, 'tmp', 'pids'),
+ :log_dir => File.join(ROOT_PATH, 'log'),
+ :log_output => true,
+ :multiple => false
+}
+
+config = YAML.load_file(File.join(ROOT_PATH, 'config', 'config.example.yml')).merge(YAML.load_file(File.join(ROOT_PATH, 'config', 'config.yml')))
+
+Daemons.run_proc('amqp-subscriber', daemon_settings) do
+ Amqp::Subscriber.new(:uri => config['amqp_uri']).subscribe(config['amqp_endpoint'])
+end
Please sign in to comment.
Something went wrong with that request. Please try again.