Skip to content

Instantly share code, notes, and snippets.

@mediocretes
Created September 12, 2011 19:29
Show Gist options
  • Save mediocretes/1212145 to your computer and use it in GitHub Desktop.
Save mediocretes/1212145 to your computer and use it in GitHub Desktop.
require 'simple_logger'
module Cie
class Data
@logger = defined?(RAILS_DEFAULT_LOGGER) ? RAILS_DEFAULT_LOGGER : SimpleLogger.instance
def self.connect(slave_ok)
options = CIE_CONNECTION.merge(:slave_ok => slave_ok)
Mongo::Connection.new(options[:server], options[:port], options).db(CIE_CONNECTION[:db_name])
end
#for passenger
def self.reconnect
@readable_db.connection.connect_to_master unless @readable_db.nil?
@writeable_db.connection.connect_to_master unless @writeable_db.nil?
end
def self.wrap(collection_name)
CollectionWrapper.new(collection_name, @writable_db ||= connect(false), @readable_db ||= connect(true))
end
def self.ping
wrap('Test')
end
def self.searches(company)
wrap('Searches.'+company)
end
#... then lots more like those two
end
class CollectionWrapper
READABLE_OK = [:find, :find_one, :group, :count, :distinct]
def initialize(collection, readable, writable)
@collection, @readable, @writable = collection, readable, writable
end
def method_missing(method, *args, &block)
if READABLE_OK.include? method
@readable[@collection].send(method, *args, &block)
else
@writable[@collection].send(method, *args, &block)
end
end
end
end
if defined? NewRelic
module Cie
class Data
include NewRelic::Agent::MethodTracer
class << self
add_method_tracer :wrap, 'Custom/ciedata/wrap'
end
end
end
module Mongo
class Collection
include NewRelic::Agent::MethodTracer
add_method_tracer :find, 'Custom/mongo/find'
add_method_tracer :find_one, 'Custom/mongo/find_one'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment