app.py aktualisiert
This commit is contained in:
47
app.py
47
app.py
@@ -1,29 +1,24 @@
|
|||||||
# app.py (v2.0 - Autark & Transparent)
|
# app.py (v2.1 - Final für Docker)
|
||||||
|
from flask import Flask, jsonify, request
|
||||||
from flask import Flask, jsonify, request, render_template_string
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
import uuid
|
|
||||||
import json
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pyngrok import ngrok, conf
|
from pyngrok import ngrok, conf
|
||||||
from config import Config
|
from config import Config
|
||||||
|
|
||||||
# --- Konfiguration ---
|
|
||||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
# Verzeichnis für Statusdateien der laufenden Jobs
|
# Der Pfad, in dem sich der Code im Container befindet
|
||||||
STATUS_DIR = "job_status"
|
# (muss mit WORKDIR im Dockerfile übereinstimmen)
|
||||||
os.makedirs(STATUS_DIR, exist_ok=True)
|
APP_DIR = "/code"
|
||||||
|
|
||||||
# SCRIPT MAP (Passen Sie hier bei Bedarf die Skriptnamen an, z.B. auf brancheneinstufung2.py)
|
|
||||||
SCRIPT_MAP = {
|
SCRIPT_MAP = {
|
||||||
"run_duplicate_check": {"script": "duplicate_checker.py", "args": []},
|
"run_duplicate_check": {"script": "duplicate_checker.py", "args": []},
|
||||||
"run_reclassify_branches": {"script": "brancheneinstufung.py", "args": ["--mode", "reclassify_branches"]},
|
"run_reclassify_branches": {"script": "brancheneinstufung2.py", "args": ["--mode", "reclassify_branches"]},
|
||||||
"run_predict_technicians": {"script": "brancheneinstufung.py", "args": ["--mode", "predict_technicians"]},
|
# ... fügen Sie hier bei Bedarf brancheneinstufung2.py hinzu
|
||||||
}
|
}
|
||||||
|
|
||||||
def setup_ngrok():
|
def setup_ngrok():
|
||||||
@@ -49,10 +44,6 @@ 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.
|
|
||||||
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()
|
||||||
@@ -64,37 +55,33 @@ 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(os.path.dirname(os.path.abspath(__file__)), script_name)
|
script_path = os.path.join(APP_DIR, script_name) # Korrekter Pfad im Container
|
||||||
|
|
||||||
if not os.path.exists(script_path):
|
if not os.path.exists(script_path):
|
||||||
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
|
command = ["python", script_name] + script_config["args"]
|
||||||
command = [python_executable, script_path] + script_config["args"]
|
|
||||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
|
||||||
|
|
||||||
# NEU: Dedizierte Log-Datei für den Subprozess erstellen
|
# 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")
|
||||||
|
|
||||||
logging.info(f"Die Ausgabe des Skripts wird in '{process_log_path}' protokolliert.")
|
|
||||||
|
|
||||||
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=script_dir,
|
cwd=APP_DIR, # Setze das Arbeitsverzeichnis explizit
|
||||||
stdout=log_file,
|
stdout=log_file,
|
||||||
stderr=log_file
|
stderr=log_file
|
||||||
)
|
)
|
||||||
|
|
||||||
return jsonify({"status": "success", "message": f"Aktion '{action}' wurde erfolgreich gestartet. Details siehe Log-Datei."}), 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 der run_script-Route bei Aktion '{action}': {e}", exc_info=True)
|
logging.error(f"Kritischer Fehler in run_script 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'])
|
||||||
|
|||||||
Reference in New Issue
Block a user