Hey, I’ve been experimenting with moving to hosting our moderately large Django installation into ASGI hosting mode, running on uvicorn. On a few of our projects everything seemed to be fine, but I have one where we have an issue with our setup. It appears that starting up in asgi mode, it doesn’t like the direct ORM call in our accounts app setup, where we ensure that one of our feature flags exists.
I’m not sure what the recommended way to do this is, or if there’s something obvious we’re doing wrong. The traceback we get on startup is below:
(apologies for image, the Stackdriver logs aren’t making it easy to export as text)
The asgi.py file looks like:
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
os.environ.setdefault("REQUESTS_CA_BUNDLE", "/etc/ssl/certs/ca-certificates.crt")
os.environ.setdefault(
"NODE_PATH", "/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript"
)
application = get_asgi_application()
and the app.ready in question is:
from contextlib import suppress
from django.apps import AppConfig
from django.db import OperationalError
from django.db import ProgrammingError
class AccountsConfig(AppConfig):
name = "accounts"
def ready(self):
from common.models import FeatureFlag
with suppress(ProgrammingError, OperationalError):
FeatureFlag.objects.get_or_create(
name="SMS_SPECIFIC_TIME",
defaults={
"enabled": True,
"description": "Send SMS at the time (24H) that is declared in data", # noqa
"data": {"time": "20:00"},
},
)
import accounts.signals # noqa
import accounts.messages # noqa
Would greatly appreciate any advice on what we should be doing differently!