Commit b7298802b37eb5cf4c77962b7104d8d9e839901b
1 parent
8d33e220
Exists in
master
and in
21 other branches
Added process action hook
Showing
1 changed file
with
12 additions
and
2 deletions
Show diff stats
lib/searchkick/logging.rb
@@ -15,6 +15,7 @@ module Searchkick | @@ -15,6 +15,7 @@ module Searchkick | ||
15 | alias_method_chain :execute, :instrumentation | 15 | alias_method_chain :execute, :instrumentation |
16 | end | 16 | end |
17 | 17 | ||
18 | + # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/log_subscriber.rb | ||
18 | class LogSubscriber < ActiveSupport::LogSubscriber | 19 | class LogSubscriber < ActiveSupport::LogSubscriber |
19 | def self.runtime=(value) | 20 | def self.runtime=(value) |
20 | Thread.current[:searchkick_runtime] = value | 21 | Thread.current[:searchkick_runtime] = value |
@@ -43,11 +44,22 @@ module Searchkick | @@ -43,11 +44,22 @@ module Searchkick | ||
43 | end | 44 | end |
44 | end | 45 | end |
45 | 46 | ||
47 | + # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/controller_runtime.rb | ||
46 | module ControllerRuntime | 48 | module ControllerRuntime |
47 | extend ActiveSupport::Concern | 49 | extend ActiveSupport::Concern |
48 | 50 | ||
51 | + protected | ||
52 | + | ||
49 | attr_internal :searchkick_runtime | 53 | attr_internal :searchkick_runtime |
50 | 54 | ||
55 | + def process_action(action, *args) | ||
56 | + # We also need to reset the runtime before each action | ||
57 | + # because of queries in middleware or in cases we are streaming | ||
58 | + # and it won't be cleaned up by the method below. | ||
59 | + Searchkick::LogSubscriber.reset_runtime | ||
60 | + super | ||
61 | + end | ||
62 | + | ||
51 | def cleanup_view_runtime | 63 | def cleanup_view_runtime |
52 | searchkick_rt_before_render = Searchkick::LogSubscriber.reset_runtime | 64 | searchkick_rt_before_render = Searchkick::LogSubscriber.reset_runtime |
53 | runtime = super | 65 | runtime = super |
@@ -56,8 +68,6 @@ module Searchkick | @@ -56,8 +68,6 @@ module Searchkick | ||
56 | runtime - searchkick_rt_after_render | 68 | runtime - searchkick_rt_after_render |
57 | end | 69 | end |
58 | 70 | ||
59 | - protected | ||
60 | - | ||
61 | def append_info_to_payload(payload) | 71 | def append_info_to_payload(payload) |
62 | super | 72 | super |
63 | payload[:searchkick_runtime] = (searchkick_runtime || 0) + Searchkick::LogSubscriber.reset_runtime | 73 | payload[:searchkick_runtime] = (searchkick_runtime || 0) + Searchkick::LogSubscriber.reset_runtime |