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
+45 -45
View File
@@ -20,7 +20,6 @@ from payphone.db import utils as dbutils, models
from payphone.cogs import base
from payphone.cogs.calls import embeds
from payphone.core import constants, checks, censorship, formatting, utils
from payphone.core.redis import redis
class Calls(base.Cog):
@@ -79,7 +78,7 @@ class Calls(base.Cog):
if len(hung_up_channel_ids) > 0:
await models.Channel.filter(id__in=hung_up_channel_ids).update(call_id=None)
await redis.srem(constants.CHANNELS_ONCALL_SET, *hung_up_channel_ids)
await self.bot.redis.srem(constants.CHANNELS_ONCALL_SET, *hung_up_channel_ids)
@commands.command(aliases=["c"])
@commands.guild_only()
@@ -97,7 +96,7 @@ class Calls(base.Cog):
user_id = ctx.author.id
channel_id = ctx.channel.id
is_on_call = await redis.sismember(
is_on_call = await self.bot.redis.sismember(
constants.CHANNELS_ONCALL_SET, str(channel_id)
)
@@ -107,7 +106,7 @@ class Calls(base.Cog):
)
return
is_searching = await redis.sismember(
is_searching = await self.bot.redis.sismember(
constants.CHANNELS_SEARCH_QUEUE, str(channel_id)
)
@@ -156,7 +155,7 @@ class Calls(base.Cog):
if last_calls_rss_channels_map[call_rs.call_id] != call_rs.channel_id:
last_calls_other_channels_ids.add(call_rs.channel_id)
other_channel_id: int = await redis.spop(constants.CHANNELS_SEARCH_QUEUE)
other_channel_id: int = await self.bot.redis.spop(constants.CHANNELS_SEARCH_QUEUE)
i = 0
while True:
@@ -165,7 +164,7 @@ class Calls(base.Cog):
other_channel_id = None
break
# Redis returns the ID as a bytes string.
# self.bot.redis returns the ID as a bytes string.
other_channel_id = int(other_channel_id)
if i == 5:
@@ -174,9 +173,9 @@ class Calls(base.Cog):
i += 1
if other_channel_id in last_calls_other_channels_ids:
await redis.sadd(constants.CHANNELS_SEARCH_QUEUE, str(other_channel_id))
await self.bot.redis.sadd(constants.CHANNELS_SEARCH_QUEUE, str(other_channel_id))
other_channel_id = await redis.spop(constants.CHANNELS_SEARCH_QUEUE)
other_channel_id = await self.bot.redis.spop(constants.CHANNELS_SEARCH_QUEUE)
if other_channel_id is None:
break
@@ -188,7 +187,7 @@ class Calls(base.Cog):
channel = await dbutils.get_channel(ctx.channel)
if other_channel_id is not None:
await redis.sadd(
await self.bot.redis.sadd(
constants.CHANNELS_ONCALL_SET, str(channel_id), str(other_channel_id)
)
@@ -217,12 +216,12 @@ class Calls(base.Cog):
).update(call=call)
except errors.DiscordException:
await redis.srem(
await self.bot.redis.srem(
constants.CHANNELS_ONCALL_SET,
str(channel_id),
str(other_channel_id),
)
await redis.sadd(constants.CHANNELS_SEARCH_QUEUE, str(channel_id))
await self.bot.redis.sadd(constants.CHANNELS_SEARCH_QUEUE, str(channel_id))
msg_phone_answered = ""
await ctx.send(
@@ -234,7 +233,7 @@ class Calls(base.Cog):
)
else:
await redis.sadd(constants.CHANNELS_SEARCH_QUEUE, str(channel_id))
await self.bot.redis.sadd(constants.CHANNELS_SEARCH_QUEUE, str(channel_id))
await ctx.send(
(
@@ -259,7 +258,7 @@ class Calls(base.Cog):
user_id = ctx.author.id
channel_id = ctx.channel.id
is_on_call = await redis.sismember(
is_on_call = await self.bot.redis.sismember(
constants.CHANNELS_ONCALL_SET, str(channel_id)
)
@@ -271,7 +270,7 @@ class Calls(base.Cog):
"channels"
)
await redis.sadd(constants.CALL_IDS_TO_FLUSH, call.id)
await self.bot.redis.sadd(constants.CALL_IDS_TO_FLUSH, call.id)
call.ended_at = datetime.now(timezone.utc)
await call.save()
@@ -290,7 +289,7 @@ class Calls(base.Cog):
except errors.DiscordException:
pass
await redis.srem(
await self.bot.redis.srem(
constants.CHANNELS_ONCALL_SET,
*(
[str(channel_id)]
@@ -337,7 +336,7 @@ class Calls(base.Cog):
if last_calls_rss_channels_map[call_rs.call_id] != call_rs.channel_id:
last_calls_other_channels_ids.add(call_rs.channel_id)
other_channel_id: int = await redis.spop(constants.CHANNELS_SEARCH_QUEUE)
other_channel_id: int = await self.bot.redis.spop(constants.CHANNELS_SEARCH_QUEUE)
i = 0
while True:
@@ -346,7 +345,7 @@ class Calls(base.Cog):
other_channel_id = None
break
# Redis returns the ID as a bytes string.
# self.bot.redis returns the ID as a bytes string.
other_channel_id = int(other_channel_id)
if i == 5:
@@ -355,9 +354,9 @@ class Calls(base.Cog):
i += 1
if other_channel_id in last_calls_other_channels_ids:
await redis.sadd(constants.CHANNELS_SEARCH_QUEUE, str(other_channel_id))
await self.bot.redis.sadd(constants.CHANNELS_SEARCH_QUEUE, str(other_channel_id))
other_channel_id = await redis.spop(constants.CHANNELS_SEARCH_QUEUE)
other_channel_id = await self.bot.redis.spop(constants.CHANNELS_SEARCH_QUEUE)
if other_channel_id is None:
break
@@ -367,7 +366,7 @@ class Calls(base.Cog):
break
if other_channel_id is not None:
await redis.sadd(
await self.bot.redis.sadd(
constants.CHANNELS_ONCALL_SET, str(channel_id), str(other_channel_id)
)
@@ -396,12 +395,12 @@ class Calls(base.Cog):
).update(call=call)
except errors.DiscordException:
await redis.srem(
await self.bot.redis.srem(
constants.CHANNELS_ONCALL_SET,
str(channel_id),
str(other_channel_id),
)
await redis.sadd(constants.CHANNELS_SEARCH_QUEUE, str(channel_id))
await self.bot.redis.sadd(constants.CHANNELS_SEARCH_QUEUE, str(channel_id))
msg_phone_answered = ""
await ctx.send(
@@ -417,7 +416,7 @@ class Calls(base.Cog):
)
else:
await redis.sadd(constants.CHANNELS_SEARCH_QUEUE, str(channel_id))
await self.bot.redis.sadd(constants.CHANNELS_SEARCH_QUEUE, str(channel_id))
channel.call = None
await channel.save()
@@ -441,7 +440,7 @@ class Calls(base.Cog):
channel_id = ctx.channel.id
is_on_call = await redis.sismember(
is_on_call = await self.bot.redis.sismember(
constants.CHANNELS_ONCALL_SET, str(channel_id)
)
@@ -452,13 +451,13 @@ class Calls(base.Cog):
"channels"
)
await redis.sadd(constants.CALL_IDS_TO_FLUSH, call.id)
await self.bot.redis.sadd(constants.CALL_IDS_TO_FLUSH, call.id)
other_channel: models.Channel = find(
lambda c: c.id != channel_id, call.channels
)
await redis.srem(
await self.bot.redis.srem(
constants.CHANNELS_ONCALL_SET,
*(
[str(channel_id)]
@@ -489,12 +488,12 @@ class Calls(base.Cog):
call.ended_at = datetime.now(timezone.utc)
await call.save()
is_searching = await redis.sismember(
is_searching = await self.bot.redis.sismember(
constants.CHANNELS_SEARCH_QUEUE, str(channel_id)
)
if is_searching:
await redis.srem(constants.CHANNELS_SEARCH_QUEUE, str(channel_id))
await self.bot.redis.srem(constants.CHANNELS_SEARCH_QUEUE, str(channel_id))
await ctx.send(f"☎️ {secrets.choice(constants.MSGS_PHONE_NO_ANSWER)}")
@@ -577,7 +576,7 @@ class Calls(base.Cog):
call.ended_at = datetime.now(timezone.utc)
await call.save()
await redis.sadd(constants.CALL_IDS_TO_FLUSH, call.id)
await self.bot.redis.sadd(constants.CALL_IDS_TO_FLUSH, call.id)
updated_channels = []
@@ -587,7 +586,7 @@ class Calls(base.Cog):
await models.Channel.bulk_update(updated_channels, ["call_id"])
await redis.srem(
await self.bot.redis.srem(
constants.CHANNELS_ONCALL_SET,
str(updated_channels[0]),
str(updated_channels[1]),
@@ -597,7 +596,7 @@ class Calls(base.Cog):
other_channel = find(lambda c: c.id != channel_id, updated_channels)
await redis.srem(
await self.bot.redis.srem(
constants.CHANNELS_ONCALL_SET, str(other_channel.id), str(channel_id)
)
@@ -666,7 +665,7 @@ class Calls(base.Cog):
user_id = message.author.id
channel_id = message.channel.id
is_on_call = await redis.sismember(
is_on_call = await self.bot.redis.sismember(
constants.CHANNELS_ONCALL_SET, str(channel_id)
)
@@ -686,7 +685,7 @@ class Calls(base.Cog):
)
except exceptions.DoesNotExist:
await redis.srem(constants.CHANNELS_ONCALL_SET, str(channel_id))
await self.bot.redis.srem(constants.CHANNELS_ONCALL_SET, str(channel_id))
return
other_channel: models.Channel = find(
@@ -755,6 +754,7 @@ class Calls(base.Cog):
if message.reference:
embed, replying_message = await embeds.ReplyEmbed(
self.bot.redis,
message.reference, call_id=call.id
)
@@ -769,7 +769,7 @@ class Calls(base.Cog):
wait=True,
)
pipe = redis.pipeline()
pipe = self.bot.redis.pipeline()
pipe.hset(
constants.CALL_MESSAGES_TEMPLATE % call.id,
@@ -815,7 +815,7 @@ class Calls(base.Cog):
)
if replying_to_id is not None:
other_replies = await redis.hget(
other_replies = await self.bot.redis.hget(
constants.CALL_MESSAGE_REPLIES_TEMPLATE % call.id, replying_to_id
)
@@ -858,7 +858,7 @@ class Calls(base.Cog):
if channel.call_id is None:
return
message_json = await redis.hget(
message_json = await self.bot.redis.hget(
constants.CALL_MESSAGES_TEMPLATE % channel.call_id, message_id
)
@@ -871,7 +871,7 @@ class Calls(base.Cog):
message_dict["was_deleted"] = True
await redis.hset(
await self.bot.redis.hset(
constants.CALL_MESSAGES_TEMPLATE % channel.call_id,
message.id,
orjson.dumps(message_dict),
@@ -892,7 +892,7 @@ class Calls(base.Cog):
# This message was already deleted.
pass
message_replies = await redis.hget(
message_replies = await self.bot.redis.hget(
constants.CALL_MESSAGE_REPLIES_TEMPLATE % channel.call_id, message.id
)
@@ -937,7 +937,7 @@ class Calls(base.Cog):
if channel.call_id is None:
return
message_json = await redis.hget(
message_json = await self.bot.redis.hget(
constants.CALL_MESSAGES_TEMPLATE % channel.call_id, message_id
)
@@ -967,13 +967,13 @@ class Calls(base.Cog):
blocked_word = censorship.moderate(content)
iteration = int(
await redis.hget(
await self.bot.redis.hget(
constants.CALL_MESSAGE_CONTENT_ITERATIONS_TEMPLATE % channel.call_id,
message.id,
)
)
pipe = redis.pipeline()
pipe = self.bot.redis.pipeline()
pipe.hincrby(
constants.CALL_MESSAGE_CONTENT_ITERATIONS_TEMPLATE % channel.call_id,
@@ -1031,13 +1031,13 @@ class Calls(base.Cog):
call_id = channel.call_id
other_channel_id = None
other_channel_message_id = await redis.hget(
other_channel_message_id = await self.bot.redis.hget(
constants.CALL_MESSAGE_FROM_PROPAGATION_TEMPLATE % call_id,
payload.message_id,
)
if other_channel_message_id is None:
other_channel_message_json = await redis.hget(
other_channel_message_json = await self.bot.redis.hget(
constants.CALL_MESSAGES_TEMPLATE % call_id, payload.message_id
)
@@ -1093,13 +1093,13 @@ class Calls(base.Cog):
call_id = channel.call_id
other_channel_id = None
other_channel_message_id = await redis.hget(
other_channel_message_id = await self.bot.redis.hget(
constants.CALL_MESSAGE_FROM_PROPAGATION_TEMPLATE % call_id,
payload.message_id,
)
if other_channel_message_id is None:
other_channel_message_json = await redis.hget(
other_channel_message_json = await self.bot.redis.hget(
constants.CALL_MESSAGES_TEMPLATE % call_id, payload.message_id
)