Commit b43688e703b2c70e5e185c09997c0f2191a07e7a

Authored by Andrew Kane
1 parent 5a975454

Moved controller runtime to separate file [skip ci]

lib/searchkick.rb
... ... @@ -26,6 +26,11 @@ require "searchkick/railtie" if defined?(Rails)
26 26 if defined?(ActiveSupport::Notifications)
27 27 require "searchkick/logging"
28 28 require "searchkick/log_subscriber"
  29 + require "searchkick/controller_runtime"
  30 +
  31 + ActiveSupport.on_load(:action_controller) do
  32 + include Searchkick::ControllerRuntime
  33 + end
29 34 end
30 35  
31 36 module Searchkick
... ...
lib/searchkick/controller_runtime.rb 0 → 100644
... ... @@ -0,0 +1,40 @@
  1 +module Searchkick
  2 + # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/controller_runtime.rb
  3 + module ControllerRuntime
  4 + extend ActiveSupport::Concern
  5 +
  6 + protected
  7 +
  8 + attr_internal :searchkick_runtime
  9 +
  10 + def process_action(action, *args)
  11 + # We also need to reset the runtime before each action
  12 + # because of queries in middleware or in cases we are streaming
  13 + # and it won't be cleaned up by the method below.
  14 + Searchkick::LogSubscriber.reset_runtime
  15 + super
  16 + end
  17 +
  18 + def cleanup_view_runtime
  19 + searchkick_rt_before_render = Searchkick::LogSubscriber.reset_runtime
  20 + runtime = super
  21 + searchkick_rt_after_render = Searchkick::LogSubscriber.reset_runtime
  22 + self.searchkick_runtime = searchkick_rt_before_render + searchkick_rt_after_render
  23 + runtime - searchkick_rt_after_render
  24 + end
  25 +
  26 + def append_info_to_payload(payload)
  27 + super
  28 + payload[:searchkick_runtime] = (searchkick_runtime || 0) + Searchkick::LogSubscriber.reset_runtime
  29 + end
  30 +
  31 + module ClassMethods
  32 + def log_process_action(payload)
  33 + messages = super
  34 + runtime = payload[:searchkick_runtime]
  35 + messages << ("Searchkick: %.1fms" % runtime.to_f) if runtime.to_f > 0
  36 + messages
  37 + end
  38 + end
  39 + end
  40 +end
... ...
lib/searchkick/logging.rb
... ... @@ -139,51 +139,9 @@ module Searchkick
139 139 end
140 140 end
141 141 end
142   -
143   - # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/controller_runtime.rb
144   - module ControllerRuntime
145   - extend ActiveSupport::Concern
146   -
147   - protected
148   -
149   - attr_internal :searchkick_runtime
150   -
151   - def process_action(action, *args)
152   - # We also need to reset the runtime before each action
153   - # because of queries in middleware or in cases we are streaming
154   - # and it won't be cleaned up by the method below.
155   - Searchkick::LogSubscriber.reset_runtime
156   - super
157   - end
158   -
159   - def cleanup_view_runtime
160   - searchkick_rt_before_render = Searchkick::LogSubscriber.reset_runtime
161   - runtime = super
162   - searchkick_rt_after_render = Searchkick::LogSubscriber.reset_runtime
163   - self.searchkick_runtime = searchkick_rt_before_render + searchkick_rt_after_render
164   - runtime - searchkick_rt_after_render
165   - end
166   -
167   - def append_info_to_payload(payload)
168   - super
169   - payload[:searchkick_runtime] = (searchkick_runtime || 0) + Searchkick::LogSubscriber.reset_runtime
170   - end
171   -
172   - module ClassMethods
173   - def log_process_action(payload)
174   - messages = super
175   - runtime = payload[:searchkick_runtime]
176   - messages << ("Searchkick: %.1fms" % runtime.to_f) if runtime.to_f > 0
177   - messages
178   - end
179   - end
180   - end
181 142 end
182 143  
183 144 Searchkick::Query.prepend(Searchkick::QueryWithInstrumentation)
184 145 Searchkick::Index.prepend(Searchkick::IndexWithInstrumentation)
185 146 Searchkick::Indexer.prepend(Searchkick::IndexerWithInstrumentation)
186 147 Searchkick.singleton_class.prepend(Searchkick::SearchkickWithInstrumentation)
187   -ActiveSupport.on_load(:action_controller) do
188   - include Searchkick::ControllerRuntime
189   -end
... ...