From b0a16a97762c4cdcf36403d46968930099ddcd0f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 26 Jul 2018 12:03:31 -0700 Subject: Subscribe to event objects via `subscribe_event` Fanout notifier can send event objects to subscribers now. Also moved `end` lower in the `finish!` method to guarantee that CPU time is shorter than real time. --- .../lib/active_support/notifications/fanout.rb | 38 ++++++++++++++++++++++ .../active_support/notifications/instrumenter.rb | 2 +- 2 files changed, 39 insertions(+), 1 deletion(-) (limited to 'activesupport/lib/active_support/notifications') diff --git a/activesupport/lib/active_support/notifications/fanout.rb b/activesupport/lib/active_support/notifications/fanout.rb index 25aab175b4..91d8491c7e 100644 --- a/activesupport/lib/active_support/notifications/fanout.rb +++ b/activesupport/lib/active_support/notifications/fanout.rb @@ -27,6 +27,15 @@ module ActiveSupport subscriber end + def subscribe_event(pattern = nil, &block) + subscriber = Subscribers.event_object_subscriber pattern, block + synchronize do + @subscribers << subscriber + @listeners_for.clear + end + subscriber + end + def unsubscribe(subscriber_or_name) synchronize do case subscriber_or_name @@ -76,6 +85,14 @@ module ActiveSupport subscriber = Timed.new pattern, listener end + wrap_all pattern, subscriber + end + + def self.event_object_subscriber(pattern, block) + wrap_all pattern, EventObject.new(pattern, block) + end + + def self.wrap_all(pattern, subscriber) unless pattern AllMessages.new(subscriber) else @@ -130,6 +147,27 @@ module ActiveSupport end end + class EventObject < Evented + def start(name, id, payload) + stack = Thread.current[:_event_stack] ||= [] + event = build_event name, id, payload + event.start! + stack.push event + end + + def finish(name, id, payload) + stack = Thread.current[:_event_stack] + event = stack.pop + event.finish! + @delegate.call event + end + + private + def build_event(name, id, payload) + ActiveSupport::Notifications::Event.new name, nil, nil, id, payload + end + end + class AllMessages # :nodoc: def initialize(delegate) @delegate = delegate diff --git a/activesupport/lib/active_support/notifications/instrumenter.rb b/activesupport/lib/active_support/notifications/instrumenter.rb index 259cb4db98..72e5064679 100644 --- a/activesupport/lib/active_support/notifications/instrumenter.rb +++ b/activesupport/lib/active_support/notifications/instrumenter.rb @@ -76,8 +76,8 @@ module ActiveSupport end def finish! - @end = now @cpu_time_finish = now_cpu + @end = now @allocation_count_finish = now_allocations end -- cgit v1.2.3