diff --git a/InvenTree/InvenTree/ready.py b/InvenTree/InvenTree/ready.py index 64161f1a7a..1f942f3637 100644 --- a/InvenTree/InvenTree/ready.py +++ b/InvenTree/InvenTree/ready.py @@ -19,6 +19,25 @@ def isRunningMigrations(): return any((x in sys.argv for x in ['migrate', 'makemigrations', 'showmigrations'])) +def isInWorkerThread(): + """Returns True if the current thread is a background worker thread.""" + return 'qcluster' in sys.argv + + +def isInServerThread(): + """Returns True if the current thread is a server thread.""" + if isInWorkerThread(): + return False + + if 'runserver' in sys.argv: + return True + + if 'gunicorn' in sys.argv[0]: + return True + + return False + + def isInMainThread(): """Django runserver starts two processes, one for the actual dev server and the other to reload the application. @@ -28,7 +47,7 @@ def isInMainThread(): if 'runserver' in sys.argv and '--noreload' not in sys.argv: return os.environ.get('RUN_MAIN', None) == 'true' - return True + return not isInWorkerThread() def canAppAccessDatabase( diff --git a/InvenTree/InvenTree/tracing.py b/InvenTree/InvenTree/tracing.py index b14dcc4a0d..3d3eda28a3 100644 --- a/InvenTree/InvenTree/tracing.py +++ b/InvenTree/InvenTree/tracing.py @@ -19,6 +19,7 @@ from opentelemetry.sdk.metrics.export import ( from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter +import InvenTree.ready from InvenTree.version import inventreeVersion # Logger configuration @@ -42,6 +43,9 @@ def setup_tracing( resources_input: The resources to send with the traces. console: Whether to output the traces to the console. """ + if InvenTree.ready.isImportingData() or InvenTree.ready.isRunningMigrations(): + return + if resources_input is None: resources_input = {} if auth is None: diff --git a/tasks.py b/tasks.py index 0634a8cfc5..36e6cc03fc 100644 --- a/tasks.py +++ b/tasks.py @@ -672,6 +672,20 @@ def wait(c): return manage(c, 'wait_for_db') +@task(pre=[wait], help={'address': 'Server address:port (default=0.0.0.0:8000)'}) +def gunicorn(c, address='0.0.0.0:8000'): + """Launch a gunicorn webserver. + + Note: This server will not auto-reload in response to code changes. + """ + c.run( + 'gunicorn -c ./docker/gunicorn.conf.py InvenTree.wsgi -b {address} --chdir ./InvenTree'.format( + address=address + ), + pty=True, + ) + + @task(pre=[wait], help={'address': 'Server address:port (default=127.0.0.1:8000)'}) def server(c, address='127.0.0.1:8000'): """Launch a (development) server using Django's in-built webserver.