feat(bot): ✨ Add a heartbeat to every shard process.
This commit is contained in:
+17
-1
@@ -8,7 +8,7 @@ import sys
|
|||||||
|
|
||||||
import discord
|
import discord
|
||||||
from discord import abc
|
from discord import abc
|
||||||
from discord.ext import commands
|
from discord.ext import commands, tasks
|
||||||
from discord.ext.commands import errors
|
from discord.ext.commands import errors
|
||||||
|
|
||||||
from tortoise import expressions, transactions, exceptions as tortoise_exc
|
from tortoise import expressions, transactions, exceptions as tortoise_exc
|
||||||
@@ -42,6 +42,14 @@ 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:
|
||||||
@@ -72,6 +80,8 @@ 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:
|
||||||
@@ -441,6 +451,12 @@ class Payphone(commands.AutoShardedBot):
|
|||||||
return discord.EmbedAuthor(
|
return discord.EmbedAuthor(
|
||||||
name=self.user.display_name, icon_url=self.user.display_avatar.url
|
name=self.user.display_name, icon_url=self.user.display_avatar.url
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@tasks.loop(seconds=1)
|
||||||
|
async def update_heartbeat(self) -> None:
|
||||||
|
"""Updates the heartbeat timestamp."""
|
||||||
|
|
||||||
|
self.heartbeat.value = int(datetime.now(timezone.utc).timestamp())
|
||||||
|
|
||||||
|
|
||||||
bot = Payphone()
|
bot = Payphone()
|
||||||
|
|||||||
Reference in New Issue
Block a user