From b03c92bdefaf33971e87baca4eb5defbd3547332 Mon Sep 17 00:00:00 2001 From: oliver Date: Sun, 2 Aug 2026 16:55:46 +0800 Subject: [PATCH] Default collectors inline with the API again. Frontend+backend start is enough; external worker remains an optional production split. Co-authored-by: Cursor --- .env.example | 6 +++--- PROD_MIN_CHECKLIST.md | 2 +- netx_api/app_startup.py | 2 +- netx_api/config.py | 6 +++--- netx_api/integrations_router.py | 2 +- netx_api/ume_runtime.py | 3 ++- netx_api/worker.py | 4 ++-- tests/test_schema_patches.py | 2 +- 8 files changed, 14 insertions(+), 13 deletions(-) diff --git a/.env.example b/.env.example index 780b88b..dbecbc0 100644 --- a/.env.example +++ b/.env.example @@ -54,8 +54,8 @@ NETX_UME_NOTIFICATION_TOPIC=ALARM # NETX_ALEMBIC_UPGRADE_ON_START=false # NETX_SKIP_LEGACY_STARTUP_DDL=false # NETX_SQL_READONLY_DATABASE_URL=postgresql+psycopg://netx_ro:xxx@127.0.0.1:5432/netx -# Device collectors: default off in API — run `python -m netx_api.worker`. -# Lab single-process: NETX_RUN_INLINE_SCHEDULERS=true -# NETX_RUN_INLINE_SCHEDULERS=true +# Device collectors run inline with the API by default (frontend+backend start is enough). +# Production split only: NETX_RUN_INLINE_SCHEDULERS=false and run `python -m netx_api.worker` +# NETX_RUN_INLINE_SCHEDULERS=false # NETX_AUDIT_ASYNC=true # NETX_AUDIT_SAMPLE_N=1 diff --git a/PROD_MIN_CHECKLIST.md b/PROD_MIN_CHECKLIST.md index 906ef7c..0140727 100644 --- a/PROD_MIN_CHECKLIST.md +++ b/PROD_MIN_CHECKLIST.md @@ -12,7 +12,7 @@ ## Runtime - Ensure PostgreSQL backup policy exists (daily logical backup + retention). - Schema: API auto-runs `alembic upgrade head` on start (see [docs/ALEMBIC.md](docs/ALEMBIC.md)). No manual migrate flag required for normal deploys. -- Collectors: default is external worker (`python -m netx_api.worker`). Only set `NETX_RUN_INLINE_SCHEDULERS=true` for single-process lab. Check `/health/ready` → `schedulers.mode`. +- Collectors: default inline with the API (normal frontend+backend start is enough). Optional split: `NETX_RUN_INLINE_SCHEDULERS=false` + `python -m netx_api.worker`. Check `/health/ready` → `schedulers.mode`. - Run `oclaw` and `netx` under process managers (systemd/Windows service/pm2 equivalent). - Enable auto-restart and startup-at-boot for both services. diff --git a/netx_api/app_startup.py b/netx_api/app_startup.py index 4469811..a8f19f2 100644 --- a/netx_api/app_startup.py +++ b/netx_api/app_startup.py @@ -130,7 +130,7 @@ def run_api_startup() -> None: finally: db.close() - if bool(getattr(settings, "run_inline_schedulers", False)): + if bool(getattr(settings, "run_inline_schedulers", True)): try: start_device_schedulers() except Exception: diff --git a/netx_api/config.py b/netx_api/config.py index 147d536..a05752c 100644 --- a/netx_api/config.py +++ b/netx_api/config.py @@ -140,9 +140,9 @@ class Settings(BaseSettings): alembic_upgrade_on_start: bool = True # Optional dedicated SQLAlchemy URL for /v1/sql/* (read-only DB role recommended). sql_readonly_database_url: str = "" - # When false (default), API skips config_sync / lldp / port_traffic schedulers — - # run `python -m netx_api.worker` alongside the API. Set true only for single-process lab. - run_inline_schedulers: bool = False + # When true (default), API also runs config_sync / lldp / port_traffic schedulers. + # Production split: set false and run `python -m netx_api.worker` beside the API. + run_inline_schedulers: bool = True settings = Settings() diff --git a/netx_api/integrations_router.py b/netx_api/integrations_router.py index 9eb27db..c60e88a 100644 --- a/netx_api/integrations_router.py +++ b/netx_api/integrations_router.py @@ -36,7 +36,7 @@ def health_ready(db: Session = Depends(get_db)) -> dict[str, Any]: "db": "down", "error": str(exc)[:240], } - inline = bool(getattr(settings, "run_inline_schedulers", False)) + inline = bool(getattr(settings, "run_inline_schedulers", True)) out["schedulers"] = { "inline": inline, "mode": "inline" if inline else "external_worker", diff --git a/netx_api/ume_runtime.py b/netx_api/ume_runtime.py index 812407a..36b1d7a 100644 --- a/netx_api/ume_runtime.py +++ b/netx_api/ume_runtime.py @@ -1,7 +1,8 @@ """UME / long-task runtime helpers shared by API and optional worker process. Device collectors (config_sync / LLDP / port_traffic) run via ``start_device_schedulers`` -(API inline when ``NETX_RUN_INLINE_SCHEDULERS=true``, otherwise ``python -m netx_api.worker``). +(API inline by default; set ``NETX_RUN_INLINE_SCHEDULERS=false`` and run +``python -m netx_api.worker`` for a split process). API process also owns UME keepalive, alarm WSS, current-alarm/inventory sync loops, and oclaw forwarder via ``start_api_sideband_threads``. """ diff --git a/netx_api/worker.py b/netx_api/worker.py index 56b1abc..3fcd6e5 100644 --- a/netx_api/worker.py +++ b/netx_api/worker.py @@ -1,12 +1,12 @@ """Background worker process for long-running schedulers. -Default deployment: API has ``NETX_RUN_INLINE_SCHEDULERS=false``; run this -alongside the API: +Optional when ``NETX_RUN_INLINE_SCHEDULERS=false`` (API does not start collectors): python -m netx_api.worker Starts: config_sync, lldp_collect, port_traffic tick loops. UME WS / keepalive remain in the API process (token + alarm coordination). +By default the API runs collectors inline — no separate worker needed. """ from __future__ import annotations diff --git a/tests/test_schema_patches.py b/tests/test_schema_patches.py index 1996f01..2345a5e 100644 --- a/tests/test_schema_patches.py +++ b/tests/test_schema_patches.py @@ -42,7 +42,7 @@ class SchemaPatchesTests(unittest.TestCase): from netx_api.config import Settings s = Settings(_env_file=None) - self.assertFalse(s.run_inline_schedulers) + self.assertTrue(s.run_inline_schedulers) self.assertTrue(s.alembic_upgrade_on_start) versions = Path(__file__).resolve().parents[1] / "alembic" / "versions"