diff --git a/aciniformes_backend/worker/scheduler.py b/aciniformes_backend/worker/scheduler.py index 45becb6..1f97974 100644 --- a/aciniformes_backend/worker/scheduler.py +++ b/aciniformes_backend/worker/scheduler.py @@ -14,7 +14,7 @@ from aciniformes_backend.settings import get_settings from .ping import ping -from .session import dbsession +from .session import session_factory logger = logging.getLogger(__name__) @@ -50,7 +50,8 @@ async def start(self): seconds=self.settings.FETCHERS_UPDATE_DELAY_IN_SECONDS, trigger="interval", ) - self.fetchers = dbsession().query(Fetcher).all() + with session_factory() as session: + self.fetchers = session.query(Fetcher).all() self.scheduler.start() for fetcher in self.fetchers: self.add_fetcher(fetcher) @@ -65,11 +66,10 @@ def stop(self): self.scheduler.shutdown() async def write_alert(self, alert: AlertCreateSchema): - receivers = dbsession().query(Receiver).all() - session = dbsession() - alert = Alert(**alert.model_dump(exclude_none=True)) - session.add(alert) - session.flush() + with session_factory() as session: + receivers = session.query(Receiver).all() + alert = Alert(**alert.model_dump(exclude_none=True)) + session.add(alert) for receiver in receivers: async with aiohttp.ClientSession() as s: async with s.request(method=receiver.method, url=receiver.url, data=receiver.receiver_body): @@ -83,7 +83,8 @@ def _parse_timedelta(fetcher: Fetcher) -> tuple[int, int]: async def __update_fetchers(self) -> AsyncIterator[None]: jobs = [job.id for job in self.scheduler.get_jobs()] old_fetchers = self.fetchers - new_fetchers = dbsession().query(Fetcher).all() + with session_factory() as session: + new_fetchers = session.query(Fetcher).all() # Проверка на удаление фетчера for fetcher in old_fetchers: @@ -144,10 +145,9 @@ async def _process_fail( self._reschedule_job(fetcher, False) def add_metric(self, metric: MetricCreateSchema): - session = dbsession() - metric = Metric(**metric.model_dump(exclude_none=True)) - session.add(metric) - session.commit() + with session_factory() as session: + metric = Metric(**metric.model_dump(exclude_none=True)) + session.add(metric) return metric async def _fetch_it(self, fetcher: Fetcher): diff --git a/aciniformes_backend/worker/session.py b/aciniformes_backend/worker/session.py index 6917d32..c346b86 100644 --- a/aciniformes_backend/worker/session.py +++ b/aciniformes_backend/worker/session.py @@ -1,5 +1,5 @@ from sqlalchemy import create_engine -from sqlalchemy.orm import Session, sessionmaker +from sqlalchemy.orm import sessionmaker from aciniformes_backend.settings import get_settings @@ -7,11 +7,4 @@ settings = get_settings() engine = create_engine(str(settings.DB_DSN), execution_options={"isolation_level": "AUTOCOMMIT", "pool_pre_ping": True}) -session = sessionmaker(bind=engine) - - -def dbsession() -> Session: - global session - localsession = session() - - return localsession +session_factory = sessionmaker(bind=engine) \ No newline at end of file