Initial microservices setup from artifacts
This commit is contained in:
0
services/notification/shared/messaging/__init__.py
Normal file
0
services/notification/shared/messaging/__init__.py
Normal file
73
services/notification/shared/messaging/events.py
Normal file
73
services/notification/shared/messaging/events.py
Normal file
@@ -0,0 +1,73 @@
|
||||
"""
|
||||
Event definitions for microservices communication
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import Dict, Any, Optional
|
||||
import uuid
|
||||
|
||||
@dataclass
|
||||
class BaseEvent:
|
||||
"""Base event class"""
|
||||
event_id: str
|
||||
event_type: str
|
||||
service_name: str
|
||||
timestamp: datetime
|
||||
data: Dict[str, Any]
|
||||
correlation_id: Optional[str] = None
|
||||
|
||||
def __post_init__(self):
|
||||
if not self.event_id:
|
||||
self.event_id = str(uuid.uuid4())
|
||||
if not self.timestamp:
|
||||
self.timestamp = datetime.utcnow()
|
||||
|
||||
# Training Events
|
||||
@dataclass
|
||||
class TrainingStartedEvent(BaseEvent):
|
||||
event_type: str = "training.started"
|
||||
|
||||
@dataclass
|
||||
class TrainingCompletedEvent(BaseEvent):
|
||||
event_type: str = "training.completed"
|
||||
|
||||
@dataclass
|
||||
class TrainingFailedEvent(BaseEvent):
|
||||
event_type: str = "training.failed"
|
||||
|
||||
# Forecasting Events
|
||||
@dataclass
|
||||
class ForecastGeneratedEvent(BaseEvent):
|
||||
event_type: str = "forecast.generated"
|
||||
|
||||
@dataclass
|
||||
class ForecastRequestedEvent(BaseEvent):
|
||||
event_type: str = "forecast.requested"
|
||||
|
||||
# User Events
|
||||
@dataclass
|
||||
class UserRegisteredEvent(BaseEvent):
|
||||
event_type: str = "user.registered"
|
||||
|
||||
@dataclass
|
||||
class UserLoginEvent(BaseEvent):
|
||||
event_type: str = "user.login"
|
||||
|
||||
# Tenant Events
|
||||
@dataclass
|
||||
class TenantCreatedEvent(BaseEvent):
|
||||
event_type: str = "tenant.created"
|
||||
|
||||
@dataclass
|
||||
class TenantUpdatedEvent(BaseEvent):
|
||||
event_type: str = "tenant.updated"
|
||||
|
||||
# Notification Events
|
||||
@dataclass
|
||||
class NotificationSentEvent(BaseEvent):
|
||||
event_type: str = "notification.sent"
|
||||
|
||||
@dataclass
|
||||
class NotificationFailedEvent(BaseEvent):
|
||||
event_type: str = "notification.failed"
|
||||
96
services/notification/shared/messaging/rabbitmq.py
Normal file
96
services/notification/shared/messaging/rabbitmq.py
Normal file
@@ -0,0 +1,96 @@
|
||||
"""
|
||||
RabbitMQ messaging client for microservices
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from typing import Dict, Any, Callable
|
||||
import aio_pika
|
||||
from aio_pika import connect_robust, Message, DeliveryMode
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class RabbitMQClient:
|
||||
"""RabbitMQ client for microservices communication"""
|
||||
|
||||
def __init__(self, connection_url: str):
|
||||
self.connection_url = connection_url
|
||||
self.connection = None
|
||||
self.channel = None
|
||||
|
||||
async def connect(self):
|
||||
"""Connect to RabbitMQ"""
|
||||
try:
|
||||
self.connection = await connect_robust(self.connection_url)
|
||||
self.channel = await self.connection.channel()
|
||||
logger.info("Connected to RabbitMQ")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to connect to RabbitMQ: {e}")
|
||||
raise
|
||||
|
||||
async def disconnect(self):
|
||||
"""Disconnect from RabbitMQ"""
|
||||
if self.connection:
|
||||
await self.connection.close()
|
||||
logger.info("Disconnected from RabbitMQ")
|
||||
|
||||
async def publish_event(self, exchange_name: str, routing_key: str, event_data: Dict[str, Any]):
|
||||
"""Publish event to RabbitMQ"""
|
||||
try:
|
||||
if not self.channel:
|
||||
await self.connect()
|
||||
|
||||
# Declare exchange
|
||||
exchange = await self.channel.declare_exchange(
|
||||
exchange_name,
|
||||
aio_pika.ExchangeType.TOPIC,
|
||||
durable=True
|
||||
)
|
||||
|
||||
# Create message
|
||||
message = Message(
|
||||
json.dumps(event_data).encode(),
|
||||
delivery_mode=DeliveryMode.PERSISTENT,
|
||||
content_type="application/json"
|
||||
)
|
||||
|
||||
# Publish message
|
||||
await exchange.publish(message, routing_key=routing_key)
|
||||
|
||||
logger.info(f"Published event to {exchange_name} with routing key {routing_key}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to publish event: {e}")
|
||||
raise
|
||||
|
||||
async def consume_events(self, exchange_name: str, queue_name: str, routing_key: str, callback: Callable):
|
||||
"""Consume events from RabbitMQ"""
|
||||
try:
|
||||
if not self.channel:
|
||||
await self.connect()
|
||||
|
||||
# Declare exchange
|
||||
exchange = await self.channel.declare_exchange(
|
||||
exchange_name,
|
||||
aio_pika.ExchangeType.TOPIC,
|
||||
durable=True
|
||||
)
|
||||
|
||||
# Declare queue
|
||||
queue = await self.channel.declare_queue(
|
||||
queue_name,
|
||||
durable=True
|
||||
)
|
||||
|
||||
# Bind queue to exchange
|
||||
await queue.bind(exchange, routing_key)
|
||||
|
||||
# Set up consumer
|
||||
await queue.consume(callback)
|
||||
|
||||
logger.info(f"Started consuming events from {queue_name}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to consume events: {e}")
|
||||
raise
|
||||
Reference in New Issue
Block a user