Source code for luna_handlers.redis_db.redis_context
import ujson as json
from classes.event import HandlerEvent as Event
from classes.schemas.event_raw import RawEvent
from crutches_on_wheels.cow.redis_db.redis_sender_context import RedisContext as _SenderRedisContext
async def _performPublish(ctx, msg: dict, accountId: str):
    """
    Publish msg with to redis
    Args:
        ctx: redis context
        msg: msg
        accountId: account id
    """
    try:
        await ctx.redis.publish(f"{ctx.channelName}:{accountId}", json.dumps(msg, ensure_ascii=False))
    except Exception:
        ctx.logger.exception("publish events to redis")
async def _publish(ctx, events: list[Event], accountId: str, requestId: str) -> None:
    """
    Publish events. Msg is published to channel 'luna-sender:{account_id}' in format:
    :json:object:`msg_to_luna_sender`
    Args:
        ctx: redis context
        events: events
        accountId: account id
        requestId: request id
    """
    if len(events) == 0:
        return
    msg = {
        "handler_id": events[0].handlerId,
        "Luna-Request-Id": requestId,
        "events": [event.raw for event in events],
        "event_create_time": events[0].createTime,
        "event_end_time": events[0].endTime,
    }
    await _performPublish(ctx, msg, accountId)
[docs]
class RedisContext(_SenderRedisContext):
    """Redis context"""
[docs]
    async def publish(self, events: list[Event], accountId: str, requestId: str) -> None:
        """
        Publish events. Msg is published to channel 'luna-sender:{account_id}' in format:
        :json:object:`msg_to_luna_sender`
        Args:
            events: events
            accountId: account id
            requestId: request id
        """
        await _publish(self, events, accountId, requestId) 
[docs]
    async def publishRawEvent(
        self,
        event: RawEvent,
        accountId: str,
        handlerId: str,
        requestId: str,
    ) -> None:
        """
        Publish events. Msg is published to channel 'luna-sender:{account_id}' in format:
        :json:object:`msg_to_luna_sender`
        Args:
            event: user raw event
            accountId: account id
            handlerId: handler id
            requestId: request id
        """
        msg = {
            "handler_id": handlerId,
            "Luna-Request-Id": requestId,
            "events": [event.asDict()],
            "event_create_time": event.createTime,
            "event_end_time": event.endTime,
        }
        await _performPublish(self, msg, accountId)