Ë
    LµCjÉ  ã                   óv   — d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZ  ee«      Zdadad„ Zed	„ «       Zy)
uò  
Synchronous database session â€” used exclusively by Celery worker tasks.

Celery tasks in this project are plain synchronous functions (see
app/workers/tasks.py). Reusing the async engine from app/db/session.py
inside a sync Celery task would require running an event loop inside
a thread that Celery itself manages, which is a well-known source of
"event loop already running" bugs. A dedicated sync engine with psycopg2
sidesteps that entirely, at the cost of maintaining two engines â€” a
trade worth making for reliability.

The engine is created lazily on first use, not at module import time.
This matters because app/workers/tasks.py is imported by the API
process too (analyze.py calls run_crop_analysis.delay()), and an
eagerly-created engine would force the API process to construct a
working sync DB connection just to import the module â€” unnecessary
coupling, and a real failure mode if the sync and async DSNs ever need
to differ (e.g. different connection pooling strategy per process role).
é    )Úcontextmanager)Úcreate_engine)ÚSessionÚsessionmaker)Úget_settings)Ú
get_loggerNc                  óä   — t         €et        «       } | j                  j                  dd«      }t	        || j
                  | j                  | j                  d¬«      a t        t         d¬«      a	t        S )Nz+asyncpgz	+psycopg2T)Ú	pool_sizeÚmax_overflowÚechoÚpool_pre_pingF)ÚbindÚexpire_on_commit)
Ú_sync_enginer   ÚDATABASE_URLÚreplacer   ÚDB_POOL_SIZEÚDB_MAX_OVERFLOWÚDB_ECHOr   Ú_SyncSessionFactory)ÚsettingsÚsync_dsns     ú@C:\Crop_Prediction\Backend\crop-ai-system\app\db\sync_session.pyÚ_get_sync_enginer   #   sg   € äÐÜ“>ˆà×(Ñ(×0Ñ0°¸[ÓIˆÜ$ØØ×+Ñ+Ø!×1Ñ1Ø×!Ñ!Øô
ˆô +´ÈuÔUÐÜÐó    c               #   óà   K  — t        «       }  | «       }	 |–— |j                  «        	 |j	                  «        y # t        $ r |j                  «        ‚ w xY w# |j	                  «        w xY w­w)N)r   ÚcommitÚ	ExceptionÚrollbackÚclose)Úsession_factoryÚsessions     r   Úsync_db_sessionr#   4   s`   è ø€ ä&Ó(€OÙ&Ó(€GðØŠØ�‰Õð
 	�‰�øô	 ò Ø×ÑÔØðûð 	�‰�üs%   ‚A.•; ªA.»AÁA ÁA+Á+A.)Ú__doc__Ú
contextlibr   Ú
sqlalchemyr   Úsqlalchemy.ormr   r   Úapp.core.configr   Úapp.core.logging_configr   Ú__name__Úloggerr   r   r   r#   © r   r   ú<module>r-      sI   ðñõ( &å $ß 0å (Ý .á	�HÓ	€à€ØÐ òð" ñ
ó ñ
r   