How to manage error notifications for background tasks

When running background tasks it is inevitable that one will fail occasionally for various reasons. How do y’all manage notifications for this?

If I wrap the task in a try/except block and send out an email if an exception is found I’m not sure how to collect the traceback so I can debug it.

If I let it fail and don’t remember to check it periodically (as well all get wrapped up in other projects) leaving it there is not good.

So I’m open to suggestions.

Perhaps a good start would be to keep a database table to log these events. Database table rows don’t get lost. They’re only deleted on your say-so.

If there’s a way to collect the traceback, then it can probably be added in a Text column, or (if it’s really big) a Media column.

Here is what I use to send myself an email from an uplink that has communication errors: How to handle unexpected disconnect in Uplink program?

Thanks for the link. I could definitely catch specific exceptions, but catching all exceptions is what I really need. I’m researching it a bit and think I found a way.

This seems to mostly be a Python question and not an Anvil question.

Python has a sys.__excepthook__ that should do the trick. I’ll try to implement it tomorrow morning.

The except Exception as e: will catch all exceptions and the f'Error:<br>\n{traceback.format_exc()} will include in the email the whole traceback the way you would see it in the console, including the type of the exception.

3 Likes

Ah that’s it. Thank you that makes perfect sense.

EDIT: here is my code that works. I threw in print(traceback.format_exc()) so the traceback would also be in the app logs.

import traceback
@anvil.server.background_task
def function_name():
    try:
        #code here
    except Exception as e:
        print(traceback.format_exc())
        send_error_email(f"""Scheduled Task function_name encountered an error.
                                    \n{traceback.format_exc()}""")

EDIT2: A BIG caveat here is that the scheduled tasks no longer show as failed in the app log with the code above. To ensure the app log has the exception raised you can add a line to raise an exception so it will show in the app log correctly. This also allows me to remove the print statement with the traceback. Here is my final code:

import traceback
@anvil.server.background_task
def function_name():
    try:
        #code here
    except Exception as e:
        send_error_email(f"""Scheduled Task function_name encountered an error.
                         \n{traceback.format_exc()}""")
        raise Exception(traceback.format_exc())

Nice.
I think your last line could be a simpler raise and Python will raise whatever exception is managing.

2 Likes

Very true! Thanks for pointing this out.