Skip to content

Commit a5e3abb

Browse files
refactor containers
1 parent aa6ba1c commit a5e3abb

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

app/infrastructure/containers.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ def __call__(self) -> asyncio.Task:
8282
class BaseStartupContainer(containers.DeclarativeContainer):
8383
"""Общий контейнер для старта бота."""
8484

85+
@staticmethod
86+
def get_collectors() -> list[HandlerCollector]:
87+
from app.presentation.bot.commands.common import collector as common_collector
88+
from app.presentation.bot.commands.sample_record import (
89+
collector as sample_record_collector,
90+
)
91+
92+
return [common_collector, sample_record_collector]
93+
8594
redis_client = providers.Singleton(lambda: aioredis.from_url(settings.REDIS_DSN))
8695

8796
redis_repo = providers.Factory(
@@ -116,23 +125,12 @@ class BaseStartupContainer(containers.DeclarativeContainer):
116125
answer_error_middleware,
117126
],
118127
callback_repo=callback_repo,
128+
collectors=get_collectors(),
119129
)
120130

121131

122132
class ApplicationStartupContainer(BaseStartupContainer):
123-
"""Контейнер приложения с ленивой загрузкой collectors."""
124-
125-
@staticmethod
126-
def get_collectors() -> list[HandlerCollector]:
127-
from app.presentation.bot.commands.common import collector as common_collector
128-
from app.presentation.bot.commands.sample_record import collector as sample_record_collector
129-
return [common_collector, sample_record_collector]
130-
131-
bot = providers.Singleton(
132-
Bot,
133-
collectors=providers.Callable(get_collectors),
134-
**BaseStartupContainer.bot.kwargs,
135-
)
133+
"""Main Fastapi application container."""
136134

137135
callback_task_manager = providers.Singleton(
138136
CallbackTaskManager,
@@ -146,12 +144,6 @@ def get_collectors() -> list[HandlerCollector]:
146144

147145

148146
class WorkerStartupContainer(BaseStartupContainer):
149-
"""Контейнер воркера с прямым импортом collectors."""
150-
151-
from app.presentation.bot.commands import common, sample_record
147+
"""SAQ Worker container"""
152148

153-
bot = providers.Singleton(
154-
Bot,
155-
collectors=[common.collector, sample_record.collector], # type:ignore
156-
**BaseStartupContainer.bot.kwargs,
157-
)
149+
pass

app/infrastructure/worker/worker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from redis.asyncio import Redis
88
from saq import CronJob, Queue
99

10-
from app.infrastructure.containers import WorkerStartupContainer
10+
from app.infrastructure.containers import WorkerStartupContainer, BaseStartupContainer
1111
from app.infrastructure.worker.tasks.simple_task import heartbeat_task
1212
from app.logger import logger
1313
from app.settings import settings
@@ -38,7 +38,7 @@ async def _shutdown_with_injection(
3838

3939

4040
async def startup(ctx: SaqCtx) -> None:
41-
worker_startup_container = WorkerStartupContainer()
41+
worker_startup_container = BaseStartupContainer()
4242

4343
worker_startup_container.wire(modules=[__name__, "app.infrastructure.worker.tasks"])
4444
await _startup_with_injection()

0 commit comments

Comments
 (0)