Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save pelted/f5ca73554a6c883c97a8eb8beb1cd2d6 to your computer and use it in GitHub Desktop.

Select an option

Save pelted/f5ca73554a6c883c97a8eb8beb1cd2d6 to your computer and use it in GitHub Desktop.

Revisions

  1. @nhocki nhocki created this gist Jun 1, 2012.
    7 changes: 7 additions & 0 deletions initializer_notification_center.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # config/initializer/notification_center.rb
    NotificationCenter.queue
    NotificationCenter.thread

    ActiveSupport::Notifications.subscribe /(.)+\.notification/i do |*args|
    NotificationCenter.queue << args
    end
    35 changes: 35 additions & 0 deletions notification_center.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    # lib/notification_center.rb
    require 'thread'
    require 'active_support/notifications'

    module NotificationCenter

    def self.logger
    Rails.logger
    end

    def self.queue
    @queue ||= Queue.new
    end

    def self.thread
    @thread ||= Thread.new do
    while args = queue.pop
    logger.fatal "\n\n\n\n"
    event = ActiveSupport::Notifications::Event.new(*args)
    logger.fatal "[#{event.name}] Starting Event"
    logger.fatal "[#{event.name}] #{args}"
    logger.fatal "[#{event.name}] #{event.duration}"
    logger.fatal "[#{event.name}] #{event.payload}"
    logger.fatal "\n\n\n\n"
    end
    end
    end

    def self.finish!
    queue << nil
    thread.join
    @thread = nil
    thread
    end
    end
    7 changes: 7 additions & 0 deletions somewhere.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # somewhere in the code
    info = {
    user: "nhocki",
    email: "what",
    message: "What's this?"
    }
    ActiveSupport::Notifications.notifier.instrument("fail.notification", :extra => info)