Deploy AIOS web (React glide grid + FastAPI slice)
Browse files- api/main.py +55 -9
api/main.py
CHANGED
|
@@ -263,22 +263,67 @@ if _WEB_DIST.is_dir():
|
|
| 263 |
# default envelope; scoped users still pay their own scope's first build, once.
|
| 264 |
# Env-gated rather than a startup event so importing `api.main` in a gate (verify_api and friends
|
| 265 |
# run against fakes) can never fire a live Odoo pull.
|
| 266 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
import time as _t
|
| 268 |
t0 = _t.time()
|
| 269 |
-
# β SEED THE ANALYTICAL STORE FIRST (2026-07-31, owner item 1). `harness.datastore` powers
|
| 270 |
-
# every measure column/condition, and ONLY app.py used to call `ensure_seed()` β so on a
|
| 271 |
-
# fresh Space disk this container resolved measures against a store that never existed:
|
| 272 |
-
# `ready()` stayed False forever, the transient rule kept retrying, and every measure cell
|
| 273 |
-
# served blank while the same code worked locally (where the file already exists).
|
| 274 |
-
# Fail-quiet like the rest of the warm-up: no seed/token β the columns stay blank and the
|
| 275 |
-
# rows still serve.
|
| 276 |
try:
|
| 277 |
from harness import datastore as _ds
|
| 278 |
if _ds.ensure_seed():
|
| 279 |
print(f"[aios-api] datastore seeded in {_t.time() - t0:.1f}s")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
except Exception as e: # noqa: BLE001
|
| 281 |
-
print(f"[aios-api] datastore seed skipped: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
try:
|
| 283 |
from harness import runtime as _runtime
|
| 284 |
rt = _runtime.get_runtime("royal-imports")
|
|
@@ -293,3 +338,4 @@ def _prewarm():
|
|
| 293 |
if os.environ.get("AIOS_PREWARM") == "1":
|
| 294 |
import threading as _threading
|
| 295 |
_threading.Thread(target=_prewarm, daemon=True, name="prewarm").start()
|
|
|
|
|
|
| 263 |
# default envelope; scoped users still pay their own scope's first build, once.
|
| 264 |
# Env-gated rather than a startup event so importing `api.main` in a gate (verify_api and friends
|
| 265 |
# run against fakes) can never fire a live Odoo pull.
|
| 266 |
+
def _seed_and_sync_store():
|
| 267 |
+
"""Bring the analytical store (harness.datastore) LIVE for this container β the app.py
|
| 268 |
+
bootstrap, mirrored (2026-07-31, owner item 1).
|
| 269 |
+
|
| 270 |
+
`harness.datastore` powers every measure column/condition, and ONLY app.py used to call
|
| 271 |
+
`ensure_seed()` β so on a fresh Space disk this container resolved measures against a store
|
| 272 |
+
that never existed and every measure cell served blank. Seeding alone was NOT enough either,
|
| 273 |
+
and that was measured live the same day: "datastore seeded in 1.7s" followed by an endless
|
| 274 |
+
`api:measure-column: ModelError the data cache is still warming up` β `ready()` demands
|
| 275 |
+
EVERY entity at phase 'live', and a seed that predates a newer entity leaves it un-synced
|
| 276 |
+
forever in a process with no sync loop. The SYNC SPRINT after the seed is what closes the
|
| 277 |
+
write_date gap and backfills anything the seed lacks (app.py:7779's exact pattern, bounded
|
| 278 |
+
passes). Fail-quiet throughout: no seed/token/Odoo β the columns stay blank, the rows still
|
| 279 |
+
serve.
|
| 280 |
+
"""
|
| 281 |
import time as _t
|
| 282 |
t0 = _t.time()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
try:
|
| 284 |
from harness import datastore as _ds
|
| 285 |
if _ds.ensure_seed():
|
| 286 |
print(f"[aios-api] datastore seeded in {_t.time() - t0:.1f}s")
|
| 287 |
+
# DEDICATED Odoo connection for this thread (the W6 postmortem rule): the sync's
|
| 288 |
+
# search_reads must never interleave on the shared client's xmlrpc transport.
|
| 289 |
+
import core.odoo as _odoo
|
| 290 |
+
try:
|
| 291 |
+
_odoo._tlocal.client = _odoo.OdooClient()
|
| 292 |
+
except Exception:
|
| 293 |
+
pass
|
| 294 |
+
for _ in range(12):
|
| 295 |
+
res = _ds.sync_all(log=lambda *a, **k: None)
|
| 296 |
+
if all(v.get("phase") == "live" for v in res.values()):
|
| 297 |
+
break
|
| 298 |
+
print(f"[aios-api] datastore sync done in {_t.time() - t0:.1f}s "
|
| 299 |
+
f"(ready={_ds.ready()})")
|
| 300 |
except Exception as e: # noqa: BLE001
|
| 301 |
+
print(f"[aios-api] datastore seed/sync skipped: {e}")
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
def _store_resync_loop():
|
| 305 |
+
"""Keep the analytical mirror current β the API-process stand-in for the re-sync the
|
| 306 |
+
Streamlit app piggybacks on page renders. Measure memos key on the pool stamp, so a
|
| 307 |
+
refreshed pool re-reads the freshly synced store."""
|
| 308 |
+
import time as _t
|
| 309 |
+
while True:
|
| 310 |
+
_t.sleep(1800)
|
| 311 |
+
try:
|
| 312 |
+
from harness import datastore as _ds
|
| 313 |
+
import core.odoo as _odoo
|
| 314 |
+
try:
|
| 315 |
+
_odoo._tlocal.client = _odoo.OdooClient()
|
| 316 |
+
except Exception:
|
| 317 |
+
pass
|
| 318 |
+
_ds.sync_all(log=lambda *a, **k: None)
|
| 319 |
+
except Exception as e: # noqa: BLE001
|
| 320 |
+
print(f"[aios-api] store resync failed: {e}")
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
def _prewarm():
|
| 324 |
+
import time as _t
|
| 325 |
+
t0 = _t.time()
|
| 326 |
+
_seed_and_sync_store()
|
| 327 |
try:
|
| 328 |
from harness import runtime as _runtime
|
| 329 |
rt = _runtime.get_runtime("royal-imports")
|
|
|
|
| 338 |
if os.environ.get("AIOS_PREWARM") == "1":
|
| 339 |
import threading as _threading
|
| 340 |
_threading.Thread(target=_prewarm, daemon=True, name="prewarm").start()
|
| 341 |
+
_threading.Thread(target=_store_resync_loop, daemon=True, name="store-resync").start()
|