stampa errori invece che far crashare app su ambiente headless
This commit is contained in:
@@ -4,6 +4,8 @@ from .config import DB_DEFAULT_DIR
|
||||
|
||||
|
||||
def _pick_db_file() -> str:
|
||||
if not os.environ.get('DISPLAY') and os.name != 'nt':
|
||||
return ''
|
||||
try:
|
||||
import tkinter as tk
|
||||
from tkinter import filedialog
|
||||
|
||||
+55
-19
@@ -167,6 +167,28 @@ def make_handler(db_path: str):
|
||||
})
|
||||
return
|
||||
|
||||
if _state.get('db_error') or not _state.get('db_path'):
|
||||
err = _e(_state.get('db_error') or 'Database non trovato.')
|
||||
self._send(200, (
|
||||
'<html><head><meta charset="utf-8">'
|
||||
'<title>RPA Dashboard</title>'
|
||||
'<style>body{font-family:sans-serif;margin:0;padding:0;background:#f5f5f5;}'
|
||||
'.content{padding:60px 40px;}'
|
||||
'.banner{position:fixed;bottom:0;left:0;right:0;background:#c0392b;color:#fff;'
|
||||
'padding:16px 24px;font-size:15px;display:flex;align-items:center;gap:12px;}'
|
||||
'.banner svg{flex-shrink:0}'
|
||||
'</style></head><body>'
|
||||
'<div class="content"><h1>RPA Dashboard</h1>'
|
||||
'<p>Il server è avviato ma non è possibile accedere ai dati.</p></div>'
|
||||
'<div class="banner">'
|
||||
'<svg width="20" height="20" viewBox="0 0 20 20" fill="white">'
|
||||
'<path d="M10 2L1 17h18L10 2zm0 3l6.5 11H3.5L10 5zm-1 4v3h2V9H9zm0 4v2h2v-2H9z"/>'
|
||||
'</svg>'
|
||||
f'<span><strong>Errore DB:</strong> {err}</span>'
|
||||
'</div></body></html>'
|
||||
))
|
||||
return
|
||||
|
||||
if self.path == '/change-db':
|
||||
chosen = _pick_db_file()
|
||||
if chosen and os.path.exists(chosen):
|
||||
@@ -314,29 +336,38 @@ def make_handler(db_path: str):
|
||||
return ReportHandler
|
||||
|
||||
|
||||
def run_server(db_path: str, host: str = '0.0.0.0', port: int = 8473):
|
||||
def run_server(db_path: str | None, host: str = '0.0.0.0', port: int = 8473, db_error: str | None = None):
|
||||
_state['db_path'] = db_path
|
||||
db_type = _refresh_db_type(db_path)
|
||||
_state['db_error'] = db_error
|
||||
if db_path:
|
||||
db_type = _refresh_db_type(db_path)
|
||||
else:
|
||||
db_type = None
|
||||
_state['db_type'] = None
|
||||
handler = make_handler(db_path)
|
||||
server = HTTPServer((host, port), handler)
|
||||
display_host = 'localhost' if host in ('0.0.0.0', '') else host
|
||||
base = f"http://{display_host}:{port}"
|
||||
print(f"RPA Report server avviato [db: {db_type}]")
|
||||
print(f" Dashboard : {base}/")
|
||||
print(f" Processi : {base}/runs")
|
||||
if db_type == DbType.INTRAZ:
|
||||
print(f" RPA Steps : {base}/steps")
|
||||
print(f" Iscr. API : {base}/iscrizioni-api")
|
||||
print(f" SharePoint : {base}/sharepoint")
|
||||
print(f" Email : {base}/email")
|
||||
if db_error:
|
||||
print(f"ATTENZIONE: {db_error}")
|
||||
print(f"RPA Report server avviato senza DB — pagina di errore su {base}/")
|
||||
else:
|
||||
print(f" PEC : {base}/pec")
|
||||
print(f" Documenti : {base}/documenti")
|
||||
print(f" Report : {base}/report")
|
||||
print(f" Schema : {base}/schema")
|
||||
print(f" Log DB : {base}/logs")
|
||||
print(f" Server log : {base}/server-logs")
|
||||
print(f" DB : {db_path}")
|
||||
print(f"RPA Report server avviato [db: {db_type}]")
|
||||
print(f" Dashboard : {base}/")
|
||||
print(f" Processi : {base}/runs")
|
||||
if db_type == DbType.INTRAZ:
|
||||
print(f" RPA Steps : {base}/steps")
|
||||
print(f" Iscr. API : {base}/iscrizioni-api")
|
||||
print(f" SharePoint : {base}/sharepoint")
|
||||
print(f" Email : {base}/email")
|
||||
else:
|
||||
print(f" PEC : {base}/pec")
|
||||
print(f" Documenti : {base}/documenti")
|
||||
print(f" Report : {base}/report")
|
||||
print(f" Schema : {base}/schema")
|
||||
print(f" Log DB : {base}/logs")
|
||||
print(f" Server log : {base}/server-logs")
|
||||
print(f" DB : {db_path}")
|
||||
print("Ctrl+C per fermare.\n")
|
||||
try:
|
||||
server.serve_forever()
|
||||
@@ -350,7 +381,12 @@ def main():
|
||||
load_dotenv()
|
||||
|
||||
db_file = os.environ.get('RPA_DB_FILE', 'rpa_FORMAZIONE.db')
|
||||
db_path = _find_db(db_file)
|
||||
db_error = None
|
||||
try:
|
||||
db_path = _find_db(db_file)
|
||||
except FileNotFoundError as exc:
|
||||
db_path = None
|
||||
db_error = str(exc)
|
||||
|
||||
port = int(os.environ.get('RPA_REPORT_PORT', 8473))
|
||||
run_server(db_path, port=port)
|
||||
run_server(db_path, port=port, db_error=db_error)
|
||||
|
||||
Reference in New Issue
Block a user