python – Celery错误电子邮件:无法接收芹菜电子邮件

我是一个PythonDjango和芹菜初学者,我想在当地设置芹菜.现在,我正在为所有失败任务设置错误电子邮件.我所做的就是这个

将这些代码添加到setting.py中

CELERY_SEND_TASK_ERROR_EMAILS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
ADMINS = (
    ('test', '...@....com'),
)
EMAIL_HOST='smtp.gmail.com'
EMAIL_HOST_USER='...@gmail.com'
EMAIL_HOST_PASSWORD='...'
EMAIL_PORT=587
EMAIL_USE_TLS = True`

在tasks.py中添加它

@app.task(name="test_exception",error_whitelist=[])

最佳答案 对于在4.0版之后尝试执行此操作的任何人,CELERY_SEND_TASK_ERROR_EMAILS显然已被删除(
source):

Tasks no longer sends error emails. This also removes support for app.mail_admins, and any functionality related to sending emails.

一种可能的解决方案是向任务装饰者添加电子邮件发送:

def shared_task_email(func):
    """
    Replacement for @shared_task decorator that emails admins if an exception is raised.
    """
    @wraps(func)
    def new_func(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except:
            subject = "Celery task failure"
            message = traceback.format_exc()
            mail_admins(subject, message)
            raise
    return shared_task(new_func)

@shared_task_email # instead of @shared_task
def test_task():
    raise Exception("Test exception raised by celery test_task")
点赞