revert(bot): ⏪ Remove heartbeat system.

This commit is contained in:
Nyeki.py
2024-10-27 00:19:40 -03:00
parent aa445fbf63
commit 29204f5985
2 changed files with 14 additions and 37 deletions
-16
View File
@@ -42,14 +42,6 @@ class Payphone(commands.AutoShardedBot):
`shutdown`. `shutdown`.
""" """
heartbeat: managers.ValueProxy[int]
"""A value proxy that connects with the main process.
It's a timestamp, updated every second and checked by the main process every 5 seconds. If the
timestamp hasn't been updated for the past 10 seconds, it means that the process was stuck, so
the shard's process is restarted.
"""
redis: aredis.Redis = aredis.Redis(host="localhost" if config.IS_DEBUG else "redis") redis: aredis.Redis = aredis.Redis(host="localhost" if config.IS_DEBUG else "redis")
def __init__(self) -> None: def __init__(self) -> None:
@@ -69,12 +61,6 @@ class Payphone(commands.AutoShardedBot):
owner_ids=[898569996949676052, 777338793803513886], owner_ids=[898569996949676052, 777338793803513886],
) )
@tasks.loop(seconds=5)
async def update_heartbeat(self) -> None:
"""Updates the heartbeat timestamp."""
self.heartbeat.value = int(datetime.now(timezone.utc).timestamp())
async def on_ready(self) -> None: async def on_ready(self) -> None:
"""Initializes the bot. """Initializes the bot.
@@ -86,8 +72,6 @@ class Payphone(commands.AutoShardedBot):
await self.redis.ping() await self.redis.ping()
self.update_heartbeat.start()
print(f"Ready! Logged in as {self.user.display_name}.") print(f"Ready! Logged in as {self.user.display_name}.")
async def on_shard_ready(self, shard_id: int) -> None: async def on_shard_ready(self, shard_id: int) -> None:
+14 -21
View File
@@ -8,7 +8,6 @@ def shard(
shard_count: int, shard_count: int,
shutdown: managers.ValueProxy[bool], shutdown: managers.ValueProxy[bool],
scheduled_shutdown: managers.ValueProxy[bool], scheduled_shutdown: managers.ValueProxy[bool],
heartbeat: managers.ValueProxy[int]
) -> None: ) -> None:
"""Starts a new shard. """Starts a new shard.
@@ -18,7 +17,6 @@ def shard(
shutdown (ValueProxy[bool]): The flag for the main process to shut down all 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 scheduled_shutdown (ValueProxy[bool]): The flag for other shards to know the bot's gonna
shut down soon. shut down soon.
heartbeat (ValueProxy[int]): A timestamp updated every second by the shard's process.
""" """
from payphone.bot import bot from payphone.bot import bot
@@ -28,7 +26,6 @@ def shard(
bot.shard_count = shard_count bot.shard_count = shard_count
bot.shutdown = shutdown bot.shutdown = shutdown
bot.scheduled_shutdown = scheduled_shutdown bot.scheduled_shutdown = scheduled_shutdown
bot.heartbeat = heartbeat
try: try:
# Skip uvloop initialization, it stops tasks from running for some reason. # Skip uvloop initialization, it stops tasks from running for some reason.
@@ -112,14 +109,14 @@ if __name__ == "__main__":
manager = mp.Manager() manager = mp.Manager()
shutdown = manager.Value("b", False) shutdown = manager.Value("b", False)
scheduled_shutdown = manager.Value("b", False) scheduled_shutdown = manager.Value("b", False)
heartbeats = [
manager.Value("i", int(datetime.now(timezone.utc).timestamp())) for i in range(shard_count)
]
print(f"Starting {shard_count} shard{"s" if shard_count != 1 else ""}.") print(f"Starting {shard_count} shard{"s" if shard_count != 1 else ""}.")
ps: list[mp.Process] = [ ps: list[mp.Process] = [
mp.Process(target=shard, args=(i, shard_count, shutdown, scheduled_shutdown, heartbeats[i])) mp.Process(
target=shard,
args=(i, shard_count, shutdown, scheduled_shutdown),
)
for i in range(shard_count) for i in range(shard_count)
] ]
flushing_p = mp.Process(target=flush_process) flushing_p = mp.Process(target=flush_process)
@@ -151,22 +148,18 @@ if __name__ == "__main__":
raise ShutdownException() raise ShutdownException()
for i in range(shard_count): for i in range(shard_count):
is_heartbeat_updated = heartbeats[i].value > (datetime.now(timezone.utc) - timedelta(seconds=30)).timestamp() if not ps[i].is_alive():
print(f"Shard {i}'s process exited. Restarting the shard...")
if not ps[i].is_alive() or not is_heartbeat_updated:
if not ps[i].is_alive():
print(f"Shard {i}'s process exited. Restarting the shard...")
else:
print(f"Shard {i} did not update heartbeat. Terminating...")
ps[i].terminate()
ps[i].join()
print(f"Shard {i}'s process was terminated. Restarting the shard...")
ps[i] = mp.Process( ps[i] = mp.Process(
target=shard, args=(i, shard_count, shutdown, scheduled_shutdown, heartbeats[i]), name=f"payphone-shard-{i}" target=shard,
args=(
i,
shard_count,
shutdown,
scheduled_shutdown,
),
name=f"payphone-shard-{i}",
) )
ps[i].start() ps[i].start()