app.py aktualisiert
This commit is contained in:
59
app.py
59
app.py
@@ -49,41 +49,58 @@ def setup_ngrok():
|
||||
|
||||
@app.route('/run-script', methods=['POST'])
|
||||
def run_script():
|
||||
"""Startet ein Skript als Hintergrundprozess und verfolgt es über eine Job-ID."""
|
||||
"""
|
||||
Startet ein Skript als Hintergrundprozess und verfolgt es über eine Job-ID.
|
||||
Verbesserte Fehlerbehandlung und Logging.
|
||||
"""
|
||||
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:
|
||||
return jsonify({"status": "error", "message": "Ungültige Aktion."}), 400
|
||||
|
||||
logging.warning(f"Ungültige oder fehlende Aktion empfangen: {action}")
|
||||
return jsonify({"status": "error", "message": "Ungültige oder fehlende Aktion."}), 400
|
||||
|
||||
script_config = SCRIPT_MAP[action]
|
||||
script_name = script_config["script"]
|
||||
script_args = script_config["args"]
|
||||
|
||||
# NEU: Job-ID generieren und als Argument übergeben
|
||||
job_id = str(uuid.uuid4())
|
||||
command = [sys.executable, script_name] + script_config["args"] + ["--job-id", job_id]
|
||||
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.info(f"Aktion '{action}' empfangen. Starte Skript: '{script_name}'...")
|
||||
|
||||
python_executable = sys.executable
|
||||
command = [python_executable, script_path] + script_args
|
||||
|
||||
# ENTSCHEIDENDER FIX: Setze das Arbeitsverzeichnis explizit
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
logging.info(f"Starte Job {job_id} für Aktion '{action}' im Verzeichnis '{script_dir}'...")
|
||||
subprocess.Popen(command, cwd=script_dir)
|
||||
# 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
|
||||
)
|
||||
|
||||
# Erstelle eine initiale Statusdatei
|
||||
status_file = os.path.join(STATUS_DIR, f"{job_id}.json")
|
||||
with open(status_file, 'w') as f:
|
||||
json.dump({
|
||||
"action": action,
|
||||
"status": "Gestartet",
|
||||
"start_time": datetime.now().isoformat(),
|
||||
"progress": "Prozess wird initialisiert..."
|
||||
}, f)
|
||||
# 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.
|
||||
|
||||
return jsonify({"status": "success", "message": f"Aktion '{action}' mit Job-ID {job_id} gestartet."}), 200
|
||||
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:
|
||||
logging.error(f"Fehler in run_script: {e}")
|
||||
return jsonify({"status": "error", "message": str(e)}), 500
|
||||
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
|
||||
|
||||
@app.route('/get-status', methods=['GET'])
|
||||
def get_status():
|
||||
|
||||
Reference in New Issue
Block a user