3

I've read over many questions on this site as well as the GitHub documentation for ExceptionNotifier and its implementation in Rails 3, and I can't figure out why it won't work for me on my production server. On my local machine (a Mac), it works perfectly in the production environment. But after I deploy with capistrano and run the app using unicorn on a Linux box, my unicorn log complains about an uninitialized constant ExceptionNotifier in my environments/production.rb file, even though my local machine seems to think it's fine. I've tried with both implementations: gem 'exception_notification', :require => 'exception_notifier' and gem 'exception_notification_rails3', :require => 'exception_notifier'. Both are throwing this error. Here's my production.rb

Rl4::Application.configure do
  # code omitted
  # Enable ExceptionNotifier, having it ignore its default exceptions
  config.middleware.use ExceptionNotifier,
    sender_address: 'reports@ctatechs.com',
    exception_recipients: 'brice@ctatechs.com',
    ignore_exceptions: ExceptionNotifier.default_ignore_exceptions
end

Here's my Gemfile

source 'https://rubygems.org'
# lots omitted
gem 'exception_notification_rails3', :require => 'exception_notifier'
# lots more omitted

And here's my application_controller.rb, which implements the gem:

class ApplicationController < ActionController::Base
  protect_from_forgery

  # By default, only production ENV considers requests not local
  unless Rails.application.config.consider_all_requests_local
    rescue_from Exception, :with => :server_error
    rescue_from ActionController::UnknownAction, :with => :not_found
    rescue_from ActionController::UnknownController, :with => :not_found
    rescue_from ActionView::MissingTemplate, :with => :not_found
  end

  private
    def server_error(exception)
      ExceptionNotifier::Notifier.exception_notification(request.env, exception).deliver
      respond_to do |format|
        format.html { render template: '/500.html', layout: 'application/layout', status: 500 }
        format.all { render nothing: true, status: 500 }
      end
    end

    def not_found(exception)
      ExceptionNotifier::Notifier.exception_notification(request.env, exception).deliver
      respond_to do |format|
        format.html { render template: '/404.html', layout: 'application/layout', status: 404 }
        format.all { render nothing: true, status: 404 }
      end
    end
end

Again, the implementation isn't my problem at the moment. Somehow, the gem isn't installing correctly or it's not being initialized properly. I did run a sudo gem install on the Linux box for both versions of the gem, as well as 'exception_notifier'. No dice. Thoughts?

Additional info I just tried to reinstall the gem as a plug-in, and got the same errors after changing the production.rb file to use config.middleware.use ::ExceptionNotifier instead. However, those modifications still worked on my local machine. Maybe I'm missing another piece of software on the Linux box? Nothing seems to be mentioned on the documentation...

Request for gem version

I'm using exception_notification, v. 3.0.1

Request for full stacktrace

I, [2013-04-24T17:34:44.113788 #16877]  INFO -- : Refreshing Gem list
E, [2013-04-24T17:34:46.562588 #16877] ERROR -- : uninitialized constant ExceptionNotifier (NameError)
/home/deployer/rl4/releases/20130424201053/config/environments/production.rb:80:in `block in <top (required)>'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:24:in `class_eval'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:24:in `configure'
/home/deployer/rl4/releases/20130424201053/config/environments/production.rb:1:in `<top (required)>'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/engine.rb:571:in `block in <class:Engine>'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `instance_exec'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `run'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:55:in `block in run_initializers'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `each'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `run_initializers'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/application.rb:136:in `initialize!'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:30:in `method_missing'
/home/deployer/rl4/releases/20130424201053/config/environment.rb:5:in `<top (required)>'
config.ru:4:in `require'
config.ru:4:in `block in <main>'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:51:in `instance_eval'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:51:in `initialize'
config.ru:1:in `new'
config.ru:1:in `<main>'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn.rb:44:in `eval'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn.rb:44:in `block in builder'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:722:in `call'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:722:in `build_app!'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:595:in `init_worker_process'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:615:in `worker_loop'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:500:in `spawn_missing_workers'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:511:in `maintain_worker_count'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:277:in `join'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/bin/unicorn:126:in `<top (required)>'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/bin/unicorn:19:in `load'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/bin/unicorn:19:in `<main>'
Brad Rice
  • 1,334
  • 2
  • 17
  • 36
  • But why are you using `exception_notification` and `exception_notifier` at the same time? I think it should be enough just with `exception_notification`. Besides `exception_notifier` is not a requirement for `exception_notification`. The last thing `exception_notifier` and `exception_notification_rails` are outdated gems with almost 3 years since the last version. You definitely should use just `exception_notification`. – Alter Lagos Apr 24 '13 at 14:01
  • Thanks for the comment, but taking out the requirement didn't fix it either. The library still isn't being acknowledged by the app in production on the other server. – Brad Rice Apr 24 '13 at 20:18
  • What `exception_notification` version are you using? What's the full stacktrace error? Have you tried to use `ExceptionNotifier.notify_exception` instead of `ExceptionNotifier::Notifier.exception_notification` ? https://github.com/smartinez87/exception_notification#background-notifications – Alter Lagos Apr 24 '13 at 20:48
  • @AlterLagos I just added in the gem version and stacktrace. I'm not sure if I'm calling the wrong methods anywhere yet because the app won't even start up when I include the configurations in my production.rb. Complaints of an uninitialized constant `ExceptionNotifier` – Brad Rice Apr 25 '13 at 14:12
  • This error appears when you deploy with capistrano, right? Have you tried to execute the app manually in production to see if works? If you run manually in staging mode, but in prod server it works? I had some issues in the past with bundler and capistrano where not all the gems are installed despites that when you login to the server you could see the gem installed. Has worked before in production without the gem? I know too that sometimes the gem order in the Gemfile matters. Are you using rvm or just plain ruby? I'm just guessing things, but I hope one of them gives you a clue – Alter Lagos Apr 25 '13 at 14:40

2 Answers2

1

Here is how you can solve it.

Considring you already have SMTP setting in your development and production.rb

Gemfile

gem 'exception_notification' ,'3.0.0', :require => 'exception_notifier'

application.rb

config.middleware.use ExceptionNotifier,
  :email_prefix => "[ERROR]",
  :sender_address => %{"Exception Notifier" <XYZ@gmail.com>},
  :exception_recipients => %w{ABC@gmail.com}

application_controller.rb

#render 500 error 
def render_error(exception)
  ExceptionNotifier::Notifier.exception_notification(request.env, exception).deliver
  respond_to do |f| 
    ...
  end
end

For me it works both in development and production(Heroku).

Hope it will help you as well.
Cheers:)

TheAshwaniK
  • 1,706
  • 1
  • 14
  • 15
1

It shames me to say that my fix for this problem was so trivial.

$ cap unicorn:stop
$ cap unicorn:start

My restart task in my deployment process was never actually stopping the instance of the server, so the new libraries weren't being loaded, and thus, the exceptions.

Ladies and gents, don't forget to try turning it off and back on again. Ever. Thanks to everyone who helped me through this problem.

Brad Rice
  • 1,334
  • 2
  • 17
  • 36