Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions hyperforge/src/hyperforge/broker/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import opentelemetry.propagate
from pydantic import TypeAdapter
from redis.asyncio import Redis, ResponseError
from redis.exceptions import ConnectionError as RedisConnectionError
from redis.exceptions import TimeoutError as RedisTimeoutError

from hyperforge import logger
from hyperforge.broker import AgentTimeoutError, Broker
Expand Down Expand Up @@ -66,12 +68,23 @@ async def _ensure_consumer_group(self) -> None:
)
except ResponseError as e:
if "BUSYGROUP" not in str(e):
logger.exception("Failed to create consumer group, will retry...")
raise

async def subscribe_activations(
self,
) -> AsyncIterator[tuple[StartInteraction, dict[str, str]]]:
await self._ensure_consumer_group()
max_retries = 10
for attempt in range(1, max_retries + 1):
try:
await self._ensure_consumer_group()
break
except (ResponseError, RedisConnectionError, RedisTimeoutError):
if attempt == max_retries:
logger.error("Failed to create consumer group after %d attempts, giving up.", max_retries)
raise
logger.warning("Failed to create consumer group (attempt %d/%d), retrying...", attempt, max_retries)
await asyncio.sleep(10)

while True:
try:
Expand All @@ -97,7 +110,14 @@ async def subscribe_activations(
except ResponseError as e:
if "NOGROUP" in str(e):
logger.warning("Consumer group lost, re-creating...")
await self._ensure_consumer_group()
try:
await self._ensure_consumer_group()
except (
ResponseError,
RedisConnectionError,
RedisTimeoutError,
):
await asyncio.sleep(1)
else:
logger.exception(
"Error while subscribing to activations, retrying..."
Expand Down
Loading