Spaces:
Running
Running
Guilherme Silberfarb Costa commited on
Commit ·
2ffc64b
1
Parent(s): 82e25a7
Improve portable startup diagnostics
Browse files
backend/app/portable_launcher.py
CHANGED
|
@@ -3,12 +3,27 @@ from __future__ import annotations
|
|
| 3 |
import socket
|
| 4 |
import threading
|
| 5 |
import time
|
|
|
|
| 6 |
import urllib.request
|
| 7 |
import webbrowser
|
|
|
|
| 8 |
|
| 9 |
import uvicorn
|
| 10 |
|
| 11 |
-
from app.runtime_config import RuntimeSettings, apply_runtime_config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
def _first_available_port(host: str, preferred_port: int, max_attempts: int = 20) -> int:
|
|
@@ -37,11 +52,13 @@ def _wait_for_server_and_open_browser(url: str, timeout_s: float = 45.0) -> None
|
|
| 37 |
time.sleep(0.5)
|
| 38 |
|
| 39 |
|
| 40 |
-
def _build_server(settings: RuntimeSettings) -> tuple[uvicorn.Server, str]:
|
| 41 |
port = _first_available_port(settings.host, settings.port)
|
| 42 |
url = f"http://{settings.host}:{port}"
|
| 43 |
|
|
|
|
| 44 |
from app.main import app
|
|
|
|
| 45 |
|
| 46 |
config = uvicorn.Config(
|
| 47 |
app=app,
|
|
@@ -54,13 +71,34 @@ def _build_server(settings: RuntimeSettings) -> tuple[uvicorn.Server, str]:
|
|
| 54 |
|
| 55 |
|
| 56 |
def main() -> int:
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
if settings.open_browser:
|
| 66 |
opener = threading.Thread(target=_wait_for_server_and_open_browser, args=(url,), daemon=True)
|
|
@@ -69,7 +107,11 @@ def main() -> int:
|
|
| 69 |
try:
|
| 70 |
server.run()
|
| 71 |
except KeyboardInterrupt:
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
return 0
|
| 74 |
|
| 75 |
|
|
|
|
| 3 |
import socket
|
| 4 |
import threading
|
| 5 |
import time
|
| 6 |
+
import traceback
|
| 7 |
import urllib.request
|
| 8 |
import webbrowser
|
| 9 |
+
from pathlib import Path
|
| 10 |
|
| 11 |
import uvicorn
|
| 12 |
|
| 13 |
+
from app.runtime_config import RuntimeSettings, apply_runtime_config, resolve_app_root
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def _append_startup_log(message: str, log_path: Path | None) -> None:
|
| 17 |
+
text = str(message).rstrip()
|
| 18 |
+
print(text, flush=True)
|
| 19 |
+
if log_path is None:
|
| 20 |
+
return
|
| 21 |
+
try:
|
| 22 |
+
log_path.parent.mkdir(parents=True, exist_ok=True)
|
| 23 |
+
with log_path.open("a", encoding="utf-8") as handle:
|
| 24 |
+
handle.write(f"{text}\n")
|
| 25 |
+
except Exception:
|
| 26 |
+
pass
|
| 27 |
|
| 28 |
|
| 29 |
def _first_available_port(host: str, preferred_port: int, max_attempts: int = 20) -> int:
|
|
|
|
| 52 |
time.sleep(0.5)
|
| 53 |
|
| 54 |
|
| 55 |
+
def _build_server(settings: RuntimeSettings, log_path: Path | None) -> tuple[uvicorn.Server, str]:
|
| 56 |
port = _first_available_port(settings.host, settings.port)
|
| 57 |
url = f"http://{settings.host}:{port}"
|
| 58 |
|
| 59 |
+
_append_startup_log("[mesa] importando backend...", log_path)
|
| 60 |
from app.main import app
|
| 61 |
+
_append_startup_log("[mesa] backend importado com sucesso", log_path)
|
| 62 |
|
| 63 |
config = uvicorn.Config(
|
| 64 |
app=app,
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
def main() -> int:
|
| 74 |
+
early_log_path = resolve_app_root() / "MesaFrame-startup.log"
|
| 75 |
+
try:
|
| 76 |
+
early_log_path.unlink(missing_ok=True)
|
| 77 |
+
except Exception:
|
| 78 |
+
pass
|
| 79 |
|
| 80 |
+
_append_startup_log("[mesa] iniciando aplicativo...", early_log_path)
|
| 81 |
+
|
| 82 |
+
try:
|
| 83 |
+
settings = apply_runtime_config()
|
| 84 |
+
log_path = settings.app_root / "MesaFrame-startup.log"
|
| 85 |
+
if log_path != early_log_path:
|
| 86 |
+
try:
|
| 87 |
+
early_log_text = early_log_path.read_text(encoding="utf-8")
|
| 88 |
+
log_path.write_text(early_log_text, encoding="utf-8")
|
| 89 |
+
except Exception:
|
| 90 |
+
pass
|
| 91 |
+
_append_startup_log(f"[mesa] pasta do app: {settings.app_root}", log_path)
|
| 92 |
+
if settings.config_path is not None:
|
| 93 |
+
_append_startup_log(f"[mesa] configuracao carregada de {settings.config_path}", log_path)
|
| 94 |
+
_append_startup_log(f"[mesa] runtime local em {settings.runtime_dir}", log_path)
|
| 95 |
+
server, url = _build_server(settings, log_path)
|
| 96 |
+
_append_startup_log(f"[mesa] iniciando servidor local em {url}", log_path)
|
| 97 |
+
except Exception:
|
| 98 |
+
detail = traceback.format_exc()
|
| 99 |
+
_append_startup_log("[mesa] falha ao iniciar o aplicativo", early_log_path)
|
| 100 |
+
_append_startup_log(detail, early_log_path)
|
| 101 |
+
return 1
|
| 102 |
|
| 103 |
if settings.open_browser:
|
| 104 |
opener = threading.Thread(target=_wait_for_server_and_open_browser, args=(url,), daemon=True)
|
|
|
|
| 107 |
try:
|
| 108 |
server.run()
|
| 109 |
except KeyboardInterrupt:
|
| 110 |
+
_append_startup_log("[mesa] encerrando aplicativo", log_path)
|
| 111 |
+
except Exception:
|
| 112 |
+
_append_startup_log("[mesa] falha durante a execucao do servidor", log_path)
|
| 113 |
+
_append_startup_log(traceback.format_exc(), log_path)
|
| 114 |
+
return 1
|
| 115 |
return 0
|
| 116 |
|
| 117 |
|
build/windows/appsettings.example.json
CHANGED
|
@@ -5,10 +5,9 @@
|
|
| 5 |
"open_browser": true
|
| 6 |
},
|
| 7 |
"paths": {
|
| 8 |
-
"
|
| 9 |
-
"
|
| 10 |
-
"
|
| 11 |
-
"users_file": "\\\\servidor\\mesa\\dados\\usuarios\\usuarios.json"
|
| 12 |
},
|
| 13 |
"env": {
|
| 14 |
"APP_LOGS_MODE": "disabled"
|
|
|
|
| 5 |
"open_browser": true
|
| 6 |
},
|
| 7 |
"paths": {
|
| 8 |
+
"models_dir": "..\\dados\\modelos_dai",
|
| 9 |
+
"trabalhos_tecnicos_db": "..\\dados\\trabalhos_tecnicos\\trabalhos_tecnicos.sqlite3",
|
| 10 |
+
"users_file": "..\\dados\\usuarios\\usuarios.json"
|
|
|
|
| 11 |
},
|
| 12 |
"env": {
|
| 13 |
"APP_LOGS_MODE": "disabled"
|