django-background-tasks
django-background-tasks copied to clipboard
Warn admin if failed attempt exceeds MAX_ATTEMPTS.
User email_admins
or other build-in tools.
Also email admin if an error occurs in execution of task (same as if a 500 server error occurred basically).
Is there any way to actually do this? I mean, is it possible to email the admins when a background task fails?
There certainly should be a flag in the very least (in the decorator) so that you can set that you want to receive an email in case of error.
Overall I have been using the package for about a year now and I'm extremely happy with it. This is the only thing that bothers me a little bit.
I've implemented this using signals (Django 2.2)
from background_task.signals import task_failed
from django.dispatch import receiver
from django.core.mail import mail_admins
task_failed_message = """
Hi, this is the Background Task notification service
The following task has failed and will not be re-tried.
Task: {task_id}
Traceback follows:
{traceback}
"""
@receiver(task_failed)
def task_failed_callback(sender, **kwargs):
completed_task = kwargs.get('completed_task')
if completed_task:
if completed_task.has_error():
message = task_failed_message.format(
task_id=completed_task.id,
traceback=completed_task.last_error
)
mail_admins(
subject="Background Task Error",
message=message,
)
https://simpleisbetterthancomplex.com/tutorial/2016/07/28/how-to-create-django-signals.html has pointers on how to integrate this into your code.
Hope this helps!
@craigmaloney thanks a lot! I will look into it.