diff --git a/payphone/bot.py b/payphone/bot.py index 2bd2888..af9658b 100644 --- a/payphone/bot.py +++ b/payphone/bot.py @@ -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") diff --git a/payphone/cogs/calls/cog.py b/payphone/cogs/calls/cog.py index 10a1f5d..e714fec 100644 --- a/payphone/cogs/calls/cog.py +++ b/payphone/cogs/calls/cog.py @@ -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 "" ) ) diff --git a/payphone/cogs/mods/cog.py b/payphone/cogs/mods/cog.py index 3428224..f8d950c 100644 --- a/payphone/cogs/mods/cog.py +++ b/payphone/cogs/mods/cog.py @@ -243,6 +243,8 @@ class Mods(base.Cog): - `[]`: The reason of the shutdown. """ + self.bot.scheduled_shutdown.value = True + oncall_channel_ids = list( await self.bot.redis.smembers(constants.CHANNELS_ONCALL_SET) ) diff --git a/run.py b/run.py index 08b771e..7313d8b 100644 --- a/run.py +++ b/run.py @@ -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)