class Banter::DbLogger

Public Class Methods

new() click to toggle source
# File lib/banter/db_logger.rb, line 7
def initialize
  @config = Configuration.configuration[:mongo]
  if @config.present?
    @client = Mongo::MongoClient.new(@config[:host], @config[:port])
  else
    @client = Mongo::MongoClient.new
  end

  @db = @client.db("logger")

end

Public Instance Methods

log_publish(routing_key, message) click to toggle source
# File lib/banter/db_logger.rb, line 36
def log_publish(routing_key, message)
  collection_name = "publisher"
  collection = @db.collection(collection_name)

  # no need for safe writes.  need to write to a file log as well.
  collection.insert({routing_key: routing_key, cts: message[:payload], ts: message[:ts], pub: message[:pub]})
  # make sure that we always index these two items in mongo
  # it's a small price of a call, so no big deal
  collection.ensure_index( {routing_key:1, ts:1} )

end
start_subscribe() click to toggle source
# File lib/banter/db_logger.rb, line 19
def start_subscribe
  subscriber = ::Banter::Server::RabbitMQSubscriber.new("")
  subscriber.start("logger") do |info, properties, contents|
    # Write to mongo the contents and the routing_key, with the routing_key being indexed
    routing_key = info[:routing_key]
    chunks = routing_key.split(".")
    # Logger will split the data into collections based upon the first parameter of the routing_key (initial roll out will only use 'honest')
    collection_name = chunks[1] || chunks[0]
    collection = @db.collection(collection_name)
    # no need for safe writes.  need to write to a file log as well.
    collection.insert({routing_key: routing_key, cts: contents, ts: properties[:timestamp]})
    # make sure that we always index these two items in mongo
    # it's a small price of a call, so no big deal
    collection.ensure_index( {routing_key:1, ts:1} )
  end
end
teardown() click to toggle source
# File lib/banter/db_logger.rb, line 48
def teardown
  @subscriber.teardown
end