Extend timeout for background label printing tasks - #12840
Conversation
✅ Deploy Preview for inventree-web-pui-preview canceled.
|
39c44d8 to
7cd74ee
Compare
|
@wyyd-yxc this structure does not really make sense or fix the repo; please revise |
Label printing tasks which are run in the background worker may legitimately take longer than the default worker timeout (e.g. when printing to a slow remote networked printer). If such a task is killed by the worker timeout it may be re-delivered, causing labels to be printed multiple times. Add a per-task timeout parameter to offload_task() and run label printing tasks with an extended default timeout. Note: the tasks.py portion of this diff includes upstream changes made after the fork this branch is based on was last synced (the fork cannot be synced without the workflow OAuth scope). The changes introduced by this PR in tasks.py are limited to the offload_task() timeout parameter. Fixes inventree#11650
|
@wyyd-yxc stop force pushing, it makes it much harder to find context on comments |
matmair
left a comment
There was a problem hiding this comment.
there seems to be some issue with some code from other branches being included
|
|
||
| ### Changed | ||
|
|
||
| - [#12840](https://github.com/inventree/InvenTree/pull/12840) increases the default timeout for background label printing tasks. Slow print jobs (e.g. when printing to remote networked printers) are no longer killed and re-delivered by the background worker, which could result in labels being printed multiple times. |
|
|
||
| # The first and last individual migrations of each pre-1.0.0 squash range, | ||
| # for every app squashed as part of the pre-1.0.0 migration-history cleanup. | ||
| PRE_1_0_0_MIGRATION_BOUNDARIES = [ |
| machine = self.create_machine('test-label-printer-api') | ||
|
|
||
| # setup the label app | ||
| apps.get_app_config('report').create_default_labels() |
| plugin_module = type( | ||
| plg_registry.get_plugin(plugin_ref, active=None) | ||
| ).print_labels.__globals__['__name__'] | ||
|
|
||
| with mock.patch( | ||
| f'{plugin_module}.offload_task', return_value=None |
There was a problem hiding this comment.
cant you just refer to this statically?
| # Timeout (in seconds) for label printing tasks which are run in the background. | ||
| # Printing (especially to remote networked printers) can take longer than the | ||
| # default background worker timeout. If a print task is killed by the worker | ||
| # timeout it may be re-delivered, resulting in duplicate prints. | ||
| # See https://github.com/inventree/InvenTree/issues/11650 | ||
| LABEL_PRINT_TIMEOUT = 600 |
There was a problem hiding this comment.
I think a short comment and the link is enough
| @@ -220,6 +220,48 @@ def test_printing_process(self): | |||
| # And that it is a valid image file | |||
| Image.open(f'{test_path}.png') | |||
|
|
|||
| def test_async_printing_timeout(self): | |||
There was a problem hiding this comment.
what doe this test offer that other does not?
|
|
||
| ### Breaking Changes | ||
|
|
||
| - [#12830](https://github.com/inventree/InvenTree/pull/12830) squashes all database migrations prior to the 1.0.0 release. This means that any users who are updating from a version older than 1.0.0 must first update to the 1.0.0 release before updating to the current release. |
|
Sorry for the noise here - the branch picked up some unrelated changes from my out-of-date fork, and I made things worse by re-writing the branch history. Closing this for now. If a longer default timeout for label print tasks is wanted on the core side I'm happy to give it another go with a clean single commit. |
|
null |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #12840 +/- ##
==========================================
- Coverage 87.20% 86.84% -0.37%
==========================================
Files 1487 1500 +13
Lines 101455 102462 +1007
Branches 11639 11639
==========================================
+ Hits 88473 88980 +507
- Misses 12919 13419 +500
Partials 63 63
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
the general approach looked right but we can not merge it this way |
Problem
When printing multiple labels at once (e.g. with the Brother label plugin on a remote networked printer), the print job can legitimately take longer than the default background worker timeout (90s). When the worker kills such a task, the queued task can be re-delivered and executed again - resulting in some labels being printed multiple times (and others never, as the output is marked complete). See #11650.
Per-task retries cannot be disabled (django-q2 only supports a global
max_attempts, see django-q2#114), but a custom timeout per task is supported by django-q and honoured by the worker (task.pop("timeout", ...)).Solution
offload_task()gains an optionaltimeoutparameter: when provided (and the task is offloaded asynchronously), it is attached to the queued task as a task-level timeout which overrides the default worker timeout. It is not passed through to the task function itself, and it is ignored for synchronously executed tasks.LABEL_PRINT_TIMEOUT = 600), so slow printer communication no longer trips the worker timeout / re-delivery behaviour.Note on the
tasks.py/CHANGELOG.mddiffThis branch is based on a fork of InvenTree which cannot be synced with upstream (the sync is refused without the
workflowOAuth scope, as upstream's recent commits touch workflow files). Consequently thetasks.pyandCHANGELOG.mddiffs also contain upstream changes made after the fork point (thePRE_1_0_0_MIGRATION_BOUNDARIESaddition and recent changelog entries). The changes introduced by this PR are limited to:offload_task(): the newtimeoutparameter (signature, docstring, and passing it to the queued task)plugin/base/label/mixins.py: theLABEL_PRINT_TIMEOUTconstant and its useplugin/builtin/labels/inventree_machine.py: using the constantHow to test
InvenTree.test_tasks.InvenTreeTaskTests.test_offload_task_timeout: verifies that a custom timeout lands on the queued task (not in the task kwargs), that no timeout key is set without one, and that it is ignored on the synchronous path.plugin.base.label.test_label_mixin.LabelMixinTests.test_async_printing_timeout: verifies non-blocking plugin printing offloads withLABEL_PRINT_TIMEOUT.machine.tests.TestLabelPrinterMachineType.test_print_label_timeout: same assertion for the label machine path.Fixes #11650