refactor: migrate broker initialization to taskiq_app and remove unused broker import

feat: add channel event processing handler for Telegram and Slack
This commit is contained in:
2026-06-20 11:59:12 +08:00
parent 0bff4f87b4
commit 236e1a95b8
5 changed files with 43 additions and 20 deletions
+27
View File
@@ -0,0 +1,27 @@
"""In-process handler for channel-bot webhook events (Telegram, Slack).
Dispatched via FastAPI ``BackgroundTasks`` from the webhook routes so the platform
gets a fast 200 OK while routing/AI happens in the background.
"""
import logging
from typing import Any
from app.db.session import get_db_context
from app.services.channels.router import ChannelMessageRouter
logger = logging.getLogger(__name__)
async def process_channel_event(incoming: Any) -> None:
"""Route an incoming channel event to its handler.
Errors are logged with full traceback but never re-raised — the response to the
platform has already been sent, and an unhandled exception here would just become
a noisy stack trace in the API process logs.
"""
try:
async with get_db_context() as db:
await ChannelMessageRouter().route(incoming, db)
except Exception:
logger.exception("channel_event_processing_failed")
-14
View File
@@ -1,14 +0,0 @@
"""Taskiq broker — shared across all task modules."""
from taskiq_redis import ListQueueBroker, RedisAsyncResultBackend
from app.core.config import settings
# Create Taskiq broker with Redis
broker = ListQueueBroker(
url=settings.TASKIQ_BROKER_URL,
).with_result_backend(
RedisAsyncResultBackend(
redis_url=settings.TASKIQ_RESULT_BACKEND,
)
)
+14 -4
View File
@@ -2,13 +2,20 @@
from taskiq import TaskiqScheduler
from taskiq.schedule_sources import LabelScheduleSource
from taskiq_redis import ListQueueBroker, RedisAsyncResultBackend
from app.worker.broker import broker
import app.worker.tasks # noqa: F401 — register @broker.task decorated functions
import app.worker.tasks.schedules # noqa: F401 — register scheduled tasks
from app.core.config import settings
# Create Taskiq broker with Redis
broker = ListQueueBroker(
url=settings.TASKIQ_BROKER_URL,
).with_result_backend(
RedisAsyncResultBackend(
redis_url=settings.TASKIQ_RESULT_BACKEND,
)
)
# Create scheduler for periodic tasks
# LabelScheduleSource auto-discovers @broker.task(schedule=[...]) decorated functions
scheduler = TaskiqScheduler(
broker=broker,
sources=[LabelScheduleSource(broker)],
@@ -26,3 +33,6 @@ async def startup() -> None:
async def shutdown() -> None:
"""Cleanup on shutdown."""
pass
import app.worker.tasks.schedules # noqa: F401
+1 -1
View File
@@ -6,7 +6,7 @@ import tempfile
from pathlib import Path
from typing import Any
from app.worker.broker import broker
from app.worker.taskiq_app import broker
logger = logging.getLogger(__name__)
+1 -1
View File
@@ -1,6 +1,6 @@
"""Taskiq scheduled tasks (cron-like)."""
from app.worker.broker import broker
from app.worker.taskiq_app import broker
from app.worker.tasks.rag_tasks import check_scheduled_syncs