notifier/resolvers/listener.py

38 lines
976 B
Python
Raw Normal View History

2023-11-26 10:18:57 +00:00
import json
from typing import List, Dict
from orm.notification import Notification
from services.db import local_session
from services.rediscache import redis
def handle_reaction(notification: Dict[str, str | int | List[int]]):
"""создаеёт новое хранимое уведомление"""
try:
with local_session() as session:
n = Notification(**notification)
session.add(n)
session.commit(n)
except Exception as e:
session.rollback()
print(f"[listener.handle_reaction] error: {str(e)}")
def stop(pubsub):
pubsub.unsubscribe()
pubsub.close()
def start():
pubsub = redis.pubsub()
pubsub.subscribe("reaction")
try:
# Бесконечный цикл прослушивания
while True:
msg = pubsub.get_message()
handle_reaction(json.loads(msg["data"]))
except Exception:
pass
finally:
stop(pubsub)