from django.apps import AppConfig # Management commands that boot Django but don't need (and shouldn't trigger) # the heavy ModelRegistry init. collectstatic in particular runs at image # build time and would otherwise pull ~6 GB of checkpoints into a throwaway # layer; the migration / shell ones are dev conveniences. _SKIP_REGISTRY_INIT = { "collectstatic", "makemigrations", "migrate", "check", "shell", "showmigrations", "diffsettings", "test", "compilemessages", "makemessages", } class ApiConfig(AppConfig): name = "api" default_auto_field = "django.db.models.BigAutoField" def ready(self): import os import sys # Skip registry init for management commands that don't serve requests. if len(sys.argv) > 1 and sys.argv[1] in _SKIP_REGISTRY_INIT: return # Django's runserver auto-reloader spawns two processes, both calling ready(). # The inner (serving) process has RUN_MAIN="true"; the outer (watcher) does not. # Skip initialization in the outer process to avoid double memory usage. uses_reloader = "runserver" in sys.argv and "--noreload" not in sys.argv if uses_reloader and os.environ.get("RUN_MAIN") != "true": return from api.services.registry import ModelRegistry ModelRegistry.initialize()