Permalink
Browse files

Simplify config.async to use only notice param

  • Loading branch information...
cris committed Sep 3, 2012
1 parent 0940f2e commit 1111f116966959a066cf22e7d1331a533193a334
Showing with 44 additions and 19 deletions.
  1. +11 −7 README.md
  2. +1 −2 lib/airbrake.rb
  3. +21 −8 lib/airbrake/configuration.rb
  4. +7 −0 test/configuration_test.rb
  5. +4 −2 test/notifier_test.rb
View
@@ -277,21 +277,25 @@ configuration.
*Ruby 1.9+. It does not support Ruby 1.8 because of its poor threading*
*support.*
-For implementing custom asynchronous send, pass any callable object instead of
-`true`. This callable receives `job` and `notice` params. `job.call` does
-actual notification delivery. `notice` is useful in case of some remote
-worker(e.g. Resque or Sidekiq), when execution of `job.call` isn't possible.
+For implementing custom asynchronous send, provide block to `config.async`. It
+receives `notice` param. Pass it to `Airbrake.sender.send_to_airbrake` method
+to do actual delivery. In this way it's possible to move Airbrake notification
+even in background worker(e.g. Resque or Sidekiq).
- # configuration for Threads or Actors
+ # Thread-based asynchronous send
Airbrake.configure do |config|
...
- config.async = lambda {|job, _notice| Thread.new {job.call}}
+ config.async do |notice|
+ Thread.new {
+ Airbrake.sender.send_to_airbrake(notice.to_xml)
+ }
+ end
end
# Resque-like configuration
Airbrake.configure do |config|
...
- config.async = lambda do |_job, notice|
+ config.async do |notice|
Resque.enqueue(AirbrakeDeliveryWorker, notice)
end
end
View
@@ -133,8 +133,7 @@ def build_lookup_hash_for(exception, options = {})
def send_notice(notice)
if configuration.public?
if configuration.async?
- job = lambda {sender.send_to_airbrake(notice.to_xml)}
- configuration.async.call(job, notice)
+ configuration.async.call(notice)
else
sender.send_to_airbrake(notice.to_xml)
end
@@ -100,9 +100,6 @@ class Configuration
# (boolean or nil; set to nil to catch exceptions when rake isn't running from a terminal; default is nil)
attr_accessor :rescue_rake_exceptions
- # Should Airbrake send notifications asynchronously
- # (boolean or nil; default is nil)
- attr_reader :async
DEFAULT_PARAMS_FILTERS = %w(password password_confirmation).freeze
@@ -135,7 +132,6 @@ class Configuration
alias_method :secure?, :secure
alias_method :use_system_ssl_cert_chain?, :use_system_ssl_cert_chain
- alias_method :async?, :async
def initialize
@secure = false
@@ -244,9 +240,24 @@ def protocol
end
end
+ # Should Airbrake send notifications asynchronously
+ # (boolean, nil or callable; default is nil).
+ # Can be used as callable-setter when block provided.
+ def async(&block)
+ if block_given?
+ @async = block
+ end
+ @async
+ end
+ alias_method :async?, :async
+
def async=(value)
# use default GirlFriday-async for 'true' value
- @async = value == true ? girl_friday_async_processor : value
+ @async = if value == true
+ default_async_processor
+ else
+ value
+ end
end
def js_api_key
@@ -286,9 +297,11 @@ def default_port
end
end
- def girl_friday_async_processor
- queue = GirlFriday::WorkQueue.new(nil, :size => 3) {|job| job.call}
- lambda {|job, _notice| queue << job}
+ def default_async_processor
+ queue = GirlFriday::WorkQueue.new(nil, :size => 3) do |notice|
+ Airbrake.sender.send_to_airbrake(notice.to_xml)
+ end
+ lambda {|notice| queue << notice}
end
end
@@ -38,6 +38,13 @@ class ConfigurationTest < Test::Unit::TestCase
assert config.async.respond_to?(:call)
end
+ should "set provided-callable for async {}" do
+ config = Airbrake::Configuration.new
+ config.async {|notice| :ok}
+ assert config.async.respond_to?(:call)
+ assert_equal :ok, config.async.call
+ end
+
should "provide default values for secure connections" do
config = Airbrake::Configuration.new
config.secure = true
View
@@ -143,7 +143,9 @@ def set_development_env
should "deliver exception in async-mode" do
Airbrake.configure do |config|
config.environment_name = 'production'
- config.async = lambda {|job, _notice| job.call}
+ config.async do |notice|
+ Airbrake.sender.send_to_airbrake(notice.to_xml)
+ end
end
exception = build_exception
sender = stub_sender!
@@ -158,7 +160,7 @@ def set_development_env
received_notice = nil
Airbrake.configure do |config|
config.environment_name = 'production'
- config.async = lambda {|_job, notice| received_notice = notice}
+ config.async {|notice| received_notice = notice}
end
exception = build_exception
sender = stub_sender!

0 comments on commit 1111f11

Please sign in to comment.