Permalink
Browse files

Merge remote branch 'stephencelis/log_subscriber' into rails3

  • Loading branch information...
2 parents edcc48a + e19c8f5 commit 58f1133526a817286d526ad47baaad67f4e92aae @pat pat committed Nov 18, 2010
View
@@ -1,6 +1,7 @@
source 'http://rubygems.org'
gem 'activerecord', '>= 3.0.2'
+gem 'actionpack', '>= 3.0.2'
gem 'riddle', '>= 1.2.1'
group :development do
@@ -0,0 +1,31 @@
+module ThinkingSphinx
+ module ActionController
+ extend ActiveSupport::Concern
+
+ protected
+
+ attr_internal :query_runtime
+
+ def cleanup_view_runtime
+ log_subscriber = ThinkingSphinx::ActiveRecord::LogSubscriber
+ query_runtime_pre_render = log_subscriber.reset_runtime
+ runtime = super
+ query_runtime_post_render = log_subscriber.reset_runtime
+ self.query_runtime = query_runtime_pre_render + query_runtime_post_render
+ runtime - query_runtime_post_render
+ end
+
+ def append_info_to_payload(payload)
+ super
+ payload[:query_runtime] = query_runtime
+ end
+
+ module ClassMethods
+ def log_process_action(payload)
+ messages, query_runtime = super, payload[:query_runtime]
+ messages << ("Sphinx: %.3fms" % query_runtime.to_f) if query_runtime
+ messages
+ end
+ end
+ end
+end
@@ -1,6 +1,7 @@
require 'thinking_sphinx/active_record/attribute_updates'
require 'thinking_sphinx/active_record/delta'
require 'thinking_sphinx/active_record/has_many_association'
+require 'thinking_sphinx/active_record/log_subscriber'
require 'thinking_sphinx/active_record/scopes'
module ThinkingSphinx
@@ -0,0 +1,61 @@
+require 'active_support/log_subscriber'
+
+module ThinkingSphinx
+ module ActiveRecord
+ class LogSubscriber < ActiveSupport::LogSubscriber
+ def self.runtime=(value)
+ Thread.current['thinking_sphinx_query_runtime'] = value
+ end
+
+ def self.runtime
+ Thread.current['thinking_sphinx_query_runtime'] ||= 0
+ end
+
+ def self.reset_runtime
+ rt, self.runtime = runtime, 0
+ rt
+ end
+
+ def initialize
+ super
+ @odd_or_even = false
+ end
+
+ def query(event)
+ self.class.runtime += event.duration
+ return unless logger.debug?
+
+ identifier = color('Sphinx Query (%.3fms)' % event.duration, GREEN, true)
+ query = event.payload[:query]
+ query = color query, nil, true if odd?
+
+ debug " #{identifier} #{query}"
+ end
+
+ def message(event)
+ return unless logger.debug?
+
+ identifier = color 'Sphinx', GREEN, true
+ message = event.payload[:message]
+ message = color message, nil, true if odd?
+
+ debug " #{identifier} #{message}"
+ end
+
+ def odd?
+ @odd_or_even = !@odd_or_even
+ end
+
+ def logger
+ return @logger if defined? @logger
+ self.logger = ::ActiveRecord::Base.logger
+ end
+
+ def logger=(logger)
+ @logger = logger
+ end
+
+ attach_to :thinking_sphinx
+ end
+ end
+end
@@ -10,6 +10,13 @@ class Railtie < Rails::Railtie
end
end
+ initializer "thinking_sphinx.action_controller" do
+ ActiveSupport.on_load :action_controller do
+ require 'thinking_sphinx/action_controller'
+ include ThinkingSphinx::ActionController
+ end
+ end
+
initializer "thinking_sphinx.set_app_root" do |app|
ThinkingSphinx::Configuration.instance.reset # Rails has setup app now
end
@@ -29,34 +29,38 @@ class Search
# Deprecated. Use ThinkingSphinx.search
def self.search(*args)
- log 'ThinkingSphinx::Search.search is deprecated. Please use ThinkingSphinx.search instead.'
+ warn 'ThinkingSphinx::Search.search is deprecated. Please use ThinkingSphinx.search instead.'
ThinkingSphinx.search *args
end
# Deprecated. Use ThinkingSphinx.search_for_ids
def self.search_for_ids(*args)
- log 'ThinkingSphinx::Search.search_for_ids is deprecated. Please use ThinkingSphinx.search_for_ids instead.'
+ warn 'ThinkingSphinx::Search.search_for_ids is deprecated. Please use ThinkingSphinx.search_for_ids instead.'
ThinkingSphinx.search_for_ids *args
end
# Deprecated. Use ThinkingSphinx.search_for_ids
def self.search_for_id(*args)
- log 'ThinkingSphinx::Search.search_for_id is deprecated. Please use ThinkingSphinx.search_for_id instead.'
+ warn 'ThinkingSphinx::Search.search_for_id is deprecated. Please use ThinkingSphinx.search_for_id instead.'
ThinkingSphinx.search_for_id *args
end
# Deprecated. Use ThinkingSphinx.count
def self.count(*args)
- log 'ThinkingSphinx::Search.count is deprecated. Please use ThinkingSphinx.count instead.'
+ warn 'ThinkingSphinx::Search.count is deprecated. Please use ThinkingSphinx.count instead.'
ThinkingSphinx.count *args
end
# Deprecated. Use ThinkingSphinx.facets
def self.facets(*args)
- log 'ThinkingSphinx::Search.facets is deprecated. Please use ThinkingSphinx.facets instead.'
+ warn 'ThinkingSphinx::Search.facets is deprecated. Please use ThinkingSphinx.facets instead.'
ThinkingSphinx.facets *args
end
-
+
+ def self.warn(message)
+ ::ActiveSupport::Deprecation.warn message
+ end
+
def self.bundle_searches(enum = nil)
bundle = ThinkingSphinx::BundledSearch.new
@@ -344,12 +348,10 @@ def populate
retry_on_stale_index do
begin
- log "Querying: '#{query}'"
- runtime = Benchmark.realtime {
+ log query do
@results = client.query query, indexes, comment
- }
- log "Found #{@results[:total_found]} results", :debug,
- "Sphinx (#{sprintf("%f", runtime)}s)"
+ end
+ log "Found #{@results[:total_found].to_i} results"
rescue Errno::ECONNREFUSED => err
raise ThinkingSphinx::ConnectionError,
'Connection to Sphinx Daemon (searchd) failed.'
@@ -406,30 +408,27 @@ def match_hash(object)
match[:attributes]['class_crc'] == object.class.to_crc32
}
end
-
- def self.log(message, method = :debug, identifier = 'Sphinx')
- return if ::ActiveRecord::Base.logger.nil?
-
- info = ''
- if ::ActiveRecord::LogSubscriber.colorize_logging
- identifier_color, message_color = "4;32;1", "0" # 0;1 = Bold
- info << " \e[#{identifier_color}m#{identifier}\e[0m "
- info << "\e[#{message_color}m#{message}\e[0m"
+
+ def self.log(message, &block)
+ return if ThinkingSphinx::ActiveRecord::LogSubscriber.logger.nil?
+
+ if block_given?
+ ::ActiveSupport::Notifications.
+ instrument('query.thinking_sphinx', :query => message, &block)
else
- info = "#{identifier} #{message}"
+ ::ActiveSupport::Notifications.
+ instrument('message.thinking_sphinx', :message => message)
end
-
- ::ActiveRecord::Base.logger.send method, info
end
-
- def log(*args)
- self.class.log(*args)
+
+ def log(query, &block)
+ self.class.log(query, &block)
end
-
+
def prepare(client)
index_options = one_class ?
one_class.sphinx_indexes.first.local_options : {}
-
+
[
:max_matches, :group_by, :group_function, :group_clause,
:group_distinct, :id_range, :cut_off, :retry_count, :retry_delay,
@@ -475,8 +474,8 @@ def retry_on_stale_index(&block)
stale_ids |= err.ids
# ID exclusion
options[:without_ids] = Array(options[:without_ids]) | err.ids
-
- log 'Sphinx Stale Ids (%s %s left): %s' % [
+
+ log 'Stale Ids (%s %s left): %s' % [
retries, (retries == 1 ? 'try' : 'tries'), stale_ids.join(', ')
]
retry
View
@@ -15,7 +15,7 @@
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
-ActiveRecord::Base.logger = Logger.new(StringIO.new)
+ThinkingSphinx::ActiveRecord::LogSubscriber.logger = Logger.new(StringIO.new)
RSpec.configure do |config|
%w( tmp tmp/config tmp/log tmp/db ).each do |path|
@@ -115,46 +115,56 @@
it "return the output of ThinkingSphinx.search" do
@results = [] # to confirm same object
ThinkingSphinx.stub!(:search => @results)
-
- ThinkingSphinx::Search.search.object_id.should == @results.object_id
+
+ ActiveSupport::Deprecation.silence do
+ ThinkingSphinx::Search.search.object_id.should == @results.object_id
+ end
end
end
describe '.search_for_ids' do
it "return the output of ThinkingSphinx.search_for_ids" do
@results = [] # to confirm same object
ThinkingSphinx.stub!(:search_for_ids => @results)
-
- ThinkingSphinx::Search.search_for_ids.object_id.
- should == @results.object_id
+
+ ActiveSupport::Deprecation.silence do
+ ThinkingSphinx::Search.search_for_ids.object_id.
+ should == @results.object_id
+ end
end
end
describe '.search_for_id' do
it "return the output of ThinkingSphinx.search_for_ids" do
@results = [] # to confirm same object
ThinkingSphinx.stub!(:search_for_id => @results)
-
- ThinkingSphinx::Search.search_for_id.object_id.
- should == @results.object_id
+
+ ActiveSupport::Deprecation.silence do
+ ThinkingSphinx::Search.search_for_id.object_id.
+ should == @results.object_id
+ end
end
end
describe '.count' do
it "return the output of ThinkingSphinx.search" do
@results = [] # to confirm same object
ThinkingSphinx.stub!(:count => @results)
-
- ThinkingSphinx::Search.count.object_id.should == @results.object_id
+
+ ActiveSupport::Deprecation.silence do
+ ThinkingSphinx::Search.count.object_id.should == @results.object_id
+ end
end
end
describe '.facets' do
it "return the output of ThinkingSphinx.facets" do
@results = [] # to confirm same object
ThinkingSphinx.stub!(:facets => @results)
-
- ThinkingSphinx::Search.facets.object_id.should == @results.object_id
+
+ ActiveSupport::Deprecation.silence do
+ ThinkingSphinx::Search.facets.object_id.should == @results.object_id
+ end
end
end

0 comments on commit 58f1133

Please sign in to comment.