Rearchitect so a single poller process is the sole owner of all SAS/SMART/ SES hardware I/O, serialized behind one gate and paced, writing results to Redis. The API/web and MQTT publisher become pure Redis readers — they no longer issue any subprocess and can restart freely without touching the bus. This addresses backplane/expander stress from concurrent + restart-triggered SMART/SES storms (the prior model re-ran a hardware sweep on every container start, and polled sg_ses 0x02+0x07 every 60s; 0x07 errored on the IOM6/ Xyratex expanders). - poller.py: paced sweep (inventory, SMART per-drive w/ gap, SES 0x02, ZFS, host/MegaRAID), startup jitter, single-instance Redis lock, LED-queue worker - services/hwgate.py: global serialization semaphore (POLL_CONCURRENCY=1) - services/store.py: Redis as the only producer<->consumer interface + LED queue - services/health.py: shared drive-health classifier (fixes overview double-count) - gate smartctl/sg_ses/zpool/ledctl; drop sg_ses 0x07 from the hot path - routers + temps read-only from the store; main.py drops the in-process poller - compose: 3 services (privileged poller + unprivileged app + redis) w/ pacing knobs
68 lines
1.8 KiB
Python
68 lines
1.8 KiB
Python
import json
|
|
import logging
|
|
import os
|
|
from typing import Any
|
|
|
|
import redis.asyncio as redis
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_redis: redis.Redis | None = None
|
|
|
|
|
|
async def init_cache() -> None:
|
|
"""Create Redis connection from environment variables."""
|
|
global _redis
|
|
host = os.environ.get("REDIS_HOST", "localhost")
|
|
port = int(os.environ.get("REDIS_PORT", "6379"))
|
|
db = int(os.environ.get("REDIS_DB", "0"))
|
|
try:
|
|
_redis = redis.Redis(host=host, port=port, db=db, decode_responses=True)
|
|
await _redis.ping()
|
|
logger.info("Redis connected at %s:%d/%d", host, port, db)
|
|
except Exception as e:
|
|
logger.warning("Redis connection failed: %s — running without cache", e)
|
|
_redis = None
|
|
|
|
|
|
async def close_cache() -> None:
|
|
"""Close Redis connection."""
|
|
global _redis
|
|
if _redis is not None:
|
|
await _redis.aclose()
|
|
_redis = None
|
|
|
|
|
|
def redis_available() -> bool:
|
|
"""Return whether Redis connection is live."""
|
|
return _redis is not None
|
|
|
|
|
|
def get_client() -> "redis.Redis | None":
|
|
"""Return the raw Redis client for primitives not covered by get/set."""
|
|
return _redis
|
|
|
|
|
|
async def cache_get(key: str) -> Any | None:
|
|
"""GET key from Redis, return deserialized value or None on miss/error."""
|
|
if _redis is None:
|
|
return None
|
|
try:
|
|
raw = await _redis.get(key)
|
|
if raw is None:
|
|
return None
|
|
return json.loads(raw)
|
|
except Exception as e:
|
|
logger.warning("Redis GET %s failed: %s", key, e)
|
|
return None
|
|
|
|
|
|
async def cache_set(key: str, value: Any, ttl: int = 120) -> None:
|
|
"""SET key in Redis with expiry, silently catches errors."""
|
|
if _redis is None:
|
|
return
|
|
try:
|
|
await _redis.set(key, json.dumps(value), ex=ttl)
|
|
except Exception as e:
|
|
logger.warning("Redis SET %s failed: %s", key, e)
|