rake 任务中的 Rails 异常通知程序 [英] Rails exception notifier in rake tasks

查看:18
本文介绍了rake 任务中的 Rails 异常通知程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 rails 应用程序,其中包含一些控制器和一些 rake 任务.一些任务由配置了 whenever gem 的 cron 执行.

I have a simple rails application with a few controller and some rake tasks. A couple of tasks are executed by cron configured with whenever gem.

我的一项任务每天执行,有时会引发异常,默认情况下我会通过 cron 收到此警告

One of my task is executed daily and sometime it raises an exception and by default I receive this warning by cron

rake aborted!
undefined method `parameterize' for nil:NilClass

Tasks: TOP => mailboxes:clean_processed
(See full trace by running task with --trace)

我想调试正在发生的事情,因此我刚刚安装了这个 异常通知 gem在我的 Gemfile

I want to debug what's happening and for this reason I've just installed this exception notification gem with this line in my Gemfile

gem "exception_notification", "~> 2.4.1", :require => 'exception_notifier'

并在我的 application.rb 文件中配置它

and configured it in my application.rb file with

# enable exception notification
config.middleware.use ExceptionNotifier,
                      :email_prefix => "[MyAppName] ",
                      :sender_address => %{"notifier" <report@example.com>},
                      :exception_recipients => %w{me@example.com}

由于这个 gem 是一个机架中间件,它只适用于 Web 请求而不适用于 rake 任务.我也想为 rake 任务启用它,我发现 this gist 可以完成这项工作.

Since this gem is a rack middleware it only works for web requests and not for rake tasks. I'd like to enable it also for rake tasks and I found this gist which do the job.

它有效,但它不是 DRY,我需要在该方法中重复 gem 配置,我还需要更改我所有的 rake 任务以将它们的语句括在一个块中,如

It works, however it's not DRY, I need to repeat gem configuration in that method and I also need to change all my rake tasks to enclose their statements in a block as in

exception_notify { actual_task_code }

有没有更好的方法来解决这个问题?

Is there any better way to solve this?

附言如果我需要更改通知 gem 不会有问题,因为我只在我的项目中添加了几行代码.

P.S. If I need to change the notification gem would not be a problem because I added only a few lines of code to my project.

P.P.S.我知道我也可以更改 crontab 中的耙线以添加 --trace 选项,但我不喜欢该解决方案,因为异常通知程序 imho 是一个更好的解决方案,它也有助于网络代码.

P.P.S. I know that I can also change the rake line in the crontab to add a --trace option, but I don't like that solution, cause exception notifier imho is a better solution which helps also in web code.

更新我刚刚发现了这个相关问题:delayed_job的exception_notification 但两个答案都使用了类似的技巧.

Update I just found out this related question: exception_notification for delayed_job but both the answers use a similar trick.

我将尝试使用 Airbrake(以前称为 hoptoad)或 Exceptional 等在线服务,但它们都是付费服务...

I'm going to try with an online service like Airbrake (formerly known as hoptoad) or Exceptional, but both of them are paid services...

更新 2:我尝试了 Airbrake App,非常好的应用,但它遇到了同样的问题,我仍然需要破解 Rakefile 来通知 rake 任务的异常.然而,hack 并不那么枯燥,因为你只需要这个代码:

Update 2: I tried the Airbrake App, very nice application, but it suffer for the same problem, I still need to hack the Rakefile to notify exceptions from rake tasks. However the hack is less dry because you just need this code:

# notify exceptions
def exception_notify
  yield
rescue Exception => exception
  HoptoadNotifier.notify exception
  raise exception
end

无需重复任何配置参数.我想我没有比这更好的方法来获得 rake 任务中的异常通知了.

There is no need to repeat any configuration parameter. I think I can't do better than this to get notified of exceptions in rake tasks.

推荐答案

Airbrake gem 修补 Rake 以允许救援,所以它已经完成了我的要求......

Airbrake gem patches Rake to allow rescuing, so it already does what I'm asking...

这篇关于rake 任务中的 Rails 异常通知程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆