Skip to content

Commit

Permalink
optimized query for stuck requests (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex75 authored Nov 15, 2024
1 parent 905ef7a commit 2b74895
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions cads_broker/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,16 +493,20 @@ def get_users_queue_from_processing_time(

def get_stuck_requests(session: sa.orm.Session, minutes: int = 15) -> list[str]:
"""Get all running requests that are not assigned to any worker."""
query = (
subquery = (
sa.select(SystemRequest.request_uid)
.outerjoin(Events, SystemRequest.request_uid == Events.request_uid)
.where(
SystemRequest.status == "running",
SystemRequest.started_at
< sa.func.now() - datetime.timedelta(minutes=minutes),
)
.where(Events.event_id.is_(None))
.subquery()
)
query = (
sa.select(subquery.c.request_uid).outerjoin(
Events, subquery.c.request_uid == Events.request_uid
)
).where(Events.event_id.is_(None))
return session.execute(query).scalars().all()


Expand Down

0 comments on commit 2b74895

Please sign in to comment.