Skip to content

Instantly share code, notes, and snippets.

@idlecool
Created February 2, 2014 16:36
Show Gist options
  • Select an option

  • Save idlecool/8771056 to your computer and use it in GitHub Desktop.

Select an option

Save idlecool/8771056 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# MongoDB endpoint for RabbitMQ
# for logging
require 'bunny'
require 'mongo'
require 'json'
include Mongo
queue_name = database_name = collection_name = "analytics"
puts "Initiating RabbitMQ Connection"
rmq_conn = Bunny.new
rmq_conn.start
rmq_channel = rmq_conn.create_channel
rmq_queue = rmq_channel.queue(queue_name)
puts "Done"
puts "Initiating Mongo Connection"
mongo_client = MongoClient.new
mongo_db = mongo_client.db(database_name)
mongo_collection = mongo_db.collection(collection_name)
puts "Done"
puts "Subscribed to the queue"
rmq_queue.subscribe(:block => true) do |delivery_info, properties, body|
begin
doc = JSON.parse(body)
doc_id = mongo_collection.insert(doc)
rescue => e
puts e.inspect
puts e.backtrace
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment