mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-25 17:36:18 -05:00
Creating common get_warn_once for rare exceptions.
This commit is contained in:
parent
3d15ba2091
commit
1a2916ef1c
3 changed files with 24 additions and 16 deletions
|
|
@ -2,7 +2,7 @@ import enum
|
|||
import subprocess
|
||||
|
||||
from wrolpi.cmd import which
|
||||
from wrolpi.common import logger, WROLPI_CONFIG
|
||||
from wrolpi.common import logger, WROLPI_CONFIG, get_warn_once
|
||||
from wrolpi.vars import DEFAULT_CPU_FREQUENCY, DOCKERIZED
|
||||
|
||||
logger = logger.getChild(__name__)
|
||||
|
|
@ -14,6 +14,8 @@ CPUFREQ_SET_BIN = which('cpufreq-set', '/usr/bin/cpufreq-set')
|
|||
|
||||
POWER_SAVE_FREQ = 'powersave' # noqa
|
||||
|
||||
warn_once = get_warn_once('Unable to use nmcli', logger)
|
||||
|
||||
|
||||
class HotspotStatus(enum.Enum):
|
||||
disconnected = enum.auto() # Radio is on, but Hotspot is not connected.
|
||||
|
|
@ -28,10 +30,9 @@ def hotspot_status() -> HotspotStatus:
|
|||
|
||||
cmd = (NMCLI_BIN,)
|
||||
try:
|
||||
output = subprocess.check_output(cmd).decode().strip()
|
||||
except FileNotFoundError as e:
|
||||
if not DOCKERIZED:
|
||||
logger.debug(f'Could not get hotspot status', exc_info=e)
|
||||
output = subprocess.check_output(cmd, stderr=subprocess.PIPE).decode().strip()
|
||||
except Exception as e:
|
||||
warn_once(e)
|
||||
return HotspotStatus.unknown
|
||||
|
||||
device = WROLPI_CONFIG.hotspot_device
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@ __all__ = [
|
|||
'get_model_by_table_name',
|
||||
'tsvector',
|
||||
'compile_tsvector',
|
||||
'WROLPI_CONFIG',
|
||||
'sanitize_link',
|
||||
'ConfigFile',
|
||||
'get_config',
|
||||
|
|
@ -90,6 +91,7 @@ __all__ = [
|
|||
'limit_concurrent',
|
||||
'truncate_object_bytes',
|
||||
'background_task',
|
||||
'get_warn_once',
|
||||
]
|
||||
|
||||
# Base is used for all SQLAlchemy models.
|
||||
|
|
@ -1009,3 +1011,17 @@ def background_task(coro) -> Task:
|
|||
BACKGROUND_TASKS.add(task)
|
||||
task.add_done_callback(BACKGROUND_TASKS.discard)
|
||||
return task
|
||||
|
||||
|
||||
def get_warn_once(message: str, logger_: logging.Logger, level=logging.ERROR):
|
||||
"""Create a function that will report an error only once.
|
||||
|
||||
This is used when a function will be called many times, but the error is not important."""
|
||||
event = multiprocessing.Event()
|
||||
|
||||
def warn_once(exception: Exception):
|
||||
if not event.is_set():
|
||||
logger_.log(level, message, exc_info=exception)
|
||||
event.set()
|
||||
|
||||
return warn_once
|
||||
|
|
|
|||
|
|
@ -10,9 +10,8 @@ from pathlib import Path
|
|||
from typing import List, Optional, Tuple
|
||||
|
||||
from wrolpi.cmd import which
|
||||
from wrolpi.common import logger, limit_concurrent
|
||||
from wrolpi.common import logger, limit_concurrent, get_warn_once
|
||||
from wrolpi.dates import now
|
||||
from wrolpi.vars import DOCKERIZED
|
||||
|
||||
try:
|
||||
import psutil
|
||||
|
|
@ -23,16 +22,8 @@ except ImportError:
|
|||
logger = logger.getChild(__name__)
|
||||
|
||||
UPTIME_BIN = which('uptime', '/usr/bin/uptime')
|
||||
PSUTIL_WARNED = multiprocessing.Event()
|
||||
|
||||
|
||||
def warn_once(exception: Exception):
|
||||
"""
|
||||
Don't spam the logs with errors when status can't use psutil.
|
||||
"""
|
||||
if not PSUTIL_WARNED.is_set():
|
||||
logger.error(f'Unable to use psutil', exc_info=exception)
|
||||
PSUTIL_WARNED.set()
|
||||
warn_once = get_warn_once('Unable to use psutil', logger)
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue