Warning once when iwgetid is not available.

This commit is contained in:
Roland 2024-08-01 13:43:53 -06:00
parent a98aebfdda
commit c3f518060e
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
3 changed files with 12 additions and 5 deletions

View file

@ -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

View file

@ -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

View file

@ -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()