2021-01-04 08:02:47 +00:00
|
|
|
import typing as t
|
2022-09-04 17:51:17 +00:00
|
|
|
from redis import asyncio as aioredis
|
2021-01-04 08:02:47 +00:00
|
|
|
|
2022-05-08 14:32:29 +00:00
|
|
|
from etebase_server.django import app_settings
|
2021-01-04 08:02:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2022-09-04 17:51:17 +00:00
|
|
|
self.redis = await aioredis.from_url(self.redis_uri)
|
2021-01-04 08:02:47 +00:00
|
|
|
|
|
|
|
async def close(self):
|
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 17:37:53 +00:00
|
|
|
if hasattr(self, "redis"):
|
2022-09-04 17:51:17 +00:00
|
|
|
await self.redis.close()
|
2021-01-04 08:02:47 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_active(self):
|
|
|
|
return self.redis_uri is not None
|
|
|
|
|
|
|
|
|
|
|
|
redisw = RedisWrapper(app_settings.REDIS_URI)
|