File size: 1,351 Bytes
0b4eacc
 
b598a6a
 
 
 
 
 
 
 
 
 
0b4eacc
 
 
 
 
 
4f1e196
 
 
b598a6a
 
 
 
4f1e196
 
 
 
 
 
 
0b4eacc
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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()