From a25b106e0c1fed76e568d7b6e79b932913c1e417 Mon Sep 17 00:00:00 2001 From: Seamus Abshere Date: Tue, 8 Dec 2015 19:17:15 -0500 Subject: [PATCH] #wip get a real connection pool error, don't know how to test it yet --- lib/exception_notification/rails.rb | 3 +- test/dummy/config/database.yml | 6 +-- .../initializers/exception_notification.rb | 42 +++++++++++++++++++ test/exception_notifier_test.rb | 13 ++++++ 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 test/dummy/config/initializers/exception_notification.rb diff --git a/lib/exception_notification/rails.rb b/lib/exception_notification/rails.rb index 88961e7c..9f9dfe84 100644 --- a/lib/exception_notification/rails.rb +++ b/lib/exception_notification/rails.rb @@ -3,6 +3,7 @@ class Engine < ::Rails::Engine config.exception_notification = ExceptionNotifier config.exception_notification.logger = Rails.logger - config.app_middleware.use ExceptionNotification::Rack + # config.app_middleware.use ExceptionNotification::Rack + config.app_middleware.insert_before ActiveRecord::ConnectionAdapters::ConnectionManagement, ExceptionNotification::Rack end end diff --git a/test/dummy/config/database.yml b/test/dummy/config/database.yml index 90d87cc2..2e2ed3e0 100644 --- a/test/dummy/config/database.yml +++ b/test/dummy/config/database.yml @@ -3,7 +3,7 @@ development: adapter: sqlite3 database: db/development.sqlite3 - pool: 5 + pool: 1 timeout: 5000 # Warning: The database defined as "test" will be erased and @@ -12,11 +12,11 @@ development: test: adapter: sqlite3 database: db/test.sqlite3 - pool: 5 + pool: 1 timeout: 5000 production: adapter: sqlite3 database: db/production.sqlite3 - pool: 5 + pool: 1 timeout: 5000 diff --git a/test/dummy/config/initializers/exception_notification.rb b/test/dummy/config/initializers/exception_notification.rb new file mode 100644 index 00000000..07f73e0d --- /dev/null +++ b/test/dummy/config/initializers/exception_notification.rb @@ -0,0 +1,42 @@ +require 'exception_notification/rails' + +ExceptionNotification.configure do |config| + # Ignore additional exception types. + # ActiveRecord::RecordNotFound, AbstractController::ActionNotFound and ActionController::RoutingError are already added. + # config.ignored_exceptions += %w{ActionView::TemplateError CustomError} + + # Adds a condition to decide when an exception must be ignored or not. + # The ignore_if method can be invoked multiple times to add extra conditions. + # config.ignore_if do |exception, options| + # not Rails.env.production? + # end + + # Notifiers ================================================================= + + # Email notifier sends notifications by email. + config.add_notifier :email, { + :email_prefix => "[ERROR] ", + :sender_address => %{"Notifier" }, + :exception_recipients => %w{exceptions@example.com} + } + + # Campfire notifier sends notifications to your Campfire room. Requires 'tinder' gem. + # config.add_notifier :campfire, { + # :subdomain => 'my_subdomain', + # :token => 'my_token', + # :room_name => 'my_room' + # } + + # HipChat notifier sends notifications to your HipChat room. Requires 'hipchat' gem. + # config.add_notifier :hipchat, { + # :api_token => 'my_token', + # :room_name => 'my_room' + # } + + # Webhook notifier sends notifications over HTTP protocol. Requires 'httparty' gem. + # config.add_notifier :webhook, { + # :url => 'http://example.com:5555/hubot/path', + # :http_method => :post + # } + +end diff --git a/test/exception_notifier_test.rb b/test/exception_notifier_test.rb index 19b1bd22..fe5a2fc2 100644 --- a/test/exception_notifier_test.rb +++ b/test/exception_notifier_test.rb @@ -60,6 +60,19 @@ class ExceptionNotifierTest < ActiveSupport::TestCase assert_equal ExceptionNotifier.notifiers, [:email] end + test "can catch database connection pool problems" do + begin + a = b = nil + a = ActiveRecord::Base.connection_pool.checkout + b = ActiveRecord::Base.connection_pool.checkout + ActiveRecord::Base.connection_pool.checkout + + ensure + ActiveRecord::Base.connection_pool.checkin a rescue nil + ActiveRecord::Base.connection_pool.checkin b rescue nil + end + end + test "should ignore exception if satisfies conditional ignore" do env = "production" ExceptionNotifier.ignore_if do |exception, options|