Rails exception notifier in rake tasks

佐手、 提交于 2019-11-29 01:28:37

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!