From 16310617bea55bbf8b409e4ef6abf22c9b57ddea Mon Sep 17 00:00:00 2001 From: gunstr <41468206+gunstr@users.noreply.github.com> Date: Fri, 10 Apr 2026 03:38:35 +0200 Subject: [PATCH] Worker debugging (#11677) * Add debugging info for background workers to the devcontainer docs * Add debugging info the the EventMixin docs * Add an option to set sync=True to launch.json --- .vscode/launch.json | 4 +++- docs/docs/develop/devcontainer.md | 7 ++++++- docs/docs/plugins/mixins/event.md | 3 +++ src/backend/InvenTree/InvenTree/settings.py | 12 +++++++++++- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index e32e7beeb8..17186b6d37 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -36,7 +36,9 @@ "program": "${workspaceFolder}/src/backend/InvenTree/manage.py", "args": [ "runserver", - "0.0.0.0:8000" + "0.0.0.0:8000", + // "--sync",// Synchronize worker tasks to foreground thread + // "--noreload", // disable auto-reload ], "django": true, "justMyCode": false diff --git a/docs/docs/develop/devcontainer.md b/docs/docs/develop/devcontainer.md index a7d2f8e1f6..a1d9721943 100644 --- a/docs/docs/develop/devcontainer.md +++ b/docs/docs/develop/devcontainer.md @@ -60,7 +60,12 @@ If you only need a superuser, run the `superuser` task. It should prompt you for #### Run background workers -If you need to process your queue with background workers, run the `worker` task. This is a foreground task which will execute in the terminal. +If you need to process your queue with background workers, open a new terminal and run the `worker` task with `invoke worker`. This is a foreground task which will execute in the terminal. + +If you are developing functions that will be executed by background workers there are a two debugging options. + +- If the workers are started with the `worker` task you can add `print` or `logger` statements to the code and monitor the output in the terminal. +- All tasks can be forced to run in the foreground worker by uncommenting the `--sync` and `--noreload` arguments under the `InvenTree Server - 3rd party` entry in `.vscode/launch.json`. With this setting you should not start a separate background worker, instead you start the `InvenTree Server - 3rd party` from the `Run and Debug` side panel. All task will now run in one single process and you can set breakpoints, inspect variables and single step also tasks that normally are offloaded to background workers. It should be noted that with this setting the GUI will be unresponsive while tasks are executed. ### Running InvenTree diff --git a/docs/docs/plugins/mixins/event.md b/docs/docs/plugins/mixins/event.md index c2f048b055..f3f61a0d1e 100644 --- a/docs/docs/plugins/mixins/event.md +++ b/docs/docs/plugins/mixins/event.md @@ -13,6 +13,9 @@ When a certain (server-side) event occurs, the background worker passes the even {{ image("plugin/enable_events.png", "Enable event integration") }} +!!! info "Worker debugging" + As the events are offloaded to a background worker debugging the `process_event()` function need some extra consideration. Please see the [Run background workers](../../develop/devcontainer.md#run-background-workers) section for further information. + ## Events Events are passed through using a string identifier, e.g. `build.completed` diff --git a/src/backend/InvenTree/InvenTree/settings.py b/src/backend/InvenTree/InvenTree/settings.py index 7c617ff47d..444919d6b8 100644 --- a/src/backend/InvenTree/InvenTree/settings.py +++ b/src/backend/InvenTree/InvenTree/settings.py @@ -865,6 +865,16 @@ BACKGROUND_WORKER_ATTEMPTS = int( get_setting('INVENTREE_BACKGROUND_MAX_ATTEMPTS', 'background.max_attempts', 5) ) +# Check if '--sync' was passed in the command line +if '--sync' in sys.argv and '--noreload' in sys.argv and DEBUG: + SYNC_TASKS = True +else: + SYNC_TASKS = False + +# Clean up sys.argv so Django doesn't complain about an unknown argument +if SYNC_TASKS: + sys.argv.remove('--sync') + # django-q background worker configuration Q_CLUSTER = { 'name': 'InvenTree', @@ -879,7 +889,7 @@ Q_CLUSTER = { 'bulk': 10, 'orm': 'default', 'cache': 'default', - 'sync': False, + 'sync': SYNC_TASKS, 'poll': 1.5, }