Skip to content

Commit

Permalink
fix session
Browse files Browse the repository at this point in the history
  • Loading branch information
grigoriev-semyon committed Dec 15, 2023
1 parent 567abf8 commit 3c99b57
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
24 changes: 12 additions & 12 deletions aciniformes_backend/worker/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
11 changes: 2 additions & 9 deletions aciniformes_backend/worker/session.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from sqlalchemy.orm import sessionmaker

from aciniformes_backend.settings import get_settings


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)

0 comments on commit 3c99b57

Please sign in to comment.