Skip to content

Commit

Permalink
Set 60 secs of timeout in until_scalar_one()
Browse files Browse the repository at this point in the history
  • Loading branch information
TaiSakuma committed Jun 11, 2024
1 parent 4074302 commit aa1526b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/nextline_rdb/utils/sa.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from logging import getLogger
from typing import Any, TypeVar

from sqlalchemy import Select, inspect, select
Expand All @@ -8,8 +9,15 @@

T = TypeVar('T', bound=DeclarativeBase)

# NOTE: Consider make this configurable.
DEFAULT_UNTIL_SCALAR_ONE_TIMEOUT = 60 # seconds

async def until_scalar_one(session: AsyncSession, stmt: Select[tuple[T]]) -> T:

async def until_scalar_one(
session: AsyncSession,
stmt: Select[tuple[T]],
timeout: float = DEFAULT_UNTIL_SCALAR_ONE_TIMEOUT,
) -> T:
'''Execute the statement until it returns exactly one row.
The statement is repeatedly executed while it returns no rows. An exception
Expand All @@ -19,7 +27,12 @@ async def until_scalar_one(session: AsyncSession, stmt: Select[tuple[T]]) -> T:
async def _f() -> T | None:
return (await session.execute(stmt)).scalar_one_or_none()

return await until_not_none(_f)
try:
return await until_not_none(_f, timeout=timeout)
except Exception:
logger = getLogger(__name__)
logger.exception('')
raise

Check warning on line 35 in src/nextline_rdb/utils/sa.py

View check run for this annotation

Codecov / codecov/patch

src/nextline_rdb/utils/sa.py#L32-L35

Added lines #L32 - L35 were not covered by tests


async def load_all(session: AsyncSession, model_base_class: type[T]) -> list[T]:
Expand Down

0 comments on commit aa1526b

Please sign in to comment.