
    LCj                     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   