2009-10-01 18:00:22 -04:00
|
|
|
require 'active_support/core_ext/module/delegation'
|
2009-09-16 18:53:49 -04:00
|
|
|
|
|
|
|
module ActiveSupport
|
2009-10-15 17:51:51 -04:00
|
|
|
# Notifications provides an instrumentation API for Ruby. To instrument an
|
|
|
|
# action in Ruby you just need to do:
|
2009-09-16 18:53:49 -04:00
|
|
|
#
|
2009-10-15 17:51:51 -04:00
|
|
|
# ActiveSupport::Notifications.instrument(:render, :extra => :information) do
|
2009-09-16 18:53:49 -04:00
|
|
|
# render :text => "Foo"
|
|
|
|
# end
|
|
|
|
#
|
2009-10-06 08:42:42 -04:00
|
|
|
# You can consume those events and the information they provide by registering
|
|
|
|
# a subscriber. For instance, let's store all instrumented events in an array:
|
2009-09-16 18:53:49 -04:00
|
|
|
#
|
2009-10-06 08:42:42 -04:00
|
|
|
# @events = []
|
|
|
|
#
|
2009-10-28 04:58:33 -04:00
|
|
|
# ActiveSupport::Notifications.subscribe do |*args|
|
|
|
|
# @events << ActiveSupport::Notifications::Event.new(*args)
|
2009-10-06 08:42:42 -04:00
|
|
|
# end
|
2009-09-16 18:53:49 -04:00
|
|
|
#
|
2009-10-15 17:51:51 -04:00
|
|
|
# ActiveSupport::Notifications.instrument(:render, :extra => :information) do
|
2009-09-16 18:53:49 -04:00
|
|
|
# render :text => "Foo"
|
|
|
|
# end
|
|
|
|
#
|
2009-10-06 08:42:42 -04:00
|
|
|
# event = @events.first
|
2009-09-16 18:53:49 -04:00
|
|
|
# event.name #=> :render
|
|
|
|
# event.duration #=> 10 (in miliseconds)
|
|
|
|
# event.result #=> "Foo"
|
|
|
|
# event.payload #=> { :extra => :information }
|
|
|
|
#
|
2009-10-15 17:51:51 -04:00
|
|
|
# When subscribing to Notifications, you can pass a pattern, to only consume
|
2009-10-06 08:42:42 -04:00
|
|
|
# events that match the pattern:
|
|
|
|
#
|
2009-10-15 17:51:51 -04:00
|
|
|
# ActiveSupport::Notifications.subscribe(/render/) do |event|
|
2009-10-06 08:42:42 -04:00
|
|
|
# @render_events << event
|
|
|
|
# end
|
|
|
|
#
|
2009-10-15 17:51:51 -04:00
|
|
|
# Notifications ships with a queue implementation that consumes and publish events
|
2009-10-06 08:42:42 -04:00
|
|
|
# to subscribers in a thread. You can use any queue implementation you want.
|
2009-09-16 18:53:49 -04:00
|
|
|
#
|
2009-10-15 17:51:51 -04:00
|
|
|
module Notifications
|
2009-11-28 15:49:07 -05:00
|
|
|
autoload :Instrumenter, 'active_support/notifications/instrumenter'
|
|
|
|
autoload :Event, 'active_support/notifications/instrumenter'
|
|
|
|
autoload :Fanout, 'active_support/notifications/fanout'
|
2009-09-30 07:59:15 -04:00
|
|
|
|
2009-10-01 18:00:22 -04:00
|
|
|
class << self
|
2009-11-28 15:49:07 -05:00
|
|
|
attr_writer :notifier
|
2010-01-06 16:23:29 -05:00
|
|
|
delegate :publish, :subscribe, :to => :notifier
|
2010-01-17 05:17:42 -05:00
|
|
|
delegate :instrument, :instrument!, :to => :instrumenter
|
2009-10-01 18:00:22 -04:00
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
def notifier
|
|
|
|
@notifier ||= Notifier.new
|
2009-10-01 18:00:22 -04:00
|
|
|
end
|
2010-01-06 16:23:29 -05:00
|
|
|
|
|
|
|
def instrumenter
|
|
|
|
Thread.current[:"instrumentation_#{notifier.object_id}"] ||= Instrumenter.new(notifier)
|
|
|
|
end
|
2009-10-01 18:00:22 -04:00
|
|
|
end
|
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
class Notifier
|
|
|
|
def initialize(queue = Fanout.new)
|
2009-10-01 18:00:22 -04:00
|
|
|
@queue = queue
|
|
|
|
end
|
|
|
|
|
2009-10-27 03:07:21 -04:00
|
|
|
def publish(*args)
|
|
|
|
@queue.publish(*args)
|
2009-10-01 18:00:22 -04:00
|
|
|
end
|
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
def subscribe(pattern = nil, &block)
|
|
|
|
@queue.bind(pattern).subscribe(&block)
|
2009-09-16 18:53:49 -04:00
|
|
|
end
|
|
|
|
|
2009-11-24 22:35:01 -05:00
|
|
|
def wait
|
2009-11-28 15:49:07 -05:00
|
|
|
@queue.wait
|
2009-09-30 07:59:15 -04:00
|
|
|
end
|
2009-09-16 18:53:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|