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
+14 -21
View File
@@ -8,7 +8,6 @@ def shard(
shard_count: int,
shutdown: managers.ValueProxy[bool],
scheduled_shutdown: managers.ValueProxy[bool],
heartbeat: managers.ValueProxy[int]
) -> None:
"""Starts a new shard.
@@ -18,7 +17,6 @@ def shard(
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.
heartbeat (ValueProxy[int]): A timestamp updated every second by the shard's process.
"""
from payphone.bot import bot
@@ -28,7 +26,6 @@ def shard(
bot.shard_count = shard_count
bot.shutdown = shutdown
bot.scheduled_shutdown = scheduled_shutdown
bot.heartbeat = heartbeat
try:
# Skip uvloop initialization, it stops tasks from running for some reason.
@@ -112,14 +109,14 @@ if __name__ == "__main__":
manager = mp.Manager()
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 ""}.")
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)
]
flushing_p = mp.Process(target=flush_process)
@@ -151,22 +148,18 @@ if __name__ == "__main__":
raise ShutdownException()
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() 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...")
if not ps[i].is_alive():
print(f"Shard {i}'s process exited. Restarting the shard...")
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()