feat(redis): add subscriber for pub/sub mode
This commit is contained in:
20
app/service/subscriber.py
Normal file
20
app/service/subscriber.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Awaitable, Callable
|
||||
from typing import Any
|
||||
|
||||
from app.dependencies.database import get_redis_pubsub
|
||||
|
||||
|
||||
class RedisSubscriber:
|
||||
def __init__(self, channel: str):
|
||||
self.pubsub = get_redis_pubsub(channel)
|
||||
self.handlers: dict[str, list[Callable[[str, str], Awaitable[Any]]]] = {}
|
||||
|
||||
async def listen(self):
|
||||
async for message in self.pubsub.listen():
|
||||
if message is not None and message["type"] == "message":
|
||||
method = self.handlers.get(message["channel"])
|
||||
if method:
|
||||
for handler in method:
|
||||
await handler(message["channel"], message["data"])
|
||||
Reference in New Issue
Block a user