fix(bot): 🐛 p.call and p.skip not warning about scheduled shutdown.

This commit is contained in:
Nyeki.py
2024-10-24 01:02:27 -03:00
parent b3184fafdf
commit afad9250b6
4 changed files with 34 additions and 10 deletions
+13
View File
@@ -27,6 +27,19 @@ class Payphone(commands.AutoShardedBot):
This value is shared between shard processes, and if any of them sets this proxy's value to
`True`, then all shard processes will be terminated by the main process (`run.py`).
To check if the shutdown has been scheduled (but not necessarily is immediately about to
occur), use `scheduled_shutdown` instead.
"""
scheduled_shutdown: managers.ValueProxy[bool]
"""A value proxy that connects with all other shard processes.
This value is shared between shard processes, and if any of them sets this proxy's value to
`True`, then the bot's shutdown has been started and all shards will be terminated soon.
To check whether the bot's about to terminate (if the message was sent to the main process) use
`shutdown`.
"""
redis: aredis.Redis = aredis.Redis(host="localhost" if config.IS_DEBUG else "redis")
+8 -8
View File
@@ -238,8 +238,8 @@ class Calls(base.Cog):
f"☎️ {secrets.choice(constants.MSGS_PHONE_CALLING)}\n"
)
+ (
"⚡ The phone will be hung up soon due to a scheduled restart.\n"
if self.bot.shutdown.value
"⚠️ The phone will be hung up soon due to a scheduled restart.\n"
if self.bot.scheduled_shutdown.value
else ""
)
+ msg_phone_answered
@@ -254,8 +254,8 @@ class Calls(base.Cog):
f"☎️ {secrets.choice(constants.MSGS_PHONE_CALLING)}\n"
)
+ (
"⚡ The phone will be hung up soon due to a scheduled restart.\n"
if self.bot.shutdown.value
"⚠️ The phone will be hung up soon due to a scheduled restart.\n"
if self.bot.scheduled_shutdown.value
else ""
)
)
@@ -435,8 +435,8 @@ class Calls(base.Cog):
f"☎️ You skipped the call.\n"
)
+ (
"⚡ The phone will be hung up soon due to a scheduled restart.\n"
if self.bot.shutdown.value
"⚠️ The phone will be hung up soon due to a scheduled restart.\n"
if self.bot.scheduled_shutdown.value
else ""
)
+ (
@@ -459,8 +459,8 @@ class Calls(base.Cog):
f"☎️ {secrets.choice(constants.MSGS_PHONE_CALLING)}\n"
)
+ (
"⚡ The phone will be hung up soon due to a scheduled restart.\n"
if self.bot.shutdown.value
"⚠️ The phone will be hung up soon due to a scheduled restart.\n"
if self.bot.scheduled_shutdown.value
else ""
)
)
+2
View File
@@ -243,6 +243,8 @@ class Mods(base.Cog):
- `[<reason>]`: The reason of the shutdown.
"""
self.bot.scheduled_shutdown.value = True
oncall_channel_ids = list(
await self.bot.redis.smembers(constants.CHANNELS_ONCALL_SET)
)
+11 -2
View File
@@ -3,13 +3,20 @@ from multiprocessing import managers
from datetime import datetime, timezone
def shard(shard_id: int, shard_count: int, shutdown: managers.ValueProxy[bool]) -> None:
def shard(
shard_id: int,
shard_count: int,
shutdown: managers.ValueProxy[bool],
scheduled_shutdown: managers.ValueProxy[bool],
) -> None:
"""Starts a new shard.
Args:
shard_id (int): The shard's ID.
shard_count (int): The total amount of shards.
shutdown (ValueProxy[bool]): The flag for the main process to shut down all shards.
scheduled_shutdown (ValueProxy[bool]): The flag for other shards to know the bot's gonna
shut down soon.
"""
from payphone.bot import bot
@@ -18,6 +25,7 @@ def shard(shard_id: int, shard_count: int, shutdown: managers.ValueProxy[bool])
bot.shard_ids = [shard_id]
bot.shard_count = shard_count
bot.shutdown = shutdown
bot.scheduled_shutdown = scheduled_shutdown
try:
import uvloop # pylint: disable=import-error
@@ -97,11 +105,12 @@ if __name__ == "__main__":
manager = mp.Manager()
shutdown = manager.Value("b", False)
scheduled_shutdown = manager.Value("b", False)
print(f"Starting {shard_count} shard{"s" if shard_count != 1 else ""}.")
ps: list[mp.Process] = [
mp.Process(target=shard, args=(i, shard_count, shutdown))
mp.Process(target=shard, args=(i, shard_count, shutdown, scheduled_shutdown))
for i in range(shard_count)
]
flushing_p = mp.Process(target=flush_process)