app.py aktualisiert
This commit is contained in:
77
app.py
77
app.py
@@ -5,49 +5,84 @@ import sys
|
||||
import os
|
||||
import logging
|
||||
|
||||
# --- Konfiguration ---
|
||||
# Logging einrichten, um Anfragen und Fehler im Terminal zu sehen
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
|
||||
# --- Flask App Initialisierung ---
|
||||
app = Flask(__name__)
|
||||
|
||||
# --- SCRIPT MAP ---
|
||||
# Dies ist die zentrale "Telefonzentrale".
|
||||
# Hier definieren wir, welcher 'action'-Befehl vom Google Sheet
|
||||
# welches lokale Skript mit welchen Argumenten startet.
|
||||
SCRIPT_MAP = {
|
||||
"run_duplicate_check": {
|
||||
"script": "duplicate_checker.py",
|
||||
"args": []
|
||||
},
|
||||
"run_reclassify_branches": {
|
||||
"script": "brancheneinstufung.py", # oder brancheneinstufung2.py
|
||||
"script": "brancheneinstufung.py", # Passen Sie ggf. auf 'brancheneinstufung2.py' an
|
||||
"args": ["--mode", "reclassify_branches"]
|
||||
},
|
||||
"run_predict_technicians": {
|
||||
"script": "brancheneinstufung.py", # oder brancheneinstufung2.py
|
||||
"script": "brancheneinstufung.py", # Passen Sie ggf. auf 'brancheneinstufung2.py' an
|
||||
"args": ["--mode", "predict_technicians"]
|
||||
},
|
||||
"run_full_enrichment": {
|
||||
"script": "brancheneinstufung.py", # Passen Sie ggf. auf 'brancheneinstufung2.py' an
|
||||
"args": ["--mode", "full_run"]
|
||||
}
|
||||
# Hier können in Zukunft einfach weitere Aktionen hinzugefügt werden,
|
||||
# z.B. für die Generierung von Marketing-Texten.
|
||||
}
|
||||
|
||||
@app.route('/run-script', methods=['POST']) # <-- Der korrekte Endpunkt
|
||||
def run_script():
|
||||
data = request.get_json()
|
||||
action = data.get('action')
|
||||
|
||||
if not action or action not in SCRIPT_MAP:
|
||||
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"]
|
||||
|
||||
if not os.path.exists(script_name):
|
||||
logging.error(f"Skript '{script_name}' für Aktion '{action}' nicht gefunden.")
|
||||
return jsonify({"status": "error", "message": f"Server-Fehler: Skript '{script_name}' nicht gefunden."}), 500
|
||||
|
||||
@app.route('/run-script', methods=['POST'])
|
||||
def run_script():
|
||||
"""
|
||||
Ein universeller Endpunkt, der verschiedene Skripte basierend
|
||||
auf einer 'action'-ID im Request starten kann.
|
||||
"""
|
||||
try:
|
||||
logging.info(f"Starte Aktion '{action}' -> Skript: '{script_name}' mit Argumenten: {script_args}")
|
||||
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
|
||||
|
||||
script_config = SCRIPT_MAP[action]
|
||||
script_name = script_config["script"]
|
||||
script_args = script_config["args"]
|
||||
|
||||
# Sicherheitsprüfung: Existiert das Skript?
|
||||
if not os.path.exists(script_name):
|
||||
logging.error(f"Skript '{script_name}' 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}' mit Argumenten: {script_args}")
|
||||
|
||||
# Stellt sicher, dass der gleiche Python-Interpreter verwendet wird
|
||||
python_executable = sys.executable
|
||||
|
||||
command = [python_executable, script_name] + script_args
|
||||
|
||||
# Popen startet den Prozess im Hintergrund und kehrt sofort zurück.
|
||||
# Das verhindert, dass Google Sheets auf das Ende des Skripts wartet und einen Timeout bekommt.
|
||||
subprocess.Popen(command)
|
||||
return jsonify({"status": "success", "message": f"Aktion '{action}' wurde erfolgreich gestartet."}), 200
|
||||
|
||||
return jsonify({"status": "success", "message": f"Aktion '{action}' wurde erfolgreich auf dem Server gestartet."}), 200
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Fehler beim Starten des Skripts für Aktion '{action}': {e}")
|
||||
return jsonify({"status": "error", "message": str(e)}), 500
|
||||
logging.error(f"Ein Fehler ist in der run_script-Route aufgetreten: {e}")
|
||||
return jsonify({"status": "error", "message": f"Server-Fehler: {str(e)}"}), 500
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Startet den Server, erreichbar im lokalen Netzwerk auf Port 8080
|
||||
# um Konflikte mit Synology DSM zu vermeiden.
|
||||
app.run(host='0.0.0.0', port=8080)
|
||||
Reference in New Issue
Block a user