File size: 9,757 Bytes
edcd2ef | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | """
Notification Microservice - Phase 5
Consumes reminder events from Kafka and sends email/push notifications.
Subscribes to "reminders" topic via Dapr Pub/Sub.
"""
import os
import uuid
import asyncio
from datetime import datetime, timezone
from typing import Dict, Any
from fastapi import FastAPI, HTTPException, BackgroundTasks
from pydantic import BaseModel
import httpx
from src.utils.logging import get_logger
logger = get_logger(__name__)
# Configuration
DAPR_HTTP_PORT = os.getenv("DAPR_HTTP_PORT", "3500")
DAPR_HOST = os.getenv("DAPR_HOST", "localhost")
BASE_URL = f"http://{DAPR_HOST}:{DAPR_HTTP_PORT}"
SUBSCRIPTION_PATH = "/reminders" # Dapr will invoke this endpoint
app = FastAPI(
title="Notification Service",
description="Microservice for sending reminder notifications",
version="1.0.0"
)
# Email service configuration
EMAIL_API_KEY = os.getenv("EMAIL_API_KEY", "")
FROM_EMAIL = os.getenv("FROM_EMAIL", "noreply@todo-app.local")
class ReminderEvent(BaseModel):
"""Reminder event from Kafka"""
event_id: str
event_type: str
topic_name: str
correlation_id: str
timestamp: str
source_service: str
payload: Dict[str, Any]
class NotificationDelivery(BaseModel):
"""Notification delivery result"""
reminder_id: str
task_id: str
status: str # sent, failed, pending
delivery_method: str # email, push
destination: str
error_message: str = ""
async def send_email_notification(
to: str,
subject: str,
body: str,
task_data: Dict[str, Any]
) -> bool:
"""
Send email notification using SendGrid (or mock).
Args:
to: Recipient email
subject: Email subject
body: Email body (HTML)
task_data: Task details for templating
Returns:
True if sent successfully, False otherwise
"""
try:
# For demo/development, we'll log the email
logger.info(
"sending_email",
to=to,
subject=subject,
task_id=task_data.get("task_id"),
task_title=task_data.get("title")
)
# If EMAIL_API_KEY is configured, use actual SendGrid
if EMAIL_API_KEY and EMAIL_API_KEY != "SG.mock":
async with httpx.AsyncClient() as client:
response = await client.post(
"https://api.sendgrid.com/v3/mail/send",
headers={
"Authorization": f"Bearer {EMAIL_API_KEY}",
"Content-Type": "application/json"
},
json={
"personalizations": [
{
"to": [{"email": to}],
"subject": subject
}
],
"from": {"email": FROM_EMAIL},
"content": [
{
"type": "text/html",
"value": body
}
]
},
timeout=10.0
)
if response.status_code in [200, 202]:
logger.info("email_sent_success", to=to, task_id=task_data.get("task_id"))
return True
else:
logger.error(
"email_send_failed",
status=response.status_code,
response=response.text
)
return False
else:
# Mock mode - just log and return success
logger.info("email_mock_mode", to=to, subject=subject)
return True
except Exception as e:
logger.error("email_exception", error=str(e))
return False
def format_reminder_email(task_data: Dict[str, Any]) -> tuple[str, str]:
"""
Format reminder email subject and body.
Args:
task_data: Task details
Returns:
Tuple of (subject, html_body)
"""
title = task_data.get("title", "Task")
due_date = task_data.get("due_date")
description = task_data.get("description", "")
subject = f"🔔 Reminder: {title}"
html_body = f"""
<html>
<body>
<h2>Task Reminder</h2>
<p>You have a task due soon:</p>
<div style="border: 1px solid #ddd; padding: 15px; border-radius: 5px;">
<h3>{title}</h3>
{f'<p><strong>Due:</strong> {due_date}</p>' if due_date else ''}
{f'<p><strong>Description:</strong> {description}</p>' if description else ''}
</div>
<p>This is an automated reminder from your Todo App.</p>
<hr>
<p style="color: #666; font-size: 12px;">
Reminder ID: {task_data.get('reminder_id')}<br>
Task ID: {task_data.get('task_id')}
</p>
</body>
</html>
"""
return subject, html_body
@app.get("/")
async def root():
"""Root endpoint"""
return {
"service": "notification-service",
"status": "running",
"version": "1.0.0",
"dapr_port": DAPR_HTTP_PORT
}
@app.get("/health")
async def health_check():
"""Health check endpoint (liveness probe)"""
return {
"status": "healthy",
"timestamp": datetime.now(timezone.utc).isoformat()
}
@app.get("/ready")
async def readiness_check():
"""Readiness check endpoint"""
# Check if email service is configured
ready = bool(EMAIL_API_KEY) or EMAIL_API_KEY == "SG.mock"
return {
"status": "ready" if ready else "not_ready",
"email_configured": ready,
"timestamp": datetime.now(timezone.utc).isoformat()
}
@app.post(SUBSCRIPTION_PATH)
async def handle_reminder_event(
event_data: ReminderEvent,
background_tasks: BackgroundTasks
):
"""
Dapr Pub/Sub subscription endpoint for reminders topic.
Automatically called by Dapr when a message is published to the "reminders" topic.
Args:
event_data: Reminder event from Kafka
background_tasks: FastAPI background tasks
Returns:
Delivery confirmation
"""
try:
logger.info(
"reminder_event_received",
event_id=event_data.event_id,
event_type=event_data.event_type,
correlation_id=event_data.correlation_id
)
# Extract reminder details
payload = event_data.payload
reminder_id = payload.get("reminder_id")
task_id = payload.get("task_id")
trigger_time = payload.get("trigger_time")
delivery_method = payload.get("delivery_method", "email")
destination = payload.get("destination")
if not all([reminder_id, task_id, destination]):
logger.error(
"invalid_reminder_event",
payload_keys=list(payload.keys()),
reminder_id=reminder_id,
task_id=task_id
)
raise HTTPException(status_code=400, detail="Invalid reminder event")
# Get task details for email formatting
task_data = payload.get("task", {})
task_data["reminder_id"] = reminder_id
task_data["task_id"] = task_id
# Send notification based on delivery method
if delivery_method == "email":
subject, body = format_reminder_email(task_data)
# Send in background task
background_tasks.add_task(
send_email_and_log(
reminder_id=reminder_id,
task_id=task_id,
destination=destination,
subject=subject,
body=body,
task_data=task_data,
correlation_id=event_data.correlation_id
)
)
else:
logger.warning(
"unsupported_delivery_method",
method=delivery_method
)
return {
"status": "processing",
"reminder_id": reminder_id,
"message": "Notification queued for delivery"
}
except HTTPException:
raise
except Exception as e:
logger.error(
"handle_reminder_event_failed",
error=str(e),
event_id=event_data.event_id
)
raise HTTPException(
status_code=500,
detail=f"Failed to process reminder event: {str(e)}"
)
async def send_email_and_log(
reminder_id: str,
task_id: str,
destination: str,
subject: str,
body: str,
task_data: Dict[str, Any],
correlation_id: str
):
"""
Send email and log delivery result.
Args:
reminder_id: Reminder ID
task_id: Task ID
destination: Email address
subject: Email subject
body: Email body
task_data: Task details
correlation_id: Correlation ID for tracing
"""
# Send email
success = await send_email_notification(destination, subject, body, task_data)
# Log delivery result
logger.info(
"notification_delivery_complete",
reminder_id=reminder_id,
task_id=task_id,
destination=destination,
status="sent" if success else "failed",
correlation_id=correlation_id
)
# TODO: Publish audit event to Kafka
# await publish_audit_event(...)
if __name__ == "__main__":
import uvicorn
port = int(os.getenv("PORT", "4000"))
logger.info("starting_notification_service", port=port)
uvicorn.run(
"main:app",
host="0.0.0.0",
port=port,
log_level="info"
)
|