From e7fc091701854c034b3d4faf8c69481ba5113ec5 Mon Sep 17 00:00:00 2001 From: Pouzor Date: Mon, 30 Mar 2026 23:55:38 +0200 Subject: [PATCH] fix: reschedule APScheduler job immediately when status check interval is updated MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Interval was read once at startup — changing it via API had no effect until server restart. Now calls reschedule_status_checks() after saving. --- backend/app/api/routes/scan.py | 2 ++ backend/app/core/scheduler.py | 6 ++++++ backend/tests/test_scan.py | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/backend/app/api/routes/scan.py b/backend/app/api/routes/scan.py index 7d9a535..ea19242 100644 --- a/backend/app/api/routes/scan.py +++ b/backend/app/api/routes/scan.py @@ -8,6 +8,7 @@ from sqlalchemy.ext.asyncio import AsyncSession from app.api.deps import get_current_user from app.core.config import settings +from app.core.scheduler import reschedule_status_checks from app.db.database import AsyncSessionLocal, get_db from app.db.models import Node, PendingDevice, ScanRun from app.schemas.nodes import NodeCreate @@ -110,6 +111,7 @@ async def update_scan_config(payload: ScanConfig, _: str = Depends(get_current_u try: settings.scanner_ranges = payload.ranges settings.save_overrides() + reschedule_status_checks(payload.interval_seconds) return payload except Exception as exc: raise HTTPException(status_code=500, detail=str(exc)) from exc diff --git a/backend/app/core/scheduler.py b/backend/app/core/scheduler.py index 91ba1df..fc44832 100644 --- a/backend/app/core/scheduler.py +++ b/backend/app/core/scheduler.py @@ -53,5 +53,11 @@ def start_scheduler() -> None: logger.info("Scheduler started — status checks every %ds", settings.status_checker_interval) +def reschedule_status_checks(interval_seconds: int) -> None: + """Update the status check interval on the running scheduler.""" + scheduler.reschedule_job("status_checks", trigger="interval", seconds=interval_seconds) + logger.info("Status checks rescheduled to every %ds", interval_seconds) + + def stop_scheduler() -> None: scheduler.shutdown(wait=False) diff --git a/backend/tests/test_scan.py b/backend/tests/test_scan.py index c03b8b4..c34b44d 100644 --- a/backend/tests/test_scan.py +++ b/backend/tests/test_scan.py @@ -156,6 +156,26 @@ async def test_ignore_device(client: AsyncClient, headers, pending_device): assert hidden_res.json() == [] +# --- Scan config --- + +@pytest.mark.asyncio +async def test_update_scan_config_reschedules_interval(client: AsyncClient, headers): + """Saving a new interval must reschedule the running APScheduler job immediately.""" + with ( + patch("app.api.routes.scan.settings") as mock_settings, + patch("app.api.routes.scan.reschedule_status_checks") as mock_reschedule, + ): + mock_settings.scanner_ranges = [] + mock_settings.save_overrides = lambda: None + res = await client.post( + "/api/v1/scan/config", + json={"ranges": ["192.168.1.0/24"], "interval_seconds": 30}, + headers=headers, + ) + assert res.status_code == 200 + mock_reschedule.assert_called_once_with(30) + + # --- Scan runs --- @pytest.mark.asyncio