Skip to content

Commit

Permalink
appservice: Retry redis connection
Browse files Browse the repository at this point in the history
The pod starts up in parallel with appservice, so redis may not yet be
ready. This caused an occasional CI failure, which can be reproduced
with delaying the redis startup in webconsoleapp-local.yaml:

    command: ["sh", "-ec", "sleep 2; redis-server"]
  • Loading branch information
martinpitt committed Sep 22, 2022
1 parent e1d438b commit ef60b95
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions appservice/multiplexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import uuid

import httpx
import redis.asyncio as redis
import redis.exceptions
import redis.asyncio
import uvicorn
import websockets

Expand All @@ -25,7 +26,7 @@
# states: wait_target or running
SESSIONS = {}

REDIS = redis.Redis(host=os.environ['REDIS_SERVICE_HOST'], port=int(os.environ.get('REDIS_SERVICE_PORT', '6379')))
REDIS = redis.asyncio.Redis(host=os.environ['REDIS_SERVICE_HOST'], port=int(os.environ.get('REDIS_SERVICE_PORT', '6379')))
logger = logging.getLogger('multiplexer')
app = Starlette()

Expand Down Expand Up @@ -217,7 +218,17 @@ async def init_sessions():
global SESSIONS

pubsub = REDIS.pubsub()
await pubsub.subscribe('sessions')
# wait for Redis service to be up
for retry in range(10):
try:
await pubsub.subscribe('sessions')
break
except redis.exceptions.ConnectionError as e:
logger.warning('Failed to connect to Redis, retry %i: %s', retry, e)
await asyncio.sleep(retry * retry + 1)
else:
raise RuntimeError('timed out trying to connect to Redis')

asyncio.create_task(watch_redis(pubsub))

sessions = await REDIS.get('sessions')
Expand Down

0 comments on commit ef60b95

Please sign in to comment.