rake 任务中的 Rails 异常通知程序

发布于 2024-12-01 05:57:12 字数 2400 浏览 0 评论 0原文

我有一个简单的 Rails 应用程序,带有一些控制器和一些 rake 任务。几个任务由使用 whenever gem 配置的 cron 执行。

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

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

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

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

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

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

# enable exception notification
config.middleware.use ExceptionNotifier,
                      :email_prefix => "[MyAppName] ",
                      :sender_address => %{"notifier" <[email protected]>},
                      :exception_recipients => %w{[email protected]}

因为这gem 是一个机架中间件,它仅适用于 Web 请求,不适用于 rake 任务。我也想为 rake 任务启用它,我发现 这个要点 可以完成这项工作。

它有效,但它不是 DRY,我需要在该方法中重复 gem 配置,并且我还需要更改所有 rake 任务以将其语句包含在块中,如

exception_notify { actual_task_code }

是否有更好的方法来解决此问题?

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

PPS 我知道我还可以更改 crontab 中的 rake 行以添加 --trace 选项,但我不喜欢该解决方案,因为异常通知程序恕我直言是一个更好的解决方案,它也有助于网页代码。

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

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

更新 2:我尝试了 Airbrake App,非常好的应用程序,但它也遇到同样的问题,我仍然需要破解 Rakefile 以通知 rake 任务的异常。然而,黑客不太干,因为你只需要这段代码:

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

不需要重复任何配置参数。我认为我不能做得比这更好,以获取 rake 任务中的异常通知。

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.

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)

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'

and configured it in my application.rb file with

# enable exception notification
config.middleware.use ExceptionNotifier,
                      :email_prefix => "[MyAppName] ",
                      :sender_address => %{"notifier" <[email protected]>},
                      :exception_recipients => %w{[email protected]}

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.

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?

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

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陈独秀 2024-12-08 05:57:12

在 config/initializers 中创建一个 task.rb 文件,该文件会对 Rake::Task#execute 进行修补以包含Exception_notify 的功能:

module Rake
  class Task
    alias :orig_execute :execute
    def execute(args=nil)
      orig_execute(args)
    rescue Exception => exception
      # Exception notification stuff
    end
  end
end

使用 Rails 3.0.12、Rake 0.9.2.2 进行测试。

Create a task.rb file in config/initializers, which monkey patches Rake::Task#execute to include the functionality of exception_notify:

module Rake
  class Task
    alias :orig_execute :execute
    def execute(args=nil)
      orig_execute(args)
    rescue Exception => exception
      # Exception notification stuff
    end
  end
end

Tested with Rails 3.0.12, Rake 0.9.2.2.

兲鉂ぱ嘚淚 2024-12-08 05:57:12

有一个新的更简单的解决方案: gem https://github.com/nikhaldi/exception_notification-rake

御弟哥哥 2024-12-08 05:57:12

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

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

痴骨ら 2024-12-08 05:57:12

感谢您的建议;它对我来说非常有用,因为我目前只有一个 cron 任务。

为了干燥它,您可以将配置转换为常量,也许通过 APP_CONFIG :
https://github.com/cjbottaro/app_config

此外,您可以扩展任何负责处理的类 < code>task :... do 将所有内容包装在 exception_notify { } 中。

Thanks for this suggestion; it works great for me since I only have one cron task at the moment.

To DRY it up, you could turn the configuration into constants, perhaps via APP_CONFIG:
https://github.com/cjbottaro/app_config

In addition, you could extend whatever class takes care of task :... do to wrap everything in exception_notify { }.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文