|
| 1 | +"""Redis-backed EventQueue implementation using Redis Streams.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import asyncio |
| 6 | +import json |
| 7 | +import logging |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +try: |
| 11 | + import redis.asyncio as aioredis # type: ignore |
| 12 | + from redis.exceptions import RedisError # type: ignore |
| 13 | +except ImportError: # pragma: no cover - optional dependency |
| 14 | + aioredis = None # type: ignore |
| 15 | + RedisError = Exception # type: ignore |
| 16 | + |
| 17 | +from a2a.server.events.event_queue import EventQueue |
| 18 | +from typing import TYPE_CHECKING |
| 19 | +if TYPE_CHECKING: |
| 20 | + from a2a.server.events.event_queue import Event |
| 21 | +from pydantic import ValidationError |
| 22 | +from a2a.types import ( |
| 23 | + Message, |
| 24 | + Task, |
| 25 | + TaskStatusUpdateEvent, |
| 26 | + TaskArtifactUpdateEvent, |
| 27 | +) |
| 28 | +from a2a.utils.telemetry import SpanKind, trace_class |
| 29 | + |
| 30 | +logger = logging.getLogger(__name__) |
| 31 | + |
| 32 | + |
| 33 | +class RedisNotAvailableError(RuntimeError): |
| 34 | + """Raised when the redis.asyncio package is not installed.""" |
| 35 | + |
| 36 | + |
| 37 | +_TYPE_MAP = { |
| 38 | + 'Message': Message, |
| 39 | + 'Task': Task, |
| 40 | + 'TaskStatusUpdateEvent': TaskStatusUpdateEvent, |
| 41 | + 'TaskArtifactUpdateEvent': TaskArtifactUpdateEvent, |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +@trace_class(kind=SpanKind.SERVER) |
| 46 | +class RedisEventQueue(EventQueue): |
| 47 | + """Redis-native EventQueue backed by a Redis Stream. |
| 48 | +
|
| 49 | + This implementation does not rely on in-memory queue structures. Each |
| 50 | + instance manages its own read cursor (last_id). `tap()` returns a new |
| 51 | + RedisEventQueue pointing to the same stream but starting at '$' so it |
| 52 | + receives only future events. |
| 53 | + """ |
| 54 | + |
| 55 | + def __init__( |
| 56 | + self, |
| 57 | + task_id: str, |
| 58 | + redis_client: Any, |
| 59 | + stream_prefix: str = 'a2a:task', |
| 60 | + maxlen: int | None = None, |
| 61 | + read_block_ms: int = 500, |
| 62 | + ) -> None: |
| 63 | + # Allow passing a custom redis client (e.g. a fake in tests). |
| 64 | + if aioredis is None and redis_client is None: |
| 65 | + raise RedisNotAvailableError('redis.asyncio is not available') |
| 66 | + |
| 67 | + self._task_id = task_id |
| 68 | + self._redis = redis_client |
| 69 | + self._stream_key = f'{stream_prefix}:{task_id}' |
| 70 | + self._maxlen = maxlen |
| 71 | + self._read_block_ms = read_block_ms |
| 72 | + |
| 73 | + # By default a normal queue should start at the beginning so it can |
| 74 | + # consume existing entries. Taps will explicitly start at '$'. |
| 75 | + self._last_id = '0-0' |
| 76 | + self._is_closed = False |
| 77 | + |
| 78 | + # No in-memory queue initialization — this class is Redis-native. |
| 79 | + |
| 80 | + async def enqueue_event(self, event: Event) -> None: |
| 81 | + """Serialize and append an event to the Redis stream.""" |
| 82 | + if self._is_closed: |
| 83 | + logger.warning('Attempt to enqueue to closed RedisEventQueue') |
| 84 | + return |
| 85 | + # Store payload as a JSON string to avoid client-specific mapping |
| 86 | + # behaviour when reading back from the stream. |
| 87 | + payload = { |
| 88 | + 'type': type(event).__name__, |
| 89 | + 'payload': event.json(), |
| 90 | + } |
| 91 | + kwargs: dict[str, Any] = {} |
| 92 | + if self._maxlen: |
| 93 | + kwargs['maxlen'] = self._maxlen |
| 94 | + try: |
| 95 | + await self._redis.xadd(self._stream_key, payload, **kwargs) |
| 96 | + except RedisError: |
| 97 | + logger.exception('Failed to XADD event to redis stream') |
| 98 | + |
| 99 | + async def dequeue_event(self, no_wait: bool = False) -> Event | Any: |
| 100 | + """Read one event from the Redis stream respecting no_wait semantics. |
| 101 | +
|
| 102 | + Returns a parsed pydantic model matching the event type. |
| 103 | + """ |
| 104 | + if self._is_closed: |
| 105 | + raise asyncio.QueueEmpty('Queue is closed') |
| 106 | + |
| 107 | + block = 0 if no_wait else self._read_block_ms |
| 108 | + # Keep reading until we find a parseable payload or a CLOSE tombstone. |
| 109 | + while True: |
| 110 | + try: |
| 111 | + result = await self._redis.xread( |
| 112 | + {self._stream_key: self._last_id}, block=block, count=1 |
| 113 | + ) |
| 114 | + except RedisError: |
| 115 | + logger.exception('Failed to XREAD from redis stream') |
| 116 | + raise |
| 117 | + |
| 118 | + if not result: |
| 119 | + raise asyncio.QueueEmpty |
| 120 | + |
| 121 | + _, entries = result[0] |
| 122 | + entry_id, fields = entries[0] |
| 123 | + self._last_id = entry_id |
| 124 | + |
| 125 | + # Normalize keys/values: redis may return bytes for both keys and values |
| 126 | + norm: dict[str, object] = {} |
| 127 | + try: |
| 128 | + for k, v in fields.items(): |
| 129 | + key = k.decode('utf-8') if isinstance(k, (bytes, bytearray)) else k |
| 130 | + if isinstance(v, (bytes, bytearray)): |
| 131 | + try: |
| 132 | + val: object = v.decode('utf-8') |
| 133 | + except Exception: |
| 134 | + val = v |
| 135 | + else: |
| 136 | + val = v |
| 137 | + norm[str(key)] = val |
| 138 | + except Exception: |
| 139 | + # Defensive: if normalization fails, skip this entry and continue |
| 140 | + logger.debug('RedisEventQueue.dequeue_event: failed to normalize entry fields, skipping %s', entry_id) |
| 141 | + continue |
| 142 | + |
| 143 | + evt_type = norm.get('type') |
| 144 | + |
| 145 | + # Handle tombstone/close message |
| 146 | + if evt_type == 'CLOSE': |
| 147 | + self._is_closed = True |
| 148 | + raise asyncio.QueueEmpty('Queue closed') |
| 149 | + |
| 150 | + raw_payload = norm.get('payload') |
| 151 | + if raw_payload is None: |
| 152 | + # Missing payload — likely due to key mismatch or malformed entry. |
| 153 | + # Skip and continue to next entry instead of returning None to callers. |
| 154 | + logger.debug('RedisEventQueue.dequeue_event: skipping entry %s with missing payload', entry_id) |
| 155 | + # continue loop to read next entry |
| 156 | + continue |
| 157 | + |
| 158 | + # If payload is a JSON string, parse it; otherwise, use as-is. |
| 159 | + if isinstance(raw_payload, str): |
| 160 | + try: |
| 161 | + data = json.loads(raw_payload) |
| 162 | + except json.JSONDecodeError: |
| 163 | + data = raw_payload |
| 164 | + else: |
| 165 | + data = raw_payload |
| 166 | + |
| 167 | + model = _TYPE_MAP.get(evt_type) |
| 168 | + if model: |
| 169 | + try: |
| 170 | + return model.parse_obj(data) |
| 171 | + except ValidationError as exc: |
| 172 | + logger.exception('Failed to parse event payload into model') |
| 173 | + raise ValueError(f'Failed to parse event of type {evt_type}') from exc |
| 174 | + |
| 175 | + # Unknown type — return raw data for flexibility |
| 176 | + logger.debug('Unknown event type: %s, returning raw payload', evt_type) |
| 177 | + return data |
| 178 | + |
| 179 | + def task_done(self) -> None: # streams do not require task_done semantics |
| 180 | + """No-op for Redis streams (kept for API compatibility).""" |
| 181 | + |
| 182 | + def tap(self) -> EventQueue: |
| 183 | + """Return a new RedisEventQueue that starts at the stream tail ('$').""" |
| 184 | + q = RedisEventQueue( |
| 185 | + task_id=self._task_id, |
| 186 | + redis_client=self._redis, |
| 187 | + stream_prefix=self._stream_key.rsplit(':', 1)[0], |
| 188 | + maxlen=self._maxlen, |
| 189 | + read_block_ms=self._read_block_ms, |
| 190 | + ) |
| 191 | + # Set tap's cursor to the current last entry id so it receives only |
| 192 | + # events appended after this point. |
| 193 | + try: |
| 194 | + lst = getattr(self._redis, 'streams', {}).get(self._stream_key, []) |
| 195 | + if lst: |
| 196 | + q._last_id = lst[-1][0] |
| 197 | + else: |
| 198 | + q._last_id = '0-0' |
| 199 | + except (AttributeError, KeyError, IndexError, TypeError): |
| 200 | + # Fallback: start at stream tail if we can't determine the last ID |
| 201 | + q._last_id = '$' |
| 202 | + return q |
| 203 | + |
| 204 | + async def close(self, immediate: bool = False) -> None: |
| 205 | + """Mark the stream closed and publish a tombstone entry for readers.""" |
| 206 | + try: |
| 207 | + await self._redis.set(f'{self._stream_key}:closed', '1') |
| 208 | + await self._redis.xadd(self._stream_key, {'type': 'CLOSE'}) |
| 209 | + except RedisError: |
| 210 | + logger.exception('Failed to write close marker to redis') |
| 211 | + |
| 212 | + def is_closed(self) -> bool: |
| 213 | + """Return True if this queue has been closed (close() called).""" |
| 214 | + return self._is_closed |
| 215 | + |
| 216 | + async def clear_events(self, clear_child_queues: bool = True) -> None: |
| 217 | + """Attempt to remove the underlying redis stream (best-effort).""" |
| 218 | + try: |
| 219 | + await self._redis.delete(self._stream_key) |
| 220 | + except RedisError: |
| 221 | + logger.exception('Failed to delete redis stream during clear_events') |
0 commit comments