1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Improved logging

This commit is contained in:
Steffo 2018-08-16 19:33:38 +02:00
parent cb26ada017
commit 42de72a03d
3 changed files with 31 additions and 10 deletions

18
bots.py
View file

@ -4,30 +4,40 @@ import telegrambot
import discordbot import discordbot
import time import time
import platform import platform
import logging
logging.getLogger().setLevel(level=logging.ERROR)
logging.getLogger(__name__).setLevel(level=logging.DEBUG)
discord_telegram_pipe = multiprocessing.Pipe() discord_telegram_pipe = multiprocessing.Pipe()
discord = multiprocessing.Process(target=discordbot.process, args=(discord_telegram_pipe[0],), daemon=True) discord = multiprocessing.Process(target=discordbot.process, args=(discord_telegram_pipe[0],), daemon=True)
telegram = multiprocessing.Process(target=telegrambot.process, args=(discord_telegram_pipe[1],), daemon=True) telegram = multiprocessing.Process(target=telegrambot.process, args=(discord_telegram_pipe[1],), daemon=True)
if __name__ == "__main__": if __name__ == "__main__":
logging.info("Starting Discord process...")
discord.start() discord.start()
logging.info("Starting Telegram process...")
telegram.start() telegram.start()
try: try:
while True: while True:
if discord.exitcode is not None: if discord.exitcode is not None:
print("Restarting Discord Bot...") logging.warning(f"Discord Bot exited with {discord.exitcode}")
del discord del discord
logging.info("Starting Discord process...")
discord = multiprocessing.Process(target=discordbot.process, args=(discord_telegram_pipe[0],), daemon=True) discord = multiprocessing.Process(target=discordbot.process, args=(discord_telegram_pipe[0],), daemon=True)
discord.start() discord.start()
if telegram.exitcode is not None: if telegram.exitcode is not None:
print("Restarting Telegram Bot...") logging.warning(f"Telegram Bot exited with {discord.exitcode}")
del telegram del telegram
telegram = multiprocessing.Process(target=telegrambot.process, args=(discord_telegram_pipe[1],), daemon=True) telegram = multiprocessing.Process(target=telegrambot.process, args=(discord_telegram_pipe[1],), daemon=True)
logging.info("Starting Telegram process...")
telegram.start() telegram.start()
time.sleep(60) time.sleep(10)
except KeyboardInterrupt: except KeyboardInterrupt:
logging.info("Now stopping...")
if platform.system() == "Linux": if platform.system() == "Linux":
os.system("reset") os.system("reset")
print("Stopping...") logging.info("Asking Discord process to stop...")
discord_telegram_pipe[0].send("stop") discord_telegram_pipe[0].send("stop")
logging.info("Waiting for Discord process to stop...")
time.sleep(30) time.sleep(30)

View file

@ -1,7 +1,10 @@
import random import random
import re import re
# noinspection PyPackageRequirements
import discord import discord
# noinspection PyPackageRequirements
import discord.opus import discord.opus
# noinspection PyPackageRequirements
import discord.voice_client import discord.voice_client
import functools import functools
import sys import sys
@ -20,7 +23,8 @@ import errors
import datetime import datetime
import sqlalchemy.exc import sqlalchemy.exc
logging.getLogger().setLevel(level=20) logging.getLogger().setLevel(level=logging.ERROR)
logging.getLogger(__name__).setLevel(level=logging.DEBUG)
# Queue emojis # Queue emojis
queue_emojis = [":one:", queue_emojis = [":one:",
@ -51,7 +55,7 @@ song_special_messages = {
"sugma": ":arrow_forward: What is sugma? {song}!", "sugma": ":arrow_forward: What is sugma? {song}!",
"sugondese": ":arrow_forward: What is sugondese? {song}!", "sugondese": ":arrow_forward: What is sugondese? {song}!",
"bofa": ":arrow_forward: What is bofa? {song}!", "bofa": ":arrow_forward: What is bofa? {song}!",
"updog": ":arrow_forward: What is updog? {song}!", "updog": ":arrow_forward: What's up, dog? {song}!",
"sayo-nara": ":arrow_forward: I gently open the door. {song} awaits me inside.", "sayo-nara": ":arrow_forward: I gently open the door. {song} awaits me inside.",
"monika": ":arrow_forward: Just Monika. Just Monika. Just {song}.", "monika": ":arrow_forward: Just Monika. Just Monika. Just {song}.",
"take me home": ":arrow_forward: Take me home, to {song}, the place I belong!", "take me home": ":arrow_forward: Take me home, to {song}, the place I belong!",
@ -169,7 +173,8 @@ class Video:
"format": "best", "format": "best",
"postprocessors": [{ "postprocessors": [{
"key": 'FFmpegExtractAudio', "key": 'FFmpegExtractAudio',
"preferredcodec": 'opus' "preferredcodec": 'opus',
"postprocessor_args": '--quiet'
}], }],
"outtmpl": f"./opusfiles/{self.file}", "outtmpl": f"./opusfiles/{self.file}",
"progress_hooks": progress_hooks, "progress_hooks": progress_hooks,
@ -327,7 +332,7 @@ class RoyalDiscordBot(discord.Client):
async def on_error(self, event_method, *args, **kwargs): async def on_error(self, event_method, *args, **kwargs):
ei = sys.exc_info() ei = sys.exc_info()
print("ERRORE CRITICO:\n" + repr(ei[1]) + "\n\n" + repr(ei)) logging.error(f"Critical error: {repr(ei[1])}")
try: try:
await self.main_channel.send(f"☢️ **ERRORE CRITICO NELL'EVENTO** `{event_method}`\n" await self.main_channel.send(f"☢️ **ERRORE CRITICO NELL'EVENTO** `{event_method}`\n"
f"Il bot si è chiuso e si dovrebbe riavviare entro qualche minuto.\n" f"Il bot si è chiuso e si dovrebbe riavviare entro qualche minuto.\n"
@ -339,7 +344,7 @@ class RoyalDiscordBot(discord.Client):
await self.change_presence(status=discord.Status.invisible) await self.change_presence(status=discord.Status.invisible)
await self.close() await self.close()
except Exception as e: except Exception as e:
print("ERRORE CRITICO PIU' CRITICO:\n" + repr(e) + "\n\n" + repr(sys.exc_info())) logging.error("Double critical error: {repr(sys.exc_info())}")
loop.stop() loop.stop()
sentry.captureException(exc_info=ei) sentry.captureException(exc_info=ei)
exit(1) exit(1)
@ -719,6 +724,11 @@ class RoyalDiscordBot(discord.Client):
return return
await channel.send(f"📻 Messaggi radio **{'attivati' if self.radio_messages else 'disattivati'}**.") await channel.send(f"📻 Messaggi radio **{'attivati' if self.radio_messages else 'disattivati'}**.")
@command
@requires_connected_voice_client
async def cmd_pause(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]):
def process(users_connection=None): def process(users_connection=None):
logging.info("Initializing the bot...") logging.info("Initializing the bot...")

View file

@ -24,7 +24,8 @@ try:
except Exception: except Exception:
model = None model = None
logging.getLogger().setLevel(level=20) logging.getLogger().setLevel(level=logging.ERROR)
logging.getLogger(__name__).setLevel(level=logging.DEBUG)
# Init the config reader # Init the config reader
config = configparser.ConfigParser() config = configparser.ConfigParser()