fix(bot): 🐛 Redis failing to connect in different event loops.

This commit is contained in:
Nyeki.py
2024-10-23 23:33:25 -03:00
parent 7acfb87910
commit c5143de3ba
11 changed files with 133 additions and 90 deletions
+33 -3
View File
@@ -15,7 +15,17 @@ def shard(shard_id: int, shard_count: int) -> None:
bot.shard_ids = [shard_id]
bot.shard_count = shard_count
bot.run(config.TOKEN)
try:
import uvloop # pylint: disable=import-error
bot.loop = uvloop.new_event_loop()
print(f"Running shard {shard_id} with uvloop.")
except ImportError:
print("Running with default loop.")
finally:
bot.run(config.TOKEN)
def flush_process():
@@ -23,7 +33,24 @@ def flush_process():
from procs.flush_messages import loop
asyncio.run(loop())
try:
import uvloop # pylint: disable=import-error
uvloop.install()
print("Flushing messages with uvloop.")
except ImportError:
try:
import winloop
winloop.install()
print("Flushing messages with winloop.")
except ImportError:
print("Could not find winloop nor uvloop for messages flushing.")
finally:
asyncio.run(loop())
if __name__ == "__main__":
@@ -35,9 +62,12 @@ if __name__ == "__main__":
import httpx
import redis.asyncio as aredis
from payphone.db import setup, models
from payphone.core import config
from payphone.core.redis import redis
redis = aredis.Redis(host="localhost" if config.IS_DEBUG else "redis")
async def init():
await setup.init()