Coverage for src/backend/InvenTree/InvenTree/setting/worker.py: 71%
22 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
1"""Configuration settings for the InvenTree background worker process."""
3import sys
5from InvenTree.config import get_setting
8def get_worker_config(
9 db_engine: str,
10 global_cache: bool = False,
11 sentry_dsn: str = '',
12 debug: bool = False,
13) -> dict:
14 """Return a dictionary of configuration settings for the background worker.
16 Arguments:
17 db_engine: The database engine being used (e.g. 'sqlite', 'postgresql', 'mysql')
18 global_cache: Whether a global redis cache is enabled
19 sentry_dsn: The DSN for sentry.io integration (if enabled)
20 debug: Whether the application is running in debug mode
22 Ref: https://django-q2.readthedocs.io/en/master/configure.html
23 """
24 BACKGROUND_WORKER_TIMEOUT = int(
25 get_setting('INVENTREE_BACKGROUND_TIMEOUT', 'background.timeout', 90)
26 )
28 # Set the retry time for background workers to be slightly longer than the worker timeout, to ensure that workers have time to timeout before being retried
29 BACKGROUND_WORKER_RETRY = max(
30 int(get_setting('INVENTREE_BACKGROUND_RETRY', 'background.retry', 300)),
31 BACKGROUND_WORKER_TIMEOUT + 120,
32 )
34 BACKGROUND_WORKER_ATTEMPTS = int(
35 get_setting('INVENTREE_BACKGROUND_MAX_ATTEMPTS', 'background.max_attempts', 5)
36 )
38 # Prevent running multiple background workers if global cache is disabled
39 # This is to prevent scheduling conflicts due to the lack of a shared cache
40 BACKGROUND_WORKER_COUNT = int(
41 get_setting('INVENTREE_BACKGROUND_WORKERS', 'background.workers', 4)
42 )
44 # If global cache is disabled, we cannot run multiple background workers
45 if not global_cache: 45 ↛ 49line 45 didn't jump to line 49 because the condition on line 45 was always true
46 BACKGROUND_WORKER_COUNT = 1
48 # If running with SQLite, limit background worker threads to 1 to prevent database locking issues
49 if 'sqlite' in db_engine: 49 ↛ 53line 49 didn't jump to line 53 because the condition on line 49 was always true
50 BACKGROUND_WORKER_COUNT = 1
52 # Check if '--sync' was passed in the command line
53 if '--sync' in sys.argv and '--noreload' in sys.argv and debug: 53 ↛ 54line 53 didn't jump to line 54 because the condition on line 53 was never true
54 SYNC_TASKS = True
55 else:
56 SYNC_TASKS = False
58 # Clean up sys.argv so Django doesn't complain about an unknown argument
59 if SYNC_TASKS: 59 ↛ 60line 59 didn't jump to line 60 because the condition on line 59 was never true
60 sys.argv.remove('--sync')
62 # django-q background worker configuration
63 config = {
64 'name': 'InvenTree',
65 'label': 'Background Tasks',
66 'workers': BACKGROUND_WORKER_COUNT,
67 'timeout': BACKGROUND_WORKER_TIMEOUT,
68 'retry': BACKGROUND_WORKER_RETRY,
69 'max_attempts': BACKGROUND_WORKER_ATTEMPTS,
70 'save_limit': 1000,
71 'queue_limit': 50,
72 'catch_up': False,
73 'bulk': 10,
74 'orm': 'default',
75 'cache': 'default',
76 'sync': SYNC_TASKS,
77 'poll': 1.5,
78 }
80 if global_cache: 80 ↛ 82line 80 didn't jump to line 82 because the condition on line 80 was never true
81 # If using external redis cache, make the cache the broker for Django Q
82 config['django_redis'] = 'worker'
84 if sentry_dsn: 84 ↛ 86line 84 didn't jump to line 86 because the condition on line 84 was never true
85 # If sentry is enabled, configure django-q to report errors to sentry
86 config['error_reporter'] = {'sentry': {'dsn': sentry_dsn}}
88 return config