app.py aktualisiert
This commit is contained in:
43
app.py
43
app.py
@@ -50,53 +50,48 @@ 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 und verfolgt es über eine Job-ID.
|
Startet ein Skript als Hintergrundprozess.
|
||||||
Verbesserte Fehlerbehandlung und Logging.
|
NEU: Leitet die Ausgabe des Skripts in eine dedizierte Log-Datei um.
|
||||||
"""
|
"""
|
||||||
action = "unknown"
|
action = "unknown"
|
||||||
try:
|
try:
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
if not data:
|
|
||||||
raise ValueError("Keine JSON-Daten im Request gefunden.")
|
|
||||||
|
|
||||||
action = data.get('action')
|
action = data.get('action')
|
||||||
|
|
||||||
if not action or action not in SCRIPT_MAP:
|
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 Aktion."}), 400
|
||||||
return jsonify({"status": "error", "message": "Ungültige oder fehlende Aktion."}), 400
|
|
||||||
|
|
||||||
script_config = SCRIPT_MAP[action]
|
script_config = SCRIPT_MAP[action]
|
||||||
script_name = script_config["script"]
|
script_name = script_config["script"]
|
||||||
script_args = script_config["args"]
|
|
||||||
|
|
||||||
script_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), script_name)
|
script_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), script_name)
|
||||||
if not os.path.exists(script_path):
|
if not os.path.exists(script_path):
|
||||||
logging.error(f"Skript '{script_path}' für Aktion '{action}' nicht gefunden.")
|
logging.error(f"Skript '{script_path}' nicht gefunden.")
|
||||||
return jsonify({"status": "error", "message": f"Server-Fehler: Skript '{script_name}' nicht gefunden."}), 500
|
return jsonify({"status": "error", "message": f"Server-Fehler: Skript nicht gefunden."}), 500
|
||||||
|
|
||||||
logging.info(f"Aktion '{action}' empfangen. Starte Skript: '{script_name}'...")
|
logging.info(f"Aktion '{action}' empfangen. Starte Skript: '{script_name}'...")
|
||||||
|
|
||||||
python_executable = sys.executable
|
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__))
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
# Führe den Prozess aus und fange stdout/stderr für Debugging ab
|
# NEU: Dedizierte Log-Datei für den Subprozess erstellen
|
||||||
process = subprocess.Popen(
|
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")
|
||||||
|
|
||||||
|
logging.info(f"Die Ausgabe des Skripts wird in '{process_log_path}' protokolliert.")
|
||||||
|
|
||||||
|
with open(process_log_path, 'w') as log_file:
|
||||||
|
subprocess.Popen(
|
||||||
command,
|
command,
|
||||||
cwd=script_dir,
|
cwd=script_dir,
|
||||||
stdout=subprocess.PIPE,
|
stdout=log_file,
|
||||||
stderr=subprocess.PIPE,
|
stderr=log_file
|
||||||
text=True
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Wir warten nicht, aber wir können kurz prüfen, ob es sofort einen Fehler gab
|
return jsonify({"status": "success", "message": f"Aktion '{action}' wurde erfolgreich gestartet. Details siehe Log-Datei."}), 200
|
||||||
# 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"Prozess für Aktion '{action}' erfolgreich mit PID {process.pid} gestartet.")
|
|
||||||
|
|
||||||
return jsonify({"status": "success", "message": f"Aktion '{action}' wurde erfolgreich auf dem Server gestartet."}), 200
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Kritischer Fehler in der run_script-Route bei Aktion '{action}': {e}", exc_info=True)
|
logging.error(f"Kritischer Fehler in der run_script-Route bei Aktion '{action}': {e}", exc_info=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user