mirror of
https://github.com/inventree/InvenTree.git
synced 2026-04-03 10:01:07 +00:00
Fix error generation on failed background task (#11552)
* Fix error generation on failed background task * Tweak unit test
This commit is contained in:
@@ -1410,32 +1410,22 @@ def after_failed_task(sender, instance: Task, created: bool, **kwargs):
|
|||||||
"""Callback when a new task failure log is generated."""
|
"""Callback when a new task failure log is generated."""
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
from InvenTree.exceptions import log_error
|
||||||
|
|
||||||
max_attempts = int(settings.Q_CLUSTER.get('max_attempts', 5))
|
max_attempts = int(settings.Q_CLUSTER.get('max_attempts', 5))
|
||||||
n = instance.attempt_count
|
n = instance.attempt_count
|
||||||
|
|
||||||
# Only notify once the maximum number of attempts has been reached
|
# Only notify once the maximum number of attempts has been reached
|
||||||
if not instance.success and n >= max_attempts:
|
if not instance.success and n >= max_attempts:
|
||||||
try:
|
# Create a new Error object associated with this failed task
|
||||||
url = InvenTree.helpers_model.construct_absolute_url(
|
# This will, in turn, trigger a notification to staff users via the Error post_save signal
|
||||||
reverse(
|
|
||||||
'admin:django_q_failure_change', kwargs={'object_id': instance.pk}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
except (ValueError, NoReverseMatch):
|
|
||||||
url = ''
|
|
||||||
|
|
||||||
# Function name
|
log_error(
|
||||||
f = instance.func
|
'task_failure',
|
||||||
|
scope='worker',
|
||||||
notify_staff_users_of_error(
|
error_name='Task Failure',
|
||||||
instance,
|
error_info=f"Task '{instance.pk}' failed after {n} attempts",
|
||||||
'inventree.task_failure',
|
error_data=str(instance.result) if instance.result else '',
|
||||||
{
|
|
||||||
'failure': instance,
|
|
||||||
'name': _('Task Failure'),
|
|
||||||
'message': _(f"Background worker task '{f}' failed after {n} attempts"),
|
|
||||||
'link': url,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -234,11 +234,8 @@ class InvenTreeTaskTests(PluginRegistryMixin, TestCase):
|
|||||||
|
|
||||||
msg = NotificationMessage.objects.last()
|
msg = NotificationMessage.objects.last()
|
||||||
|
|
||||||
self.assertEqual(msg.name, 'Task Failure')
|
self.assertEqual(msg.name, 'Server Error')
|
||||||
self.assertEqual(
|
self.assertEqual(msg.message, 'An error has been logged by the server.')
|
||||||
msg.message,
|
|
||||||
"Background worker task 'InvenTree.tasks.failed_task' failed after 10 attempts",
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_delete_old_emails(self):
|
def test_delete_old_emails(self):
|
||||||
"""Test the delete_old_emails task."""
|
"""Test the delete_old_emails task."""
|
||||||
|
|||||||
Reference in New Issue
Block a user