fixed test warning

This commit is contained in:
Jonas Linter
2025-10-15 08:55:51 +02:00
parent 3669d0ca00
commit 5a0ae44a45
4 changed files with 54 additions and 26 deletions

View File

@@ -174,13 +174,12 @@ class EmailAlertHandler(logging.Handler):
self._flush_buffer(immediate=True),
self.loop,
)
else:
# Schedule delayed flush if not already scheduled
if not self._flush_task or self._flush_task.done():
self._flush_task = asyncio.run_coroutine_threadsafe(
self._schedule_delayed_flush(),
self.loop,
)
# Schedule delayed flush if not already scheduled
elif not self._flush_task or self._flush_task.done():
self._flush_task = asyncio.run_coroutine_threadsafe(
self._schedule_delayed_flush(),
self.loop,
)
except Exception:
# Never let the handler crash - just log and continue
@@ -237,7 +236,9 @@ class EmailAlertHandler(logging.Handler):
emoji = "⚠️"
reason = f"({self.buffer_minutes} minute buffer)"
subject = f"{emoji} AlpineBits Error {alert_type}: {error_count} errors {reason}"
subject = (
f"{emoji} AlpineBits Error {alert_type}: {error_count} errors {reason}"
)
# Build plain text body
body = f"Error Alert - {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n"
@@ -290,10 +291,18 @@ class EmailAlertHandler(logging.Handler):
# Flush any remaining errors immediately
if self.error_buffer and self.loop:
try:
asyncio.run_coroutine_threadsafe(
self._flush_buffer(immediate=False),
self.loop,
).result(timeout=5)
# Check if the loop is still running
if not self.loop.is_closed():
future = asyncio.run_coroutine_threadsafe(
self._flush_buffer(immediate=False),
self.loop,
)
future.result(timeout=5)
else:
_LOGGER.warning(
"Event loop closed, cannot flush %d remaining errors",
len(self.error_buffer),
)
except Exception:
_LOGGER.exception("Error flushing buffer on close")