Spaces:
Running
Running
Guilherme Silberfarb Costa commited on
Commit ·
b75b945
1
Parent(s): 4678b81
Warm portable caches after startup
Browse files
backend/app/portable_launcher.py
CHANGED
|
@@ -53,6 +53,16 @@ def _wait_for_server_and_open_browser(url: str, timeout_s: float = 45.0) -> None
|
|
| 53 |
time.sleep(0.5)
|
| 54 |
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
def _build_server(settings: RuntimeSettings, log_path: Path | None) -> tuple[uvicorn.Server, str]:
|
| 57 |
port = _first_available_port(settings.host, settings.port)
|
| 58 |
url = f"http://{settings.host}:{port}"
|
|
@@ -106,6 +116,9 @@ def main() -> int:
|
|
| 106 |
opener = threading.Thread(target=_wait_for_server_and_open_browser, args=(url,), daemon=True)
|
| 107 |
opener.start()
|
| 108 |
|
|
|
|
|
|
|
|
|
|
| 109 |
try:
|
| 110 |
server.run()
|
| 111 |
except KeyboardInterrupt:
|
|
|
|
| 53 |
time.sleep(0.5)
|
| 54 |
|
| 55 |
|
| 56 |
+
def _warmup_backend_runtime() -> None:
|
| 57 |
+
try:
|
| 58 |
+
time.sleep(1.0)
|
| 59 |
+
from app.portable_warmup import warmup_portable_runtime
|
| 60 |
+
|
| 61 |
+
warmup_portable_runtime()
|
| 62 |
+
except Exception:
|
| 63 |
+
pass
|
| 64 |
+
|
| 65 |
+
|
| 66 |
def _build_server(settings: RuntimeSettings, log_path: Path | None) -> tuple[uvicorn.Server, str]:
|
| 67 |
port = _first_available_port(settings.host, settings.port)
|
| 68 |
url = f"http://{settings.host}:{port}"
|
|
|
|
| 116 |
opener = threading.Thread(target=_wait_for_server_and_open_browser, args=(url,), daemon=True)
|
| 117 |
opener.start()
|
| 118 |
|
| 119 |
+
warmup = threading.Thread(target=_warmup_backend_runtime, daemon=True, name="mesa-portable-warmup")
|
| 120 |
+
warmup.start()
|
| 121 |
+
|
| 122 |
try:
|
| 123 |
server.run()
|
| 124 |
except KeyboardInterrupt:
|
backend/app/portable_warmup.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
import folium
|
| 6 |
+
|
| 7 |
+
from app.portable_runtime_log import append_runtime_log
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _log_step(label: str, start: float, extra: str = "") -> None:
|
| 11 |
+
elapsed = time.perf_counter() - start
|
| 12 |
+
suffix = f" ({extra})" if extra else ""
|
| 13 |
+
append_runtime_log(f"[mesa] warmup: {label} em {elapsed:.2f}s{suffix}")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def warmup_portable_runtime() -> None:
|
| 17 |
+
started = time.perf_counter()
|
| 18 |
+
append_runtime_log("[mesa] warmup: iniciado em segundo plano")
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
from app.core.map_layers import _carregar_bairros_geojson, add_bairros_layer
|
| 22 |
+
|
| 23 |
+
step = time.perf_counter()
|
| 24 |
+
geojson = _carregar_bairros_geojson()
|
| 25 |
+
total_features = 0
|
| 26 |
+
if isinstance(geojson, dict):
|
| 27 |
+
total_features = len(geojson.get("features") or [])
|
| 28 |
+
_log_step("camada de bairros preparada", step, f"{total_features} features")
|
| 29 |
+
|
| 30 |
+
step = time.perf_counter()
|
| 31 |
+
mapa = folium.Map(
|
| 32 |
+
location=[-30.03, -51.23],
|
| 33 |
+
zoom_start=12,
|
| 34 |
+
tiles="OpenStreetMap",
|
| 35 |
+
prefer_canvas=True,
|
| 36 |
+
control_scale=True,
|
| 37 |
+
)
|
| 38 |
+
add_bairros_layer(mapa, show=False)
|
| 39 |
+
_ = mapa.get_root().render()
|
| 40 |
+
_log_step("renderizacao base do mapa preparada", step)
|
| 41 |
+
except Exception as exc:
|
| 42 |
+
append_runtime_log(f"[mesa] warmup: falha ao preparar mapa base: {exc}")
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
from app.services import trabalhos_tecnicos_service
|
| 46 |
+
|
| 47 |
+
step = time.perf_counter()
|
| 48 |
+
catalogo_modelos, trabalhos_por_chave = trabalhos_tecnicos_service._montar_cache_trabalhos_por_modelo()
|
| 49 |
+
_log_step(
|
| 50 |
+
"cache de trabalhos tecnicos preparado",
|
| 51 |
+
step,
|
| 52 |
+
f"{len(catalogo_modelos)} modelos / {len(trabalhos_por_chave)} chaves",
|
| 53 |
+
)
|
| 54 |
+
except Exception as exc:
|
| 55 |
+
append_runtime_log(f"[mesa] warmup: falha ao preparar cache de trabalhos tecnicos: {exc}")
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
from app.services import pesquisa_service
|
| 59 |
+
|
| 60 |
+
step = time.perf_counter()
|
| 61 |
+
catalogo_vias = pesquisa_service._carregar_catalogo_vias()
|
| 62 |
+
_log_step("catalogo de logradouros preparado", step, f"{len(catalogo_vias)} vias")
|
| 63 |
+
except Exception as exc:
|
| 64 |
+
append_runtime_log(f"[mesa] warmup: falha ao preparar catalogo de logradouros: {exc}")
|
| 65 |
+
|
| 66 |
+
total = time.perf_counter() - started
|
| 67 |
+
append_runtime_log(f"[mesa] warmup: concluido em {total:.2f}s")
|