modified: app.py

This commit is contained in:
SimolZimol
2024-09-06 16:50:00 +02:00
parent 6170ad8cd7
commit 144377c0db

28
app.py
View File

@@ -38,15 +38,14 @@ ASKNOTES_INTRO_FILE = "asknotesintro.txt"
bot_process = None bot_process = None
def bot_status(): def bot_status():
"""Überprüft, ob der Bot läuft, indem der Prozess nach dem Skript-Namen durchsucht wird.""" """Überprüft, ob der Bot läuft."""
for proc in psutil.process_iter(['pid', 'name', 'cmdline']): global bot_process
# Überprüfen, ob 'bot.py' im Befehlszeilen-Argument des Prozesses vorhanden ist if bot_process is None:
if 'python' in proc.info['name'] and 'bot.py' in proc.info['cmdline']: return False
return True return bot_process.poll() is None # None bedeutet, dass der Prozess noch läuft
return False
def start_bot(): def start_bot():
"""Startet den Bot, falls er nicht bereits läuft.""" """Startet den Bot."""
global bot_process global bot_process
if not bot_status(): if not bot_status():
bot_process = subprocess.Popen(["python", "bot.py"], cwd=os.path.dirname(os.path.abspath(__file__))) bot_process = subprocess.Popen(["python", "bot.py"], cwd=os.path.dirname(os.path.abspath(__file__)))
@@ -56,13 +55,10 @@ def start_bot():
def stop_bot(): def stop_bot():
"""Stoppt den Bot.""" """Stoppt den Bot."""
global bot_process global bot_process
if bot_status(): if bot_process and bot_status():
for proc in psutil.process_iter(['pid', 'name', 'cmdline']): bot_process.terminate()
if 'python' in proc.info['name'] and 'bot.py' in proc.info['cmdline']: bot_process.wait() # Warten, bis der Prozess beendet ist
proc.terminate() bot_process = None
proc.wait()
print("Bot wurde gestoppt.")
bot_process = None
else: else:
print("Bot läuft nicht.") print("Bot läuft nicht.")
@@ -203,7 +199,7 @@ def admin_dashboard():
connection.close() connection.close()
if user_data and user_data["permission"] >= 8: if user_data and user_data["permission"] >= 8:
return render_template("admin_dashboard.html", user_info=user_info) return render_template("admin_dashboard.html", user_info=user_info, bot_running=bot_status())
else: else:
return redirect(url_for("user_dashboard")) return redirect(url_for("user_dashboard"))
return redirect(url_for("landing_page")) return redirect(url_for("landing_page"))
@@ -239,7 +235,7 @@ def logout():
@app.route("/start_bot") @app.route("/start_bot")
def start(): def start():
if is_admin(): if is_admin():
start_bot() start_bot()
user_info = session["discord_user"] user_info = session["discord_user"]
return render_template("admin_dashboard.html", user_info=user_info) return render_template("admin_dashboard.html", user_info=user_info)
return redirect(url_for("landing_page")) return redirect(url_for("landing_page"))