1
0
mirror of https://github.com/etesync/server synced 2024-11-22 16:58:08 +00:00
etesync-server/etebase_fastapi/redis.py
Xiretza b620d0a39c
fix(etebase_fastapi): fix crash on shutdown (#133)
self.redis isn't None, it's actually unset, so accessing it results
in an exception:

```
ERROR:    Traceback (most recent call last):
  File "./.venv/lib/python3.9/site-packages/starlette/routing.py", line 624, in lifespan
    await receive()
  File "./.venv/lib/python3.9/site-packages/starlette/routing.py", line 521, in __aexit__
    await self._router.shutdown()
  File "./.venv/lib/python3.9/site-packages/starlette/routing.py", line 608, in shutdown
    await handler()
  File "./etebase_fastapi/main.py", line 72, in on_shutdown
    await redisw.close()
  File "./etebase_fastapi/redis.py", line 18, in close
    if self.redis is not None:
AttributeError: 'RedisWrapper' object has no attribute 'redis'
```
2022-05-07 20:37:53 +03:00

28 lines
620 B
Python

import typing as t
import aioredis
from django_etebase import app_settings
class RedisWrapper:
redis: aioredis.Redis
def __init__(self, redis_uri: t.Optional[str]):
self.redis_uri = redis_uri
async def setup(self):
if self.redis_uri is not None:
self.redis = await aioredis.create_redis_pool(self.redis_uri)
async def close(self):
if hasattr(self, "redis"):
self.redis.close()
await self.redis.wait_closed()
@property
def is_active(self):
return self.redis_uri is not None
redisw = RedisWrapper(app_settings.REDIS_URI)