Backends API¶
This module contains the backend classes used to publish events to external systems.
PublisherBackend¶
- class django_broadcaster.backends.PublisherBackend[source]¶
Bases:
ABCAbstract base class for publisher backends
- abstractmethod publish(event: OutboxEvent) bool[source]¶
Publish an event to the backend
- abstractmethod health_check() bool[source]¶
Check if the backend is healthy and available
- Returns:
True if healthy, False otherwise
- Return type:
RedisStreamBackend¶
- class django_broadcaster.backends.RedisStreamBackend(config: Dict[str, Any])[source]¶
Bases:
PublisherBackendRedis Stream publisher backend
- publish(event: OutboxEvent) bool[source]¶
Publish event to Redis Stream
- publish(event: OutboxEvent) bool[source]¶
Publish event to Redis Stream
Creating Custom Backends¶
To create a custom backend, you need to subclass PublisherBackend and implement the required methods:
from django_broadcaster.backends import PublisherBackend
from django_broadcaster.models import OutboxEvent
class MyCustomBackend(PublisherBackend):
"""
Custom publisher backend implementation
"""
def __init__(self, config):
self.config = config
# Initialize your backend with the provided configuration
def publish(self, event: OutboxEvent) -> bool:
"""
Publish an event to your custom backend
"""
try:
# Convert to CloudEvent format
cloud_event = event.to_cloud_event()
# Implement your publishing logic here
# ...
return True
except Exception as e:
# Log the error and re-raise
raise
def health_check(self) -> bool:
"""
Check if the backend is healthy and available
"""
try:
# Implement your health check logic here
# ...
return True
except Exception:
return False