django-background-tasks icon indicating copy to clipboard operation
django-background-tasks copied to clipboard

Warn admin if failed attempt exceeds MAX_ATTEMPTS.

Open philippeowagner opened this issue 9 years ago • 5 comments

User email_admins or other build-in tools.

philippeowagner avatar Jul 28 '15 20:07 philippeowagner

Also email admin if an error occurs in execution of task (same as if a 500 server error occurred basically).

emihir0 avatar Jan 17 '19 21:01 emihir0

Is there any way to actually do this? I mean, is it possible to email the admins when a background task fails?

nicolazilio avatar Nov 21 '19 16:11 nicolazilio

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.

emihir0 avatar Nov 25 '19 11:11 emihir0

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 avatar Sep 29 '20 20:09 craigmaloney

@craigmaloney thanks a lot! I will look into it.

nicolazilio avatar Sep 30 '20 08:09 nicolazilio