From 8b340ab2f62bac2af9d5917e296bb4101530282a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 15 Oct 2009 18:06:15 -0300 Subject: Revert "Rename Orchestra to Notifications [#3321 state:resolved]" This reverts commit 8cbf825425dc8ad3770881ea4e100b9023c69ce2. --- activesupport/lib/active_support/autoload.rb | 2 +- activesupport/lib/active_support/cache.rb | 2 +- activesupport/lib/active_support/notifications.rb | 103 ---------------------- activesupport/lib/active_support/orchestra.rb | 103 ++++++++++++++++++++++ 4 files changed, 105 insertions(+), 105 deletions(-) delete mode 100644 activesupport/lib/active_support/notifications.rb create mode 100644 activesupport/lib/active_support/orchestra.rb (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/autoload.rb b/activesupport/lib/active_support/autoload.rb index da12ecceca..a0fc2bb123 100644 --- a/activesupport/lib/active_support/autoload.rb +++ b/activesupport/lib/active_support/autoload.rb @@ -17,7 +17,7 @@ module ActiveSupport autoload :MessageVerifier, 'active_support/message_verifier' autoload :Multibyte, 'active_support/multibyte' autoload :OptionMerger, 'active_support/option_merger' - autoload :Notifications, 'active_support/notifications' + autoload :Orchestra, 'active_support/orchestra' autoload :OrderedHash, 'active_support/ordered_hash' autoload :OrderedOptions, 'active_support/ordered_options' autoload :Rescuable, 'active_support/rescuable' diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index e0f671f283..a415686020 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -247,7 +247,7 @@ module ActiveSupport payload = { :key => key } payload.merge!(options) if options.is_a?(Hash) - event = ActiveSupport::Notifications.instrument(:"cache_#{operation}", payload, &block) + event = ActiveSupport::Orchestra.instrument(:"cache_#{operation}", payload, &block) log("#{operation} (%.1fms)" % event.duration, key, options) event.result end diff --git a/activesupport/lib/active_support/notifications.rb b/activesupport/lib/active_support/notifications.rb deleted file mode 100644 index 6335686196..0000000000 --- a/activesupport/lib/active_support/notifications.rb +++ /dev/null @@ -1,103 +0,0 @@ -require 'thread' - -module ActiveSupport - # Notifications provides an instrumentation API for Ruby. To instrument an action - # in Ruby you just need to: - # - # ActiveSupport::Notifications.instrument(:render, :extra => :information) do - # render :text => "Foo" - # end - # - # Those actions are consumed by listeners. A listener is anything that responds - # to push. You can even register an array: - # - # @listener = [] - # ActiveSupport::Notifications.register @listener - # - # ActiveSupport::Notifications.instrument(:render, :extra => :information) do - # render :text => "Foo" - # end - # - # event #=> ActiveSupport::Notifications::Event - # event.name #=> :render - # event.duration #=> 10 (in miliseconds) - # event.result #=> "Foo" - # event.payload #=> { :extra => :information } - # - # Notifications ships with a default listener implementation which puts events in - # a stream and consume them in a Thread. This implementation is thread safe - # and is available at ActiveSupport::Notifications::Listener. - # - module Notifications - @stacked_events = Hash.new { |h,k| h[k] = [] } - @listeners = [] - - def self.instrument(name, payload=nil) - stack = @stacked_events[Thread.current.object_id] - event = Event.new(name, stack.last, payload) - stack << event - event.result = yield - event - ensure - event.finish! - stack.delete(event) - @listeners.each { |s| s.push(event) } - end - - def self.register(listener) - @listeners << listener - end - - def self.unregister(listener) - @listeners.delete(listener) - end - - class Event - attr_reader :name, :time, :duration, :parent, :thread_id, :payload - attr_accessor :result - - def initialize(name, parent=nil, payload=nil) - @name = name - @time = Time.now - @thread_id = Thread.current.object_id - @parent = parent - @payload = payload - end - - def finish! - @duration = 1000 * (Time.now.to_f - @time.to_f) - end - end - - class Listener - attr_reader :mutex, :signaler, :thread - - def initialize - @mutex, @signaler = Mutex.new, ConditionVariable.new - @stream = [] - @thread = Thread.new do - loop do - (event = @stream.shift) ? consume(event) : wait - end - end - end - - def wait - @mutex.synchronize do - @signaler.wait(@mutex) - end - end - - def push(event) - @mutex.synchronize do - @stream.push(event) - @signaler.broadcast - end - end - - def consume(event) - raise NotImplementedError - end - end - end -end diff --git a/activesupport/lib/active_support/orchestra.rb b/activesupport/lib/active_support/orchestra.rb new file mode 100644 index 0000000000..efe30669d8 --- /dev/null +++ b/activesupport/lib/active_support/orchestra.rb @@ -0,0 +1,103 @@ +require 'thread' + +module ActiveSupport + # Orchestra provides an instrumentation API for Ruby. To instrument an action + # in Ruby you just need to: + # + # ActiveSupport::Orchestra.instrument(:render, :extra => :information) do + # render :text => "Foo" + # end + # + # Those actions are consumed by listeners. A listener is anything that responds + # to push. You can even register an array: + # + # @listener = [] + # ActiveSupport::Orchestra.register @listener + # + # ActiveSupport::Orchestra.instrument(:render, :extra => :information) do + # render :text => "Foo" + # end + # + # event #=> ActiveSupport::Orchestra::Event + # event.name #=> :render + # event.duration #=> 10 (in miliseconds) + # event.result #=> "Foo" + # event.payload #=> { :extra => :information } + # + # Orchestra ships with a default listener implementation which puts events in + # a stream and consume them in a Thread. This implementation is thread safe + # and is available at ActiveSupport::Orchestra::Listener. + # + module Orchestra + @stacked_events = Hash.new { |h,k| h[k] = [] } + @listeners = [] + + def self.instrument(name, payload=nil) + stack = @stacked_events[Thread.current.object_id] + event = Event.new(name, stack.last, payload) + stack << event + event.result = yield + event + ensure + event.finish! + stack.delete(event) + @listeners.each { |s| s.push(event) } + end + + def self.register(listener) + @listeners << listener + end + + def self.unregister(listener) + @listeners.delete(listener) + end + + class Event + attr_reader :name, :time, :duration, :parent, :thread_id, :payload + attr_accessor :result + + def initialize(name, parent=nil, payload=nil) + @name = name + @time = Time.now + @thread_id = Thread.current.object_id + @parent = parent + @payload = payload + end + + def finish! + @duration = 1000 * (Time.now.to_f - @time.to_f) + end + end + + class Listener + attr_reader :mutex, :signaler, :thread + + def initialize + @mutex, @signaler = Mutex.new, ConditionVariable.new + @stream = [] + @thread = Thread.new do + loop do + (event = @stream.shift) ? consume(event) : wait + end + end + end + + def wait + @mutex.synchronize do + @signaler.wait(@mutex) + end + end + + def push(event) + @mutex.synchronize do + @stream.push(event) + @signaler.broadcast + end + end + + def consume(event) + raise NotImplementedError + end + end + end +end -- cgit v1.2.3