fix!: removed .commit() from repository level
This commit is contained in:
@@ -8,7 +8,7 @@ from sqlalchemy.dialects.postgresql import insert
|
||||
|
||||
from config import cfg
|
||||
from db.models import RWSyncOutbox, Subscription
|
||||
from db.session import async_session
|
||||
from db.session import UnitOfWork, async_session
|
||||
from external.rw import sync_subscription_by_telegram_id
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -48,7 +48,8 @@ async def enqueue_all_rw_syncs(session: "AsyncSession") -> int:
|
||||
return count
|
||||
|
||||
|
||||
async def process_rw_syncs(session: "AsyncSession", batch_size: int = 50) -> int:
|
||||
async def process_rw_syncs(uow: UnitOfWork, batch_size: int = 50) -> int:
|
||||
session = uow.session
|
||||
now = datetime.now(UTC)
|
||||
jobs = await session.scalars(
|
||||
select(RWSyncOutbox)
|
||||
@@ -63,7 +64,7 @@ async def process_rw_syncs(session: "AsyncSession", batch_size: int = 50) -> int
|
||||
jobs = list(jobs)
|
||||
for job in jobs:
|
||||
job.locked_until = now + LOCK_DURATION
|
||||
await session.commit()
|
||||
await uow.commit()
|
||||
|
||||
for job in jobs:
|
||||
subscription = job.subscription
|
||||
@@ -81,7 +82,7 @@ async def process_rw_syncs(session: "AsyncSession", batch_size: int = 50) -> int
|
||||
RWSyncOutbox.revision == job.revision,
|
||||
)
|
||||
)
|
||||
await session.commit()
|
||||
await uow.commit()
|
||||
continue
|
||||
|
||||
delay = min(timedelta(minutes=2**job.attempts), MAX_RETRY_DELAY)
|
||||
@@ -94,7 +95,7 @@ async def process_rw_syncs(session: "AsyncSession", batch_size: int = 50) -> int
|
||||
locked_until=None,
|
||||
)
|
||||
)
|
||||
await session.commit()
|
||||
await uow.commit()
|
||||
logger.warning("RW sync failed for subscription_id=%s", subscription.id)
|
||||
|
||||
return len(jobs)
|
||||
@@ -104,8 +105,8 @@ async def run_rw_sync_worker() -> None:
|
||||
interval = cfg.rw_sync_interval_minutes * 60
|
||||
while True:
|
||||
try:
|
||||
async with async_session() as session:
|
||||
await process_rw_syncs(session)
|
||||
async with async_session() as session, UnitOfWork(session) as uow:
|
||||
await process_rw_syncs(uow)
|
||||
except Exception:
|
||||
logger.exception("RW sync worker iteration failed")
|
||||
await asyncio.sleep(interval)
|
||||
@@ -116,8 +117,9 @@ async def run_rw_sync_reconciler() -> None:
|
||||
while True:
|
||||
try:
|
||||
async with async_session() as session:
|
||||
count = await enqueue_all_rw_syncs(session)
|
||||
await session.commit()
|
||||
async with UnitOfWork(session) as uow:
|
||||
count = await enqueue_all_rw_syncs(uow.session)
|
||||
await uow.commit()
|
||||
logger.info("Queued %s subscriptions for RW reconciliation", count)
|
||||
except Exception:
|
||||
logger.exception("RW reconciliation iteration failed")
|
||||
|
||||
Reference in New Issue
Block a user