rerun client on exception (and log)

This commit is contained in:
klemek
2019-11-26 13:30:18 +01:00
parent 443ab035df
commit 20006345df
2 changed files with 23 additions and 4 deletions
+2 -1
View File
@@ -1,4 +1,5 @@
.idea
venv
__pycache__
.env
.env
error_*
+21 -3
View File
@@ -1,4 +1,5 @@
import os
import traceback
import discord
from datetime import datetime
from dotenv import load_dotenv
@@ -8,7 +9,7 @@ import emotes
import help
from utils import debug
VERSION = "1.1"
VERSION = "1.2"
t0 = datetime.now()
# Loading token
@@ -87,5 +88,22 @@ async def on_message(message):
await COMMANDS[args[0]](message, args)
# Launch client
client.run(token)
print(f"Current PID: {os.getpid()}")
# Launch client and rerun on errors
while True:
try:
client.run(token)
break # clean kill
except Exception as e:
t = datetime.now()
print(f"Exception raised at {t:%Y-%m-%d %H:%M} : {repr(e)}")
fileName = f"error_{t:%Y-%m-%d_%H-%M-%S}.txt"
if os.path.exists(fileName):
print("Two many errors, killing")
break
with open(fileName, 'w') as f:
f.write(f"Discord Analyst v{VERSION} started at {t0:%Y-%m-%d %H:%M}\r\n"
f"Exception raised at {t:%Y-%m-%d %H:%M}\r\n"
f"\r\n"
f"{traceback.format_exc()}")