feat(backend): module registry, manifests, enable/disable without restart

Platform v2 step 1.3 (APPS_PLATFORM §4, §5.3, D-4, D-19).
- core/modules: Module/Perm/Limit/SettingDef/DeviceApp/Mount model; registry
  that discovers modules/<key>/manifest.py and validates it (keys, kinds,
  dependencies and cycles, permission prefixes and roles, unique settings,
  each router claimed once and importable, device_app only on apps, workers);
  free_tier.py (D-5 constant, used from 1.4)
- 25 manifests (the §4.2 map) with permission keys + default roles derived
  from today's perm_* meaning, limits, device apps and the 55 settings
- main.py mounts every router from the manifests; non-core routers carry
  Depends(module_required(key)) -> 403 module.disabled while off
- site_modules table (revision 0004), cached enablement; disabling a module
  also turns off modules that depend on it; core can't be disabled
- worker supervisor: connect_menu / connect_orders / connect_remote are now
  separate workers (v1's single _connect_loop split), started and stopped as
  their modules turn on and off; crashes are logged and restarted
- GET /api/platform/bootstrap (venue name/tz/language/currency, effective
  modules, user, language, role-template permission keys), GET /modules,
  PUT /modules/{key}
- settings API derives its key list and defaults from the manifests
- reports router split: history endpoints on history_router (`history`
  module, free tier later) under the same /api/reports prefix
- tests: registry validation, mount coverage, toggle round-trip, dependency
  propagation, bootstrap/catalogue shape, supervisor; pack rule CS-13

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-12 17:46:52 +02:00
co-authored by Claude Opus 5.5
parent 968b6c78fb
commit 231a97c186
69 changed files with 1396 additions and 195 deletions
+32 -20
View File
@@ -501,32 +501,44 @@ async def _push_stats_snapshot():
logger.warning("Stats snapshot push failed: %s", e)
async def _connect_loop():
"""Faster loop: pulls pending online orders every CONNECT_SYNC_INTERVAL_SECONDS.
Also pushes menu snapshot and stats every 5 minutes (piggybacking on this loop).
On startup, waits for the first heartbeat to populate site_numeric_id then
immediately pushes menu + stats without waiting a full cycle."""
push_every = max(1, (5 * 60) // ORDER_POLL_INTERVAL)
# ── Module workers (APPS_PLATFORM D-19) ─────────────────────────────────────
# Started/stopped by core/modules/supervisor.py while their module is effective:
# connect_menu -> menu_push_worker (menu snapshot every 5 min)
# connect_orders -> order_pull_worker (pending online orders every CONNECT_SYNC_INTERVAL_SECONDS)
# connect_remote -> stats_push_worker (stats snapshot every 5 min)
# v1 ran all three in one _connect_loop. Each now waits for the first heartbeat to
# learn site_numeric_id, then does its first run at once.
PUSH_INTERVAL_SECONDS = 5 * 60
# Wait for the heartbeat loop to get site_numeric_id, then do an immediate push
for _ in range(30): # wait up to 30 seconds
async def _wait_for_site_id(max_seconds: int = 30) -> None:
for _ in range(max_seconds):
if license_state.get("site_numeric_id"):
break
return
await asyncio.sleep(1)
await _push_menu_snapshot()
await _push_stats_snapshot()
tick = 0
async def menu_push_worker():
await _wait_for_site_id()
while True:
await _push_menu_snapshot()
await asyncio.sleep(PUSH_INTERVAL_SECONDS)
async def stats_push_worker():
await _wait_for_site_id()
while True:
await _push_stats_snapshot()
await asyncio.sleep(PUSH_INTERVAL_SECONDS)
async def order_pull_worker():
await _wait_for_site_id()
while True:
await asyncio.sleep(ORDER_POLL_INTERVAL)
await _pull_pending_orders()
tick += 1
if tick % push_every == 0:
await _push_menu_snapshot()
await _push_stats_snapshot()
await asyncio.sleep(ORDER_POLL_INTERVAL)
async def start_cloud_sync() -> asyncio.Task:
heartbeat_task = asyncio.create_task(_sync_loop())
asyncio.create_task(_connect_loop())
return heartbeat_task
"""License heartbeat only (core). Connect pushes/pulls are module workers (see above)."""
return asyncio.create_task(_sync_loop())