diff --git a/wrolpi/admin.py b/wrolpi/admin.py index c214bfcf..659b2fb2 100644 --- a/wrolpi/admin.py +++ b/wrolpi/admin.py @@ -12,7 +12,8 @@ logger = logger.getChild(__name__) POWER_SAVE_FREQ = 'powersave' # noqa -warn_once = get_warn_once('Unable to use nmcli', logger) +nmcli_warn_once = get_warn_once('Unable to use nmcli', logger) +iwgetid_warn_once = get_warn_once('Unable to use iwgetid', logger) class HotspotStatus(enum.Enum): @@ -37,7 +38,7 @@ def hotspot_status() -> HotspotStatus: try: output = subprocess.check_output(cmd, stderr=subprocess.PIPE).decode().strip() except Exception as e: - warn_once(e) + nmcli_warn_once(e) return HotspotStatus.unknown for line in output.splitlines(): @@ -69,6 +70,9 @@ def get_current_ssid(interface='wlan0') -> str | None: else: logger.error(f'Failed to get current ssid: {result.stderr}') return None + except FileNotFoundError as e: + iwgetid_warn_once(e) + return None except Exception as e: logger.error('Failed to get current ssid', exc_info=e) return None diff --git a/wrolpi/common.py b/wrolpi/common.py index 5b29dfe4..cfbe820f 100644 --- a/wrolpi/common.py +++ b/wrolpi/common.py @@ -1563,12 +1563,12 @@ 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(): + from wrolpi.api_utils import api_app + if not api_app.shared_ctx.warn_once.get(message): logger__.log(level, message, exc_info=exception) - event.set() + api_app.shared_ctx.warn_once.update({message: True}) return warn_once diff --git a/wrolpi/contexts.py b/wrolpi/contexts.py index d4097d42..aa460aa8 100644 --- a/wrolpi/contexts.py +++ b/wrolpi/contexts.py @@ -47,6 +47,9 @@ def attach_shared_contexts(app: Sanic): app.shared_ctx.single_tasks_started = multiprocessing.Event() app.shared_ctx.flags_initialized = multiprocessing.Event() + # Warnings + app.shared_ctx.warn_once = manager.dict() + # Locks app.shared_ctx.config_save_lock = multiprocessing.Lock()