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
|
2010-02-15 09:44:30 -05:00
|
|
|
# a log 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
|
2010-06-11 06:15:34 -04:00
|
|
|
# event.duration #=> 10 (in milliseconds)
|
2009-09-16 18:53:49 -04:00
|
|
|
# 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
|
2010-02-15 09:44:30 -05:00
|
|
|
# to log 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-03-01 20:33:59 -05:00
|
|
|
delegate :publish, :subscribe, :unsubscribe, :to => :notifier
|
2010-02-04 04:39:55 -05:00
|
|
|
delegate :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)
|
2010-07-18 18:37:23 -04:00
|
|
|
@queue.subscribe(pattern, &block)
|
2009-09-16 18:53:49 -04:00
|
|
|
end
|
|
|
|
|
2010-03-01 19:01:08 -05:00
|
|
|
def unsubscribe(subscriber)
|
|
|
|
@queue.unsubscribe(subscriber)
|
|
|
|
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
|