revert(bot): ⏪ Remove heartbeat system.
This commit is contained in:
@@ -42,14 +42,6 @@ class Payphone(commands.AutoShardedBot):
|
||||
`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")
|
||||
|
||||
def __init__(self) -> None:
|
||||
@@ -69,12 +61,6 @@ class Payphone(commands.AutoShardedBot):
|
||||
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:
|
||||
"""Initializes the bot.
|
||||
|
||||
@@ -86,8 +72,6 @@ class Payphone(commands.AutoShardedBot):
|
||||
|
||||
await self.redis.ping()
|
||||
|
||||
self.update_heartbeat.start()
|
||||
|
||||
print(f"Ready! Logged in as {self.user.display_name}.")
|
||||
|
||||
async def on_shard_ready(self, shard_id: int) -> None:
|
||||
|
||||
@@ -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...")
|
||||
|
||||
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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user