app.py aktualisiert

This commit is contained in:
2025-08-18 10:58:02 +00:00
parent 4d9a181bb8
commit f48bce9ee0

25
app.py
View File

@@ -44,6 +44,10 @@ def setup_ngrok():
@app.route('/run-script', methods=['POST']) @app.route('/run-script', methods=['POST'])
def run_script(): def run_script():
"""
Startet ein Skript als Hintergrundprozess.
Finale, robuste Version mit absoluten Pfaden.
"""
action = "unknown" action = "unknown"
try: try:
data = request.get_json() data = request.get_json()
@@ -55,17 +59,22 @@ def run_script():
script_config = SCRIPT_MAP[action] script_config = SCRIPT_MAP[action]
script_name = script_config["script"] script_name = script_config["script"]
script_path = os.path.join(APP_DIR, script_name) # Korrekter Pfad im Container # ENTSCHEIDENDER FIX: Finde den Pfad relativ zu DIESER app.py Datei
# __file__ ist der Pfad zur aktuell laufenden Datei (app.py)
# os.path.dirname(__file__) ist der Ordner, in dem app.py liegt (/app im Container)
script_dir = os.path.dirname(os.path.abspath(__file__))
script_path = os.path.join(script_dir, script_name)
if not os.path.exists(script_path): if not os.path.exists(script_path):
return jsonify({"status": "error", "message": f"Server-Fehler: Skript {script_name} nicht gefunden."}), 500 logging.error(f"Skript unter dem Pfad '{script_path}' NICHT GEFUNDEN.")
return jsonify({"status": "error", "message": f"Server-Fehler: Skript '{script_name}' nicht gefunden."}), 500
logging.info(f"Aktion '{action}' empfangen. Starte Skript: '{script_name}'...") logging.info(f"Aktion '{action}' empfangen. Starte Skript: '{script_path}'...")
command = ["python", script_name] + script_config["args"] python_executable = sys.executable
command = [python_executable, script_path] + script_config["args"]
# Log-Datei für den Subprozess log_dir = os.path.join(script_dir, "Log")
log_dir = os.path.join(APP_DIR, "Log")
os.makedirs(log_dir, exist_ok=True) os.makedirs(log_dir, exist_ok=True)
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
process_log_path = os.path.join(log_dir, f"{timestamp}_{action}.log") process_log_path = os.path.join(log_dir, f"{timestamp}_{action}.log")
@@ -73,7 +82,7 @@ def run_script():
with open(process_log_path, 'w') as log_file: with open(process_log_path, 'w') as log_file:
subprocess.Popen( subprocess.Popen(
command, command,
cwd=APP_DIR, # Setze das Arbeitsverzeichnis explizit # Wir müssen das Arbeitsverzeichnis nicht mehr setzen, da wir absolute Pfade verwenden
stdout=log_file, stdout=log_file,
stderr=log_file stderr=log_file
) )
@@ -81,7 +90,7 @@ def run_script():
return jsonify({"status": "success", "message": f"Aktion '{action}' gestartet. Log wird in '{process_log_path}' geschrieben."}), 200 return jsonify({"status": "success", "message": f"Aktion '{action}' gestartet. Log wird in '{process_log_path}' geschrieben."}), 200
except Exception as e: except Exception as e:
logging.error(f"Kritischer Fehler in run_script bei Aktion '{action}': {e}", exc_info=True) logging.error(f"Kritischer Fehler in der run_script-Route bei Aktion '{action}': {e}", exc_info=True)
return jsonify({"status": "error", "message": f"Server-Fehler: {str(e)}"}), 500 return jsonify({"status": "error", "message": f"Server-Fehler: {str(e)}"}), 500
@app.route('/get-status', methods=['GET']) @app.route('/get-status', methods=['GET'])