diff --git a/cmsmanage/django_q2_admin_email_reporter.py b/cmsmanage/django_q2_admin_email_reporter.py new file mode 100644 index 0000000..6350a9c --- /dev/null +++ b/cmsmanage/django_q2_admin_email_reporter.py @@ -0,0 +1,20 @@ +import sys + +from django.views.debug import ExceptionReporter +from django.core import mail + + +class AdminEmailReporter: + def __init__(self, **kwargs): + pass + + def report(self): + reporter = ExceptionReporter(None, *sys.exc_info()) + message = f"Failed to run task\n{reporter.get_traceback_text()}" + html_message = reporter.get_traceback_html() + mail.mail_admins( + "Django Q task failed", + message, + # fail_silently=True, + html_message=html_message, + ) diff --git a/cmsmanage/django_q2_helper.py b/cmsmanage/django_q2_helper.py index 1a1659a..533d49a 100644 --- a/cmsmanage/django_q2_helper.py +++ b/cmsmanage/django_q2_helper.py @@ -1,5 +1,3 @@ -import sys - from django_q.models import Schedule diff --git a/cmsmanage/settings/base.py b/cmsmanage/settings/base.py index f643460..c4a8bbd 100644 --- a/cmsmanage/settings/base.py +++ b/cmsmanage/settings/base.py @@ -124,6 +124,7 @@ Q_CLUSTER = { "retry": 60 * 6, "timeout": 60 * 5, "catch_up": False, + "error_reporter": {"admin_email": {}}, "ALT_CLUSTERS": { "internal": { "retry": 60 * 60, diff --git a/pyproject.toml b/pyproject.toml index 4401f37..a9763ce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,6 +35,9 @@ server = [ "setuptools~=68.2", ] +[project.entry-points."djangoq.errorreporters"] +admin_email = "cmsmanage.django_q2_admin_email_reporter:AdminEmailReporter" + [tool.black] line-length = 88