异常通知常量未初始化 Rails 3

3
我已经阅读了本网站上的许多问题以及 ExceptionNotifier 在 Rails 3 中的 GitHub 文档,但我不知道为什么在我的生产服务器上它不起作用。在我的本地机器上(一台 Mac),生产环境下它能够完美运行。但是,当我使用 capistrano 部署并在 Linux 框架上使用 unicorn 运行应用程序时,我的 unicorn 日志会抱怨我的 environments/production.rb 文件中的uninitialized constant ExceptionNotifier,尽管我的本地机器似乎认为它没有问题。我已经尝试了两种实现方式:gem 'exception_notification',:require => 'exception_notifier'gem 'exception_notification_rails3',:require => 'exception_notifier'。都会导致这个错误。下面是我的 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

这是我的Gemfile文件:
source 'https://rubygems.org'
# lots omitted
gem 'exception_notification_rails3', :require => 'exception_notifier'
# lots more omitted

这是我的application_controller.rb文件,它实现了这个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

目前实现不是我的问题。某种程度上,这个gem没有正确安装或者没有正确初始化。我在Linux盒子上为两个版本的gem以及 'exception_notifier' 运行了sudo gem install命令,但都没有成功。有什么想法吗?

附加信息 我刚刚尝试将gem重新安装为插件,并在更改production.rb文件以使用 config.middleware.use ::ExceptionNotifier 之后得到了相同的错误。然而,这些修改在我的本地机器上仍然有效。也许我在Linux盒子上缺少另一个软件?文档中似乎没有提到任何东西...

请求gem版本

我正在使用exception_notification,版本为3.0.1

请求完整的堆栈跟踪

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>'

但是你为什么同时使用exception_notificationexception_notifier呢?我认为只用exception_notification就足够了。此外,exception_notifier并不是exception_notification的必要组件。最后,exception_notifierexception_notification_rails都是过时的宝石,距离上一个版本已经有近3年时间了。你应该只使用exception_notification - Alter Lagos
谢谢您的评论,但是去掉要求也没有解决问题。在其他服务器上的生产应用程序仍然无法识别该库。 - Brad Rice
你使用的是哪个 exception_notification 版本?完整的堆栈跟踪错误是什么?你尝试过使用 ExceptionNotifier.notify_exception 而不是 ExceptionNotifier::Notifier.exception_notification 吗?https://github.com/smartinez87/exception_notification#background-notifications - Alter Lagos
@AlterLagos 我刚刚添加了gem版本和stacktrace。我还不确定是否在任何地方调用了错误的方法,因为当我在production.rb中包含配置时,应用程序甚至无法启动。抱怨未初始化常量“ExceptionNotifier”。 - Brad Rice
这个错误是在使用capistrano部署时出现的,对吧?你尝试过在生产环境手动执行应用程序来查看是否正常工作吗?如果你在staging模式下手动运行,但在prod服务器上它可以工作吗?我以前遇到过一些与bundler和capistrano相关的问题,其中并没有安装所有的gems,尽管当你登录到服务器时,你可以看到已经安装了gem。之前在生产环境中是否没有这个gem也能正常工作?我也知道有时候Gemfile中gem的顺序很重要。你是使用rvm还是纯ruby?我只是猜测,但希望其中一个能给你一个线索。 - Alter Lagos
2个回答

1

我会尽力帮您进行翻译。

这是如何解决它的方法。

考虑您已经在development和production.rb中设置了SMTP设置

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

对我�说,它在开�和生产�境(赫�克)都有效。

希望它也能帮到你。
干��


1

我很惭愧地说,我解决这个问题的方法非常简单。

$ cap unicorn:stop
$ cap unicorn:start

我的部署过程中重新启动任务实际上从未停止服务器实例,因此新的库未被加载,因此引发了异常。

女士们和先生们,请不要忘记尝试关闭并重新启动。感谢所有帮助我解决这个问题的人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接