Friday, April 8, 2011

Notify When Rake Task Fails

There is an useful script to notify via email or let hoptoad know whenever a rake tasks fails with any exceptions. It is a small job to how to make it work in your application.

Post the content into your own Rakefile, change FROM_EMAIL and TO_EMAIL.

require 'pony'

EMAIL_FROM = 'from@example.com'
EMAIL_TO = 'to@example.com'

def handle_exception(exception)
$stderr.puts "#{name} aborted!"
$stderr.puts exception.message
$stderr.puts exception.backtrace.join("\n")

# Send email

subject = "Rake exception - #{exception.message}"
body = "#{exception.message}\n\n#{exception.backtrace.join("\n")}"

Pony.mail(:to => EMAIL_TO, :from => EMAIL_FROM, :subject => subject, :body => body)

OR

# Send to hoptoad

HoptoadNotifier.notify(exception)


$stderr.puts
$stderr.puts
$stderr.puts "Exception details sent to #{EMAIL_TO}"
end

module Rake
class Application

def standard_exception_handling
begin
yield
rescue Exception => e
handle_exception(e)
exit(false)
end
end

end
end

No comments:

Post a Comment