Faster management commands

This commit is contained in:
Erik Kalkoken 2023-01-25 03:25:47 +00:00 committed by Ariel Rin
parent 57f7178f1e
commit 31c1f8bb7d
3 changed files with 57 additions and 5 deletions

View File

@ -8,6 +8,7 @@ from django.conf import settings
from esi.clients import esi_client_factory from esi.clients import esi_client_factory
from allianceauth import __version__ from allianceauth import __version__
from allianceauth.utils.django import StartupCommand
SWAGGER_SPEC_PATH = os.path.join(os.path.dirname( SWAGGER_SPEC_PATH = os.path.join(os.path.dirname(
@ -175,15 +176,16 @@ class EveProvider:
class EveSwaggerProvider(EveProvider): class EveSwaggerProvider(EveProvider):
def __init__(self, token=None, adapter=None): def __init__(self, token=None, adapter=None):
if settings.DEBUG: if settings.DEBUG or StartupCommand().is_management_command:
self._client = None self._client = None
logger.info( logger.info('ESI client will be loaded on-demand')
'DEBUG mode detected: ESI client will be loaded on-demand.'
)
else: else:
logger.info('Loading ESI client')
try: try:
self._client = esi_client_factory( self._client = esi_client_factory(
token=token, spec_file=SWAGGER_SPEC_PATH, app_info_text=("allianceauth v" + __version__) token=token,
spec_file=SWAGGER_SPEC_PATH,
app_info_text=f"allianceauth v{__version__}"
) )
except (HTTPError, RefResolutionError): except (HTTPError, RefResolutionError):
logger.exception( logger.exception(

View File

@ -0,0 +1,25 @@
import sys
from copy import copy
from pathlib import Path
class StartupCommand:
"""Information about the command this Django instance was started with."""
def __init__(self) -> None:
self._argv = copy(sys.argv)
@property
def argv(self) -> list:
"""Return raw list of command line arguments."""
return self._argv
@property
def script_name(self) -> str:
"""Return the base script name."""
path = Path(self._argv[0])
return path.name
@property
def is_management_command(self) -> bool:
return self.script_name == "manage.py"

View File

@ -0,0 +1,25 @@
from unittest.mock import patch
from django.test import TestCase
from allianceauth.utils.django import StartupCommand
MODULE_PATH = "allianceauth.utils.django"
class TestStartupCommand(TestCase):
def test_should_detect_management_command(self):
# when
with patch(MODULE_PATH + ".sys") as m:
m.argv = ["manage.py", "check"]
info = StartupCommand()
# then
self.assertTrue(info.is_management_command)
def test_should_detect_not_a_management_command(self):
# when
with patch(MODULE_PATH + ".sys") as m:
m.argv = ['/home/python/allianceauth-dev/venv/bin/gunicorn', 'myauth.wsgi']
info = StartupCommand()
# then
self.assertFalse(info.is_management_command)