This repository has been archived on 2026-05-31. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
GallerySubscriber/backend/app/events.py
T
2026-01-24 22:52:51 -05:00

31 lines
946 B
Python

"""Cross-process event publishing via Redis.
This module provides synchronous event publishing for use in Celery tasks.
The events are consumed by the WebSocket module which broadcasts them to clients.
"""
import json
import redis
from app.config import get_settings
# Redis pub/sub channel for cross-process events
EVENTS_CHANNEL = "gallery_subscriber:events"
def publish_event(event_type: str, data: dict):
"""Publish an event to Redis (for use in Celery tasks).
This is a sync function that can be called from Celery tasks.
The Quart app subscribes to these events and broadcasts to WebSocket clients.
Args:
event_type: Type of event (e.g., "download.started", "download.completed")
data: Event payload data
"""
settings = get_settings()
r = redis.from_url(settings.redis_url)
message = json.dumps({"type": event_type, "data": data})
r.publish(EVENTS_CHANNEL, message)
r.close()