Forked from nhocki/initializer_notification_center.rb
Last active
August 29, 2015 14:07
-
-
Save tyrauber/f66264972fa8756fe5e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializer/notification_center.rb | |
NotificationCenter.queue | |
NotificationCenter.thread | |
ActiveSupport::Notifications.subscribe /(.)+\.notification/i do |*args| | |
NotificationCenter.queue << args | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# somewhere in the code | |
info = { | |
user: "nhocki", | |
email: "what", | |
message: "What's this?" | |
} | |
ActiveSupport::Notifications.instrument("fail.notification", info) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment