app.py aktualisiert
This commit is contained in:
45
app.py
45
app.py
@@ -50,53 +50,48 @@ def setup_ngrok():
|
||||
@app.route('/run-script', methods=['POST'])
|
||||
def run_script():
|
||||
"""
|
||||
Startet ein Skript als Hintergrundprozess und verfolgt es über eine Job-ID.
|
||||
Verbesserte Fehlerbehandlung und Logging.
|
||||
Startet ein Skript als Hintergrundprozess.
|
||||
NEU: Leitet die Ausgabe des Skripts in eine dedizierte Log-Datei um.
|
||||
"""
|
||||
action = "unknown"
|
||||
try:
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
raise ValueError("Keine JSON-Daten im Request gefunden.")
|
||||
|
||||
action = data.get('action')
|
||||
|
||||
if not action or action not in SCRIPT_MAP:
|
||||
logging.warning(f"Ungültige oder fehlende Aktion empfangen: {action}")
|
||||
return jsonify({"status": "error", "message": "Ungültige oder fehlende Aktion."}), 400
|
||||
return jsonify({"status": "error", "message": "Ungültige Aktion."}), 400
|
||||
|
||||
script_config = SCRIPT_MAP[action]
|
||||
script_name = script_config["script"]
|
||||
script_args = script_config["args"]
|
||||
|
||||
script_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), script_name)
|
||||
if not os.path.exists(script_path):
|
||||
logging.error(f"Skript '{script_path}' für Aktion '{action}' nicht gefunden.")
|
||||
return jsonify({"status": "error", "message": f"Server-Fehler: Skript '{script_name}' nicht gefunden."}), 500
|
||||
logging.error(f"Skript '{script_path}' nicht gefunden.")
|
||||
return jsonify({"status": "error", "message": f"Server-Fehler: Skript nicht gefunden."}), 500
|
||||
|
||||
logging.info(f"Aktion '{action}' empfangen. Starte Skript: '{script_name}'...")
|
||||
|
||||
python_executable = sys.executable
|
||||
command = [python_executable, script_path] + script_args
|
||||
|
||||
command = [python_executable, script_path] + script_config["args"]
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
# Führe den Prozess aus und fange stdout/stderr für Debugging ab
|
||||
process = subprocess.Popen(
|
||||
command,
|
||||
cwd=script_dir,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True
|
||||
)
|
||||
# NEU: Dedizierte Log-Datei für den Subprozess erstellen
|
||||
log_dir = os.path.join(script_dir, "Log")
|
||||
os.makedirs(log_dir, exist_ok=True)
|
||||
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||
process_log_path = os.path.join(log_dir, f"{timestamp}_{action}.log")
|
||||
|
||||
# Wir warten nicht, aber wir können kurz prüfen, ob es sofort einen Fehler gab
|
||||
# Dies ist eine Vereinfachung. Für eine vollwertige Lösung bräuchten wir einen Job-Queue.
|
||||
# Aber für jetzt sollte das den Fehler aufdecken.
|
||||
logging.info(f"Die Ausgabe des Skripts wird in '{process_log_path}' protokolliert.")
|
||||
|
||||
logging.info(f"Prozess für Aktion '{action}' erfolgreich mit PID {process.pid} gestartet.")
|
||||
with open(process_log_path, 'w') as log_file:
|
||||
subprocess.Popen(
|
||||
command,
|
||||
cwd=script_dir,
|
||||
stdout=log_file,
|
||||
stderr=log_file
|
||||
)
|
||||
|
||||
return jsonify({"status": "success", "message": f"Aktion '{action}' wurde erfolgreich auf dem Server gestartet."}), 200
|
||||
return jsonify({"status": "success", "message": f"Aktion '{action}' wurde erfolgreich gestartet. Details siehe Log-Datei."}), 200
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Kritischer Fehler in der run_script-Route bei Aktion '{action}': {e}", exc_info=True)
|
||||
|
||||
Reference in New Issue
Block a user