Skip to content

Commit

Permalink
Remove sqs_manager singleton (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaFicarelli authored Aug 20, 2024
1 parent f54e139 commit 9483571
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
5 changes: 3 additions & 2 deletions app/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from app.config import settings
from app.errors import ApiError, ApiErrorCode
from app.logger import L
from app.queue.session import sqs_manager
from app.queue.session import SQSManager
from app.schema.api import ErrorResponse


Expand All @@ -31,6 +31,7 @@ async def lifespan(_: FastAPI) -> AsyncIterator[dict[str, Any]]:
os.cpu_count(),
settings.ENVIRONMENT,
)
sqs_manager = SQSManager()
sqs_manager.configure(
queue_names=[
settings.SQS_LONGRUN_QUEUE_NAME,
Expand All @@ -41,7 +42,7 @@ async def lifespan(_: FastAPI) -> AsyncIterator[dict[str, Any]]:
)
try:
async with sqs_manager:
yield {}
yield {"sqs_manager": sqs_manager}
except asyncio.CancelledError as err:
# this can happen if the task is cancelled without sending SIGINT
L.info("Ignored {} in lifespan", err)
Expand Down
7 changes: 4 additions & 3 deletions app/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

from fastapi import Depends
from sqlalchemy.ext.asyncio import AsyncSession
from starlette.requests import Request

from app.db.session import database_session_manager
from app.queue.session import SQSManager, sqs_manager
from app.queue.session import SQSManager
from app.repository.group import RepositoryGroup


Expand All @@ -22,9 +23,9 @@ def _repo_group(db: "SessionDep") -> RepositoryGroup:
return RepositoryGroup(db=db)


def _sqs_manager() -> SQSManager:
def _sqs_manager(request: Request) -> SQSManager:
"""Return the SQS manager."""
return sqs_manager
return request.state.sqs_manager


SessionDep = Annotated[AsyncSession, Depends(_database_session_factory)]
Expand Down
3 changes: 0 additions & 3 deletions app/queue/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,3 @@ def client(self) -> AioBaseClient:
def queue_urls(self) -> dict[str, str]:
"""Return the dict of queue urls."""
return self._queue_urls


sqs_manager = SQSManager()

0 comments on commit 9483571

Please sign in to comment.