Skip to content

Commit

Permalink
store notifications asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
etehtsea committed Feb 20, 2011
1 parent cc0f7e9 commit a7b8975
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 0 additions & 2 deletions free_relic.gemspec
@@ -1,5 +1,3 @@
# Provide a simple gemspec so you can easily use your enginex
# project in your rails apps through git.
Gem::Specification.new do |s|
s.name = "free_relic"
s.summary = "Insert FreeRelic summary."
Expand Down
27 changes: 24 additions & 3 deletions lib/free_relic.rb
Expand Up @@ -3,11 +3,32 @@
require 'free_relic/mute_middleware'
require 'free_relic/engine'

ActiveSupport::Notifications.subscribe do |*args|
FreeRelic::Metric.store!(args) unless FreeRelic.mute?
module FreeRelic
def self.queue
@queue ||= Queue.new
end

def self.thread
@thread ||= Thread.new do
while args = queue.pop
FreeRelic::Metric.store!(args)
end
end
end

def self.finish!
queue << nil
thread.join
@thread = nil
thread
end
end

module FreeRelic
FreeRelic.queue
FreeRelic.thread

ActiveSupport::Notifications.subscribe do |*args|
FreeRelic.queue << args unless FreeRelic.mute?
end

Mongoid.configure do |config|
Expand Down
1 change: 1 addition & 0 deletions spec/free_relic_spec.rb
Expand Up @@ -4,6 +4,7 @@
def notify_sql
ActiveSupport::Notifications.instrument "sql.any_orm",
payload { sleep(0.001) }
FreeRelic.finish!
end

before { FreeRelic::Metric.delete_all }
Expand Down
10 changes: 8 additions & 2 deletions spec/integration/navigation_spec.rb
Expand Up @@ -4,12 +4,18 @@
include Capybara

it "should store notifications" do
expect { visit "/users" }.to change{ FreeRelic::Metric.count }
expect do
visit "/users"
FreeRelic.finish!
end.to change{ FreeRelic::Metric.count }
end

it "should not store notifications if path muted by regexp" do
FreeRelic.mute_regexp = %r{^/users}
expect { visit "/users" }.not_to change{ FreeRelic::Metric.count }
expect do
visit "/users"
FreeRelic.finish!
end.not_to change{ FreeRelic::Metric.count }
FreeRelic.mute_regexp = nil
end

Expand Down

0 comments on commit a7b8975

Please sign in to comment.