Repository navigation
Background Task Features - #12913
Background Task Features#12913
Conversation
✅ Deploy Preview for inventree-web-pui-preview canceled.
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #12913 +/- ##
==========================================
+ Coverage 87.30% 87.34% +0.03%
==========================================
Files 1506 1506
Lines 103092 103334 +242
Branches 11582 11582
==========================================
+ Hits 90008 90252 +244
+ Misses 13019 13017 -2
Partials 65 65
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
@SchrodingersGat :
IMO refusing timeout longer than retry interval is OK for InvenTree; long print job is now primary candidate for extended timeout, but few minutes shall be enough. It is also possible to subclass ORM broker and override
This is mainly policy choice +
Moving @receiver(post_execute, sender='django_q', dispatch_uid='failure_post_execute_notification')
def after_failed_task(sender, task, **kwargs):
"""Notify staff when a saved task failure will not be retried."""
from InvenTree.exceptions import log_error
if task['success']: # this execution attempt succeeded
return
instance = Task.objects.filter(pk=task['id'], success=False).first()
if instance is None: # Task succeeded (in other execution)
return
n = instance.attempt_count
limit_reached = Conf.MAX_ATTEMPTS > 0 and n >= Conf.MAX_ATTEMPTS
if not (task.get('ack_failure', False) or limit_reached):
return
# Create a new Error object associated with this failed task
# This will, in turn, trigger a notification to staff users via the Error post_save signal
message = f"Task '{instance.func} ({instance.pk})' failed after {n} attempts"
logger.error(message)
log_error(
'task_failure',
scope='worker',
error_name='Task Failure',
error_info=message,
error_data=str(instance.result) if instance.result else '',
) |
| Note: | ||
| django-q2 has no concept of a per-task retry limit: the ORM broker (which | ||
| InvenTree always uses) simply leaves a failed task's queue entry in place, so it | ||
| gets redelivered indefinitely (governed by the cluster-wide 'retry' timeout) |
There was a problem hiding this comment.
(indefinitely -> global cluster limit still applies)
| task_kwargs = dict(kwargs) | ||
| if not retry: | ||
| # Bandaid for django-q2 having no per-task retry limit: 'ack_failure' | ||
| # is its one native per-task option that acknowledges (and so drops) | ||
| # a task as soon as it fails, rather than leaving it to be redelivered | ||
| task_kwargs['ack_failure'] = True | ||
| if timeout is not None: | ||
| task_kwargs['timeout'] = timeout | ||
|
|
||
| task = AsyncTask(taskname, *args, group=group, **task_kwargs) |
There was a problem hiding this comment.
(q_options will reduce task argument collision a bit, but offload_task signature / kwargs handling will consume args sooner)
| task_kwargs = dict(kwargs) | |
| if not retry: | |
| # Bandaid for django-q2 having no per-task retry limit: 'ack_failure' | |
| # is its one native per-task option that acknowledges (and so drops) | |
| # a task as soon as it fails, rather than leaving it to be redelivered | |
| task_kwargs['ack_failure'] = True | |
| if timeout is not None: | |
| task_kwargs['timeout'] = timeout | |
| task = AsyncTask(taskname, *args, group=group, **task_kwargs) | |
| task_kwargs = dict(kwargs) | |
| q_options = dict(task_kwargs.pop('q_options', {})) | |
| q_options['group'] = group | |
| if not retry: | |
| # Bandaid for django-q2 having no per-task retry limit: 'ack_failure' | |
| # is its one native per-task option that acknowledges (and so drops) | |
| # a task as soon as it fails, rather than leaving it to be redelivered | |
| q_options['ack_failure'] = True | |
| if timeout is not None: | |
| q_options['timeout'] = timeout | |
| task = AsyncTask(taskname, *args, q_options=q_options, **task_kwargs) | |
| task['ack_failure'] = True | ||
|
|
||
| if timeout is not None: | ||
| task['timeout'] = timeout |
There was a problem hiding this comment.
This is probably good spot to fail when timeout is longer than configured retry interval.
| fails (see note below) | ||
| timeout: Optional per-task override (in seconds) of the worker's task timeout | ||
| timeout: Optional per-task override (in seconds) of the worker's task timeout. | ||
| Clamped down (with a warning) if it would leave less than 120s of headroom |
|
@SchrodingersGat : I updated #12913 (comment); most cases are handed; look at |
This PR introduces two new features for background tasks:
Optional Single-Shot Mode
offload_taskcan now be called withretry=Falseto disable retry on failure. This can be used for tasks which should only run once.Utilizes the ack_failure task attribute from django-q
Per-Task Timeout
Introduces per-task timeout configuration. Utilizes the timeout task attribute from django-q
Once this is merged, we could apply this to label and report generation - potentially closing #11650