refactor: refactored working with redis connections
This commit is contained in:
parent
ea907d0bf2
commit
1a5b4d0e63
5
bot.py
5
bot.py
|
@ -11,7 +11,8 @@ from data import config
|
||||||
|
|
||||||
# noinspection PyUnusedLocal
|
# noinspection PyUnusedLocal
|
||||||
async def on_startup(app: web.Application):
|
async def on_startup(app: web.Application):
|
||||||
import middlewares, filters
|
import middlewares
|
||||||
|
import filters
|
||||||
import handlers
|
import handlers
|
||||||
middlewares.setup(dp)
|
middlewares.setup(dp)
|
||||||
filters.setup(dp)
|
filters.setup(dp)
|
||||||
|
@ -46,7 +47,7 @@ async def init() -> web.Application:
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
bot = Bot(config.BOT_TOKEN, parse_mode=ParseMode.HTML, validate_token=True)
|
bot = Bot(config.BOT_TOKEN, parse_mode=ParseMode.HTML, validate_token=True)
|
||||||
storage = RedisStorage2(**config.aiogram_redis)
|
storage = RedisStorage2(**config.redis)
|
||||||
dp = Dispatcher(bot, storage=storage)
|
dp = Dispatcher(bot, storage=storage)
|
||||||
|
|
||||||
web.run_app(init())
|
web.run_app(init())
|
||||||
|
|
|
@ -2,7 +2,7 @@ from pathlib import Path
|
||||||
|
|
||||||
BOT_TOKEN = ''
|
BOT_TOKEN = ''
|
||||||
BASE_URL = 'https://example.com' # Webhook domain
|
BASE_URL = 'https://example.com' # Webhook domain
|
||||||
WEBHOOK_PATH = f'/webhook/bot/{BOT_TOKEN}'
|
WEBHOOK_PATH = f'/tg/webhooks/bot/{BOT_TOKEN}'
|
||||||
WEBHOOK_URL = f'{BASE_URL}{WEBHOOK_PATH}'
|
WEBHOOK_URL = f'{BASE_URL}{WEBHOOK_PATH}'
|
||||||
|
|
||||||
LOGS_BASE_PATH = str(Path(__file__).parent.parent / 'logs')
|
LOGS_BASE_PATH = str(Path(__file__).parent.parent / 'logs')
|
||||||
|
@ -23,13 +23,7 @@ mysql_info = {
|
||||||
'port': 3306,
|
'port': 3306,
|
||||||
}
|
}
|
||||||
|
|
||||||
aiogram_redis = {
|
redis = {
|
||||||
'host': ip['redis'],
|
'host': ip['redis'],
|
||||||
'password': ''
|
'password': ''
|
||||||
}
|
}
|
||||||
|
|
||||||
redis = {
|
|
||||||
'address': (ip['redis'], 6379),
|
|
||||||
'password': '',
|
|
||||||
'encoding': 'utf8'
|
|
||||||
}
|
|
||||||
|
|
|
@ -13,4 +13,42 @@ async def create_pools():
|
||||||
data_pool = await aioredis.create_redis_pool(**config.redis, db=1)
|
data_pool = await aioredis.create_redis_pool(**config.redis, db=1)
|
||||||
|
|
||||||
|
|
||||||
|
class BaseRedis:
|
||||||
|
def __init__(self, host: str, port: int = 6379, db: int = 0, password: str = ''):
|
||||||
|
self.host = host
|
||||||
|
self.port = port
|
||||||
|
self.db = db
|
||||||
|
self.password = password
|
||||||
|
|
||||||
|
self._redis: Optional[aioredis.Redis] = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def closed(self):
|
||||||
|
return not self._redis or self._redis.closed
|
||||||
|
|
||||||
|
async def connect(self):
|
||||||
|
if self.closed:
|
||||||
|
self._redis = await aioredis.create_redis_pool((self.host, self.port), db=self.db, password=self.password,
|
||||||
|
timeout=10, encoding='utf8')
|
||||||
|
|
||||||
|
async def disconnect(self):
|
||||||
|
if not self.closed:
|
||||||
|
self._redis.close()
|
||||||
|
await self._redis.wait_closed()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def redis(self) -> aioredis.Redis:
|
||||||
|
if self.closed:
|
||||||
|
if not self._redis:
|
||||||
|
try:
|
||||||
|
await self.connect()
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
raise TimeoutError('Redis connection timeout')
|
||||||
|
else:
|
||||||
|
return self._redis
|
||||||
|
else:
|
||||||
|
raise RuntimeError("Redis connection is not opened")
|
||||||
|
return self._redis
|
||||||
|
|
||||||
|
|
||||||
asyncio.get_event_loop().run_until_complete(create_pools())
|
asyncio.get_event_loop().run_until_complete(create_pools())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user