diff options
-rw-r--r-- | activerecord/test/cases/mysql_rake_test.rb | 4 | ||||
-rw-r--r-- | activesupport/lib/active_support/notifications.rb | 38 | ||||
-rw-r--r-- | activesupport/lib/active_support/notifications/fanout.rb | 11 |
3 files changed, 35 insertions, 18 deletions
diff --git a/activerecord/test/cases/mysql_rake_test.rb b/activerecord/test/cases/mysql_rake_test.rb index 39d0ae428c..67b61f631f 100644 --- a/activerecord/test/cases/mysql_rake_test.rb +++ b/activerecord/test/cases/mysql_rake_test.rb @@ -129,7 +129,7 @@ module ActiveRecord ActiveRecord::Base.stubs(:establish_connection).returns(true) end - def test_establishes_connection_to_postgresql_database + def test_establishes_connection_to_mysql_database ActiveRecord::Base.expects(:establish_connection).with @configuration ActiveRecord::Tasks::DatabaseTasks.drop @configuration @@ -142,7 +142,7 @@ module ActiveRecord end end - class MySQLTestPurge < ActiveRecord::TestCase + class MySQLPurgeTest < ActiveRecord::TestCase def setup @connection = stub(:recreate_database => true) @configuration = { diff --git a/activesupport/lib/active_support/notifications.rb b/activesupport/lib/active_support/notifications.rb index e3d8cf48ce..55041ea2af 100644 --- a/activesupport/lib/active_support/notifications.rb +++ b/activesupport/lib/active_support/notifications.rb @@ -135,17 +135,30 @@ module ActiveSupport # to log subscribers in a thread. You can use any queue implementation you want. # module Notifications - @instrumenters = Hash.new { |h,k| h[k] = notifier.listening?(k) } + class Registry # :nodoc: + def self.instance + Thread.current[name] ||= new + end - class << self - attr_accessor :notifier + attr_reader :notifier, :instrumenter + + def initialize + self.notifier = Fanout.new + end + + def notifier=(notifier) + @notifier = notifier + @instrumenter = Instrumenter.new(notifier) + end + end + class << self def publish(name, *args) notifier.publish(name, *args) end def instrument(name, payload = {}) - if @instrumenters[name] + if notifier.listening?(name) instrumenter.instrument(name, payload) { yield payload if block_given? } else yield payload if block_given? @@ -153,9 +166,7 @@ module ActiveSupport end def subscribe(*args, &block) - notifier.subscribe(*args, &block).tap do - @instrumenters.clear - end + notifier.subscribe(*args, &block) end def subscribed(callback, *args, &block) @@ -167,14 +178,19 @@ module ActiveSupport def unsubscribe(args) notifier.unsubscribe(args) - @instrumenters.clear end def instrumenter - Thread.current[:"instrumentation_#{notifier.object_id}"] ||= Instrumenter.new(notifier) + Registry.instance.instrumenter end - end - self.notifier = Fanout.new + def notifier + Registry.instance.notifier + end + + def notifier=(notifier) + Registry.instance.notifier = notifier + end + end end end diff --git a/activesupport/lib/active_support/notifications/fanout.rb b/activesupport/lib/active_support/notifications/fanout.rb index 17c99089c1..3a2ccf1489 100644 --- a/activesupport/lib/active_support/notifications/fanout.rb +++ b/activesupport/lib/active_support/notifications/fanout.rb @@ -2,6 +2,9 @@ module ActiveSupport module Notifications # This is a default queue implementation that ships with Notifications. # It just pushes events to all registered log subscribers. + # + # Only one of these objects should instantiated per thread. Concurrent + # access to this class is not allowed. class Fanout def initialize @subscribers = [] @@ -85,9 +88,7 @@ module ActiveSupport class Timed < Evented def initialize(pattern, delegate) - @timestack = Hash.new { |h,id| - h[id] = Hash.new { |ids,name| ids[name] = [] } - } + @timestack = [] super end @@ -96,11 +97,11 @@ module ActiveSupport end def start(name, id, payload) - @timestack[id][name].push Time.now + @timestack.push Time.now end def finish(name, id, payload) - started = @timestack[id][name].pop + started = @timestack.pop @delegate.call(name, started, Time.now, id, payload) end end |