diff --git a/InvenTree/InvenTree/tasks.py b/InvenTree/InvenTree/tasks.py index da257f49cf..90deaaa225 100644 --- a/InvenTree/InvenTree/tasks.py +++ b/InvenTree/InvenTree/tasks.py @@ -61,7 +61,7 @@ def raise_warning(msg): warnings.warn(msg) -def offload_task(taskname, *args, force_sync=False, **kwargs): +def offload_task(taskname, *args, force_async=False, force_sync=False, **kwargs): """Create an AsyncTask if workers are running. This is different to a 'scheduled' task, in that it only runs once! If workers are not running or force_sync flag @@ -79,7 +79,7 @@ def offload_task(taskname, *args, force_sync=False, **kwargs): except (OperationalError, ProgrammingError): # pragma: no cover raise_warning(f"Could not offload task '{taskname}' - database not ready") - if is_worker_running() and not force_sync: # pragma: no cover + if force_async or (is_worker_running() and not force_sync): # Running as asynchronous task try: task = AsyncTask(taskname, *args, **kwargs) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 6f9db50ee5..4347e5ebac 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -20,9 +20,11 @@ from djmoney.money import Money import InvenTree.tasks from common.models import InvenTreeSetting from common.settings import currency_codes -from stock.models import StockLocation +from part.models import Part, PartCategory +from stock.models import StockItem, StockLocation from . import config, helpers, ready, status, version +from .tasks import offload_task from .validators import validate_overage, validate_part_name @@ -618,3 +620,48 @@ class TestInstanceName(helpers.InvenTreeTestCase): # The site should also be changed site_obj = Site.objects.all().order_by('id').first() self.assertEqual(site_obj.domain, 'http://127.1.2.3') + + +class TestOffloadTask(helpers.InvenTreeTestCase): + """Tests for offloading tasks to the background worker""" + + fixtures = [ + 'category', + 'part', + 'location', + 'stock', + ] + + def test_offload_tasks(self): + """Test that we can offload various tasks to the background worker thread. + + This set of tests also ensures that various types of objects + can be encoded by the django-q serialization layer! + + Note that as the background worker is not actually running for the tests, + the call to 'offload_task' won't really *do* anything! + + However, it serves as a validation that object serialization works! + + Ref: https://github.com/inventree/InvenTree/pull/3273 + """ + + offload_task( + 'dummy_tasks.parts', + part=Part.objects.get(pk=1), + cat=PartCategory.objects.get(pk=1), + force_async=True + ) + + offload_task( + 'dummy_tasks.stock', + item=StockItem.objects.get(pk=1), + loc=StockLocation.objects.get(pk=1), + force_async=True + ) + + offload_task( + 'dummy_task.numbers', + 1, 2, 3, 4, 5, + force_async=True + ) diff --git a/requirements.in b/requirements.in index 26353cdaf3..844ffe6b3b 100644 --- a/requirements.in +++ b/requirements.in @@ -18,7 +18,7 @@ django-redis>=5.0.0 # Redis integration django-q # Background task scheduling django-sql-utils # Advanced query annotation / aggregation django-sslserver # Secure HTTP development server -django-stdimage # Advanced ImageField management +django-stdimage<6.0.0 # Advanced ImageField management # FIXED 2022-06-29 6.0.0 breaks serialization for django-q django-user-sessions # user sessions in DB django-weasyprint # django weasyprint integration djangorestframework # DRF framework diff --git a/requirements.txt b/requirements.txt index 967f860faf..cfb733a184 100644 --- a/requirements.txt +++ b/requirements.txt @@ -107,7 +107,7 @@ django-sql-utils==0.6.1 # via -r requirements.in django-sslserver==0.22 # via -r requirements.in -django-stdimage==6.0.1 +django-stdimage==5.3.0 # via -r requirements.in django-user-sessions==1.7.1 # via -r requirements.in