Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ActiveSupport::Notifications

Lucas Mazza
December 18, 2012

 ActiveSupport::Notifications

Lightining Talk apresentada no Hackin Evening da Plataformatec

Lucas Mazza

December 18, 2012
Tweet

More Decks by Lucas Mazza

Other Decks in Programming

Transcript

  1. ActiveSupport::Notifications.subscribe 'xml.processed' do |*args| event = ActiveSupport::Notifications::Event.new(*args) event.name # 'xml.processed'

    event.payload[:xml] # 'WTF_BBQ_10.xml' event.duration # 10 miliseconds event.time # Time event.end # Time event.payload[:exception] # ["RuntimeError", ":poop:"] end
  2. ActiveSupport::Notifications.subscribe %r[xml] do |*args| event = ActiveSupport::Notifications::Event.new(*args) event.name # 'xml.processed'

    / 'xml.not_found' event.payload[:xml] # 'WTF_BBQ_10.xml' event.duration # 10 miliseconds event.time # Time event.end # Time event.payload[:exception] # ["RuntimeError", ":poop:"] end
  3. Q%(!+* ,#+$ ( b,&!+ class TimedSubscriber def call(name, started, finished,

    id, payload) # Instrumented block finished end end ActiveSupport::Notifications.subscribe %r[xml], TimedSubscriber.new
  4. Ev$)"$- class EventedSubscriber def start(name, id, payload) # Instrumented block

    started end def finish(name, id, payload) # Instrumented block finished end end ActiveSupport::Notifications.subscribe %r[xml], EventedSubscriber.new A!"#v$S%pp&r" 4
  5. class OrderXmlParser extend Forwardable def_delegator ActiveSupport::Notifications, :instrument def process(doc) instrument

    'xml.processed', :xml => doc do |payload| # Actual work. # payload[:xml] => doc end end end <3 F&rw(r-(b,$ <3
  6. ActiveSupport::Notifications.subscribe 'xml.processed' do |*args| event = ActiveSupport::Notifications::Event.new(*args) Rails.logger.info "processed #{event.payload[:xml]}"

    end ActiveSupport::Notifications.subscribe 'xml.not_found' do |*args| event = ActiveSupport::Notifications::Event.new(*args) Rails.logger.info ":bomb: #{event.payload[:xml]} not found" end
  7. require 'active_support/log_subscriber' class ProcessingLogger < ActiveSupport::LogSubscriber # xml.processed def processed(event)

    info "processed #{event.payload[:xml]}" end # xml.not_found def not_found(event) debug ":bomb: #{event.payload[:xml]} not found" end end ProcessingLogger.attach_to :xml
  8. • Workers (Resque, Sidekiq, etc) • uso de APIs (Faraday)

    • Rake tasks • E toda a stack da sua aplicação :)
  9. ')!