
    J&jk	                     &    d dl mZ  G d de      Zy)    )	AppConfigc                       e Zd ZdZdZddZy)ServicesConfigzdjango.db.models.BigAutoFieldservicesNc                 J    ddl m}m} 	 ddlm}  |        y# ||f$ r Y yw xY w)u  
        Load the country snapshot into memory at Django startup.

        This runs once per process:
          - gunicorn pre-fork:  master process loads, workers inherit via fork.
          - gunicorn post-fork: each worker calls ready() independently.
          - Kubernetes replicas: each pod loads independently from the same DB.

        Failure policy
        --------------
        We distinguish two categories of startup failure:

        1. MIGRATION STATE (table does not exist yet):
           django.db.OperationalError / ProgrammingError are caught and
           silently ignored.  The snapshot will NOT be loaded, and any
           attempt to call resolve_istat_country_code() before the table
           exists will raise SnapshotNotLoadedError — which is the correct
           behavior during `manage.py migrate`.

        2. ALL OTHER FAILURES (DB reachable, table exists, data invalid):
           RuntimeError from _validate_snapshot() is NOT caught here.
           The process will fail to start with a clear error message.
           This is intentional — a partially-initialized country reference
           is a fatal misconfiguration, not a recoverable runtime error.

        Multi-worker safety
        -------------------
        The snapshot is loaded per process.  No shared memory is assumed
        across nodes or Kubernetes replicas.  Each worker/pod loads
        independently from the same DB at startup.
        r   )OperationalErrorProgrammingError)load_country_snapshotN)	django.dbr   r	   services.country_snapshotr
   )selfr   r	   r
   s       /backend/services/apps.pyreadyzServicesConfig.ready   s1    B 	A	G!# "23 	
 	s    "")returnN)__name__
__module____qualname__default_auto_fieldnamer        r   r   r      s    8D+r   r   N)django.appsr   r   r   r   r   <module>r      s    !/Y /r   