feat: persistent monitor_enabled flag + auto-start keepalive on boot
Makes live monitoring (and therefore alerting) genuinely 24/7 and restart-surviving, instead of runtime-only keepalive. - NL43Config.monitor_enabled (default True) + migrate_add_monitor_enabled.py. - On startup, auto-start keepalive monitors for every monitor_enabled + tcp_enabled unit — so feeds/alerts resume after a restart with no manual step. - /monitor/start and /monitor/stop now PERSIST monitor_enabled (start=True, stop=False) in addition to applying keepalive at runtime, so the toggle sticks. Roster output includes monitor_enabled for the admin UI to read. On by default: configure a unit -> it's monitored 24/7 unless toggled off. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+19
@@ -38,6 +38,25 @@ async def lifespan(app: FastAPI):
|
||||
await poller.start()
|
||||
logger.info("Background poller started")
|
||||
|
||||
# Auto-start keepalive live monitors for units configured for 24/7 monitoring
|
||||
# (monitor_enabled). This is what keeps alerting running unattended across
|
||||
# restarts — without it a feed only runs while someone has the live view open.
|
||||
try:
|
||||
from app.monitor import monitor_manager
|
||||
from app.database import SessionLocal
|
||||
from app.models import NL43Config
|
||||
db = SessionLocal()
|
||||
try:
|
||||
units = db.query(NL43Config).filter_by(monitor_enabled=True, tcp_enabled=True).all()
|
||||
for cfg in units:
|
||||
m = await monitor_manager.get(cfg.unit_id)
|
||||
await m.set_keepalive(True)
|
||||
logger.info(f"Auto-started keepalive monitor for {cfg.unit_id}")
|
||||
finally:
|
||||
db.close()
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to auto-start monitors: {e}")
|
||||
|
||||
yield # Application runs
|
||||
|
||||
# Shutdown
|
||||
|
||||
@@ -23,6 +23,10 @@ class NL43Config(Base):
|
||||
poll_interval_seconds = Column(Integer, nullable=True, default=60) # Polling interval (10-3600 seconds)
|
||||
poll_enabled = Column(Boolean, default=True) # Enable/disable background polling for this device
|
||||
|
||||
# Live monitor (fan-out DOD feed). Keepalive runs it 24/7 even with no viewer,
|
||||
# which is what makes alerting continuous. On by default; toggleable from the UI.
|
||||
monitor_enabled = Column(Boolean, default=True)
|
||||
|
||||
|
||||
class NL43Status(Base):
|
||||
"""
|
||||
|
||||
+18
-7
@@ -295,22 +295,32 @@ async def monitor_stream(websocket: WebSocket, unit_id: str):
|
||||
|
||||
|
||||
@router.post("/{unit_id}/monitor/start")
|
||||
async def monitor_start(unit_id: str):
|
||||
"""Keep the device's feed running even with no browser attached, so alerting
|
||||
evaluates continuously. Runtime-only (resets on restart)."""
|
||||
async def monitor_start(unit_id: str, db: Session = Depends(get_db)):
|
||||
"""Enable 24/7 keepalive monitoring: persist monitor_enabled and start the feed
|
||||
now, so alerting evaluates continuously even with no viewer. Survives restarts
|
||||
(auto-started on boot from the persisted flag)."""
|
||||
cfg = db.query(NL43Config).filter_by(unit_id=unit_id).first()
|
||||
if cfg:
|
||||
cfg.monitor_enabled = True
|
||||
db.commit()
|
||||
from app.monitor import monitor_manager
|
||||
monitor = await monitor_manager.get(unit_id)
|
||||
await monitor.set_keepalive(True)
|
||||
return {"status": "ok", "unit_id": unit_id, "running": monitor.running, "keepalive": True}
|
||||
return {"status": "ok", "unit_id": unit_id, "monitor_enabled": True, "running": monitor.running}
|
||||
|
||||
|
||||
@router.post("/{unit_id}/monitor/stop")
|
||||
async def monitor_stop(unit_id: str):
|
||||
"""Drop the keep-alive; the feed stops once no browser subscribers remain."""
|
||||
async def monitor_stop(unit_id: str, db: Session = Depends(get_db)):
|
||||
"""Disable keepalive monitoring: persist monitor_enabled=False and drop the
|
||||
keepalive (the feed stops once no browser subscribers remain)."""
|
||||
cfg = db.query(NL43Config).filter_by(unit_id=unit_id).first()
|
||||
if cfg:
|
||||
cfg.monitor_enabled = False
|
||||
db.commit()
|
||||
from app.monitor import monitor_manager
|
||||
monitor = await monitor_manager.get(unit_id)
|
||||
await monitor.set_keepalive(False)
|
||||
return {"status": "ok", "unit_id": unit_id, "keepalive": False}
|
||||
return {"status": "ok", "unit_id": unit_id, "monitor_enabled": False}
|
||||
|
||||
|
||||
@router.get("/_monitor/status")
|
||||
@@ -501,6 +511,7 @@ def get_roster(db: Session = Depends(get_db)):
|
||||
"web_enabled": cfg.web_enabled,
|
||||
"poll_enabled": cfg.poll_enabled,
|
||||
"poll_interval_seconds": cfg.poll_interval_seconds,
|
||||
"monitor_enabled": cfg.monitor_enabled,
|
||||
"status": None
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user