Rails exception notifier in rake tasks

后端 未结 4 1243
余生分开走
余生分开走 2020-12-15 22:16

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 e

相关标签:
4条回答
  • 2020-12-15 22:29

    there is new easier solution: gem https://github.com/nikhaldi/exception_notification-rake

    0 讨论(0)
  • 2020-12-15 22:35

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

    0 讨论(0)
  • 2020-12-15 22:38

    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.

    0 讨论(0)
  • 2020-12-15 22:49

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

    0 讨论(0)
提交回复
热议问题