Spaces:
Sleeping
Sleeping
Download src/api/dapr_handlers.py from hashir-qt/tasklert-api: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/spaces/hashir-qt/tasklert-api/resolve/main/src/api/dapr_handlers.py
- Command line
-
hf download hf://spaces/hashir-qt/tasklert-api/src/api/dapr_handlers.py
-
curl -L -o dapr_handlers.py https://huggingface.co/spaces/hashir-qt/tasklert-api/resolve/main/src/api/dapr_handlers.py
3.1 kB
| """Phase V: Dapr subscription discovery and event handlers.""" | |
| import logging | |
| from fastapi import APIRouter, Request | |
| logger = logging.getLogger(__name__) | |
| router = APIRouter(tags=["Dapr"]) | |
| async def dapr_subscribe(): | |
| """Dapr subscription manifest — tells Dapr which topics to forward to which routes.""" | |
| return [ | |
| {"pubsubname": "taskpubsub", "topic": "task-events", "route": "/events/task"}, | |
| {"pubsubname": "taskpubsub", "topic": "reminders", "route": "/events/reminder"}, | |
| {"pubsubname": "taskpubsub", "topic": "task-updates", "route": "/events/sync"}, | |
| ] | |
| async def on_task_event(request: Request): | |
| """Handle task-events topic: update stats, check achievements, spawn recurring instances.""" | |
| from ..services import stats_service, recurring_service | |
| from ..core.database import async_session | |
| try: | |
| envelope = await request.json() | |
| event = envelope.get("data", envelope) # CloudEvents wraps in "data" | |
| event_type = event.get("event_type", "") | |
| user_id = event.get("user_id", "") | |
| task_id = event.get("task_id") | |
| if event_type == "completed" and user_id and task_id: | |
| async with async_session() as session: | |
| await stats_service.record_completion(session, user_id, task_id, event.get("task_data", {})) | |
| await recurring_service.maybe_create_next(session, event.get("task_data", {})) | |
| except Exception as exc: | |
| logger.error("Error handling task event: %s", exc) | |
| return {"status": "RETRY"} | |
| return {"status": "SUCCESS"} | |
| async def on_reminder_event(request: Request): | |
| """Handle reminders topic: push notification to user's WebSocket connections.""" | |
| from .websocket import manager | |
| try: | |
| envelope = await request.json() | |
| event = envelope.get("data", envelope) | |
| user_id = event.get("user_id", "") | |
| if user_id: | |
| await manager.send_to_user(user_id, { | |
| "type": "reminder", | |
| "task_id": event.get("task_id"), | |
| "title": event.get("title", "Task reminder"), | |
| }) | |
| except Exception as exc: | |
| logger.error("Error handling reminder event: %s", exc) | |
| return {"status": "RETRY"} | |
| return {"status": "SUCCESS"} | |
| async def on_sync_event(request: Request): | |
| """Handle task-updates topic: broadcast to all WebSocket connections for this user.""" | |
| from .websocket import manager | |
| try: | |
| envelope = await request.json() | |
| event = envelope.get("data", envelope) | |
| user_id = event.get("user_id", "") | |
| if user_id: | |
| await manager.send_to_user(user_id, { | |
| "type": "task_changed", | |
| "event_type": event.get("event_type"), | |
| "task_id": event.get("task_id"), | |
| }) | |
| except Exception as exc: | |
| logger.error("Error handling sync event: %s", exc) | |
| return {"status": "RETRY"} | |
| return {"status": "SUCCESS"} | |