0

I've set up my Python GAE app so that I get an email when a request handler raises an exception. To do this, I just subclass webapp2.RequestHandler and override handle_exception to send me an email with pertinent details.

Is it possible to get an email when a task in the task queue raises an exception?

EDIT: Adding more details since my original question wasn't clear. :)

I send myself an email for request handler exceptions using the following code:

class MyRequestHandler(webapp2.RequestHandler):
    def handle_exception(self, exception, debug_mode):
        ...
        mail.send_mail_to_admins(sender="me@example.com", ...)
        ...

In this way, I get an email for any exception raised by a request handler.

I would also like to get emails for exceptions raised when I submit tasks using the deferred library. In this case, I don't think I can use a custom request handler. I suppose I could put a wrapper function around all my calls to deferred.defer():

def my_deferred(...):
    try:
        deferred.defer(...)
    except Exception, msg:
        mail.send_mail_to_admins(sender="me@example.com", ...)

Is there a better way to accomplish this?

new name
  • 15,861
  • 19
  • 68
  • 114

1 Answers1

2

Task queue requests are handled exactly the same way as regular requests, so you should be able to use the same approach. (Of course this does not apply when the task queue doesn't get to submit the request to your handler. But that should be rare. And I can't tell from your question what kind of error you see in the logs that you'd like to receive email about. More detailed questions get better answers. :-)

Guido van Rossum
  • 16,690
  • 3
  • 46
  • 49
  • Hi Guido, thank you for prodding me to write a better question. I've elaborated above. – new name Jun 03 '12 at 13:55
  • There isn't a better way that's built into the deferred library or some other part of the SDK, and personally I wouldn't overthink this. For a web app of any scale, receiving an email whenever an exception is hit just doesn't work -- you'd get overwhelmed by the email. It's better just to view the logs occasionally in the admin console. Also note that the chart on the dashboard has a dropdown where you can see the error rate of your app over the past hours and days. – Guido van Rossum Jun 05 '12 at 15:22
  • Thanks, I have a relatively low traffic app where exceptions could be a problem so I may try this anyway. – new name Jun 05 '12 at 21:49