feat(bot): ✨ Add rich logging.

This commit is contained in:
Nyeki.py
2024-10-27 01:30:04 -03:00
parent ec00a557fa
commit 09d2808bd4
6 changed files with 141 additions and 47 deletions
+31 -21
View File
@@ -2,6 +2,13 @@ from multiprocessing import managers
from datetime import datetime, timezone
from payphone.core import logging
logging.setup()
log = logging.logger
def shard(
shard_id: int,
@@ -27,7 +34,7 @@ def shard(
bot.shutdown = shutdown
bot.scheduled_shutdown = scheduled_shutdown
print(f"Running shard {shard_id} with default loop.")
log.info("Running shard %s with default loop." % shard_id)
bot.run(config.TOKEN)
@@ -40,10 +47,10 @@ def flush_process():
import uvloop # pylint: disable=import-error
uvloop.install()
print("Flushing messages with uvloop.")
log.info("Flushing messages with uvloop.")
except ImportError:
print("Flushing messages with default loop.")
log.info("Flushing messages with default loop.")
finally:
asyncio.run(loop())
@@ -79,11 +86,11 @@ if __name__ == "__main__":
await redis.flushall()
print("Running DB and Redis cleanup...")
log.info("Cleanup done! Loading recommended shard amount...")
run_async(init())
print("Cleanup done! Loading recommended shard amount...")
log.info("Cleanup done! Loading recommended shard amount...")
response = httpx.get(
"https://discord.com/api/v10/gateway/bot",
@@ -98,7 +105,7 @@ if __name__ == "__main__":
shutdown = manager.Value("b", False)
scheduled_shutdown = manager.Value("b", False)
print(f"Starting {shard_count} shard{"s" if shard_count != 1 else ""}.")
log.info("Starting %s shards." % shard_count)
ps: list[mp.Process] = [
mp.Process(
@@ -112,7 +119,7 @@ if __name__ == "__main__":
for i, p in enumerate(ps):
p.start()
print(
log.info(
f"Shard {i}'s process started."
+ (
" Waiting 5 seconds start initialize next shard..."
@@ -124,11 +131,11 @@ if __name__ == "__main__":
if i != len(ps) - 1:
time.sleep(5)
print("Starting messages flushing process...")
log.info("Starting messages flushing process...")
flushing_p.start()
print("Messages flushing process started.")
log.info("Messages flushing process started.")
try:
while True:
@@ -137,7 +144,9 @@ if __name__ == "__main__":
for i in range(shard_count):
if not ps[i].is_alive():
print(f"Shard {i}'s process exited. Restarting the shard...")
log.warning(
"Shard %s's process exited. Restarting the shard..." % s
)
ps[i] = mp.Process(
target=shard,
@@ -151,43 +160,44 @@ if __name__ == "__main__":
)
ps[i].start()
print(
f"Shard {i} was restarted. Waiting 5 seconds to check next shard..."
log.info(
"Shard %s was restarted. Waiting 5 seconds to check next shard..."
% i
)
time.sleep(5)
if not flushing_p.is_alive():
print("Messages flushing process was stopped. Restarting it...")
log.info("Messages flushing process was stopped. Restarting it...")
flushing_p = mp.Process(target=flush_process, name="payphone-flushing")
flushing_p.start()
print("Messages flushing process started.")
log.info("Messages flushing process started.")
time.sleep(5)
except KeyboardInterrupt:
print("Ctrl-C was pressed.")
log.info("Ctrl-C was pressed.")
except ShutdownException:
print("A shard shut down the bot.")
log.info("A shard shut down the bot.")
finally:
print(f"Terminating {shard_count} shards...")
log.info("Terminating %s shards..." % shard_count)
for i, p in enumerate(ps):
p.terminate()
print(f"Terminated shard {i}.")
log.info("Terminated shard %s." % i)
print("Stopping messages flushing process...")
log.info("Stopping messages flushing process...")
flushing_p.terminate()
print("Checking that all processes have finished...")
log.info("Checking that all processes have finished...")
for p in ps:
p.join()
flushing_p.join()
print("All processes have been terminated.")
log.info("All processes have been terminated.")