notifier/resolvers/listener.py

25 lines
749 B
Python
Raw Normal View History

2023-11-26 10:18:57 +00:00
import json
from orm.notification import Notification
from services.db import local_session
from services.rediscache import redis
2023-11-26 11:54:07 +00:00
async def handle_reaction(notification: dict[str, str | int]):
2023-11-26 10:18:57 +00:00
"""создаеёт новое хранимое уведомление"""
2023-11-26 12:37:22 +00:00
with local_session() as session:
try:
2023-11-26 10:18:57 +00:00
n = Notification(**notification)
session.add(n)
session.commit(n)
2023-11-26 12:37:22 +00:00
except Exception as e:
session.rollback()
print(f"[listener.handle_reaction] error: {str(e)}")
2023-11-26 10:18:57 +00:00
2023-11-26 11:54:07 +00:00
async def reactions_worker():
async for message in redis.listen("reaction"):
2023-11-26 12:37:22 +00:00
msg = json.loads((await message).get("data", ""))
2023-11-26 11:54:07 +00:00
if msg:
await handle_reaction(msg)