Files
Nexus/server/infrastructure/database/push_schedule_repo.py
T
Your Name 99201f48fd fix: MissingGreenlet error — convert all middleware to pure ASGI
Root cause: BaseHTTPMiddleware.call_next() runs the endpoint in a
separate async task, which breaks SQLAlchemy's greenlet context.
After session.commit(), the connection is released to the pool.
When session.refresh() tries to acquire a new connection for a
SELECT, it fails with MissingGreenlet because the greenlet spawn
context is not available in the call_next() task.

Fix (two-part):
1. Convert all 4 middleware classes from BaseHTTPMiddleware to pure
   ASGI middleware — eliminates call_next() entirely so the entire
   request chain runs in the same async context.
2. Set expire_on_commit=False on the session factory — after commit,
   objects retain their in-memory values instead of being expired.
   This removes the need for session.refresh() entirely.

All session.refresh() calls removed from 11 repository files.
With expire_on_commit=False, post-commit attribute access no longer
triggers lazy loads that would also fail with MissingGreenlet.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-05-25 15:24:15 +08:00

92 lines
3.2 KiB
Python

"""Nexus — PushSchedule & PushRetryJob Repository (Async SQLAlchemy)"""
from typing import Optional, List
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from server.domain.models import PushSchedule, PushRetryJob
class PushScheduleRepositoryImpl:
def __init__(self, session: AsyncSession):
self.session = session
async def get_by_id(self, id: int) -> Optional[PushSchedule]:
result = await self.session.execute(select(PushSchedule).where(PushSchedule.id == id))
return result.scalar_one_or_none()
async def get_all(self) -> List[PushSchedule]:
result = await self.session.execute(select(PushSchedule).order_by(PushSchedule.id))
return list(result.scalars().all())
async def get_enabled(self) -> List[PushSchedule]:
result = await self.session.execute(
select(PushSchedule).where(PushSchedule.enabled == True).order_by(PushSchedule.id)
)
return list(result.scalars().all())
async def create(self, schedule: PushSchedule) -> PushSchedule:
self.session.add(schedule)
await self.session.commit()
return schedule
async def update(self, schedule: PushSchedule) -> PushSchedule:
await self.session.commit()
return schedule
async def delete(self, id: int) -> bool:
schedule = await self.get_by_id(id)
if schedule:
await self.session.delete(schedule)
await self.session.commit()
return True
return False
class PushRetryJobRepositoryImpl:
def __init__(self, session: AsyncSession):
self.session = session
async def get_pending(self, limit: int = 100) -> List[PushRetryJob]:
result = await self.session.execute(
select(PushRetryJob)
.where(PushRetryJob.status == "pending")
.order_by(PushRetryJob.created_at)
.limit(limit)
)
return list(result.scalars().all())
async def get_all(self, limit: int = 100) -> List[PushRetryJob]:
result = await self.session.execute(
select(PushRetryJob)
.order_by(PushRetryJob.created_at.desc())
.limit(limit)
)
return list(result.scalars().all())
async def get_by_id(self, id: int) -> Optional[PushRetryJob]:
result = await self.session.execute(select(PushRetryJob).where(PushRetryJob.id == id))
return result.scalar_one_or_none()
async def delete(self, id: int) -> bool:
job = await self.get_by_id(id)
if job:
await self.session.delete(job)
await self.session.commit()
return True
return False
async def create(self, job: PushRetryJob) -> PushRetryJob:
self.session.add(job)
await self.session.commit()
return job
async def update_status(self, id: int, status: str, **kwargs) -> PushRetryJob:
job = await self.session.get(PushRetryJob, id)
if job:
job.status = status
for key, value in kwargs.items():
if hasattr(job, key):
setattr(job, key, value)
await self.session.commit()
return job