Mark async socket handling task done and implement startup cleanup
This commit is contained in:
@@ -7,6 +7,7 @@ in ``app.main`` delegates resource creation and task registration here.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import suppress
|
||||
from pathlib import Path
|
||||
|
||||
import aiohttp
|
||||
@@ -101,13 +102,22 @@ async def startup_shared_resources(
|
||||
http_session: aiohttp.ClientSession = _create_http_session(settings)
|
||||
geo_service.init_geoip(settings.geoip_db_path)
|
||||
|
||||
scheduler: AsyncIOScheduler = AsyncIOScheduler(timezone="UTC")
|
||||
scheduler.start()
|
||||
scheduler: AsyncIOScheduler | None = None
|
||||
try:
|
||||
scheduler = AsyncIOScheduler(timezone="UTC")
|
||||
scheduler.start()
|
||||
|
||||
health_check.register(app)
|
||||
blocklist_import.register(app)
|
||||
geo_cache_flush.register(app)
|
||||
geo_re_resolve.register(app)
|
||||
history_sync.register(app)
|
||||
health_check.register(app)
|
||||
blocklist_import.register(app)
|
||||
geo_cache_flush.register(app)
|
||||
geo_re_resolve.register(app)
|
||||
history_sync.register(app)
|
||||
|
||||
return http_session, scheduler
|
||||
return http_session, scheduler
|
||||
except Exception:
|
||||
with suppress(Exception):
|
||||
await http_session.close()
|
||||
if scheduler is not None:
|
||||
with suppress(Exception):
|
||||
scheduler.shutdown(wait=False)
|
||||
raise
|
||||
|
||||
@@ -121,9 +121,8 @@ def _send_command_sync(
|
||||
) -> object:
|
||||
"""Send a command to fail2ban and return the parsed response.
|
||||
|
||||
This is a **synchronous** function intended to be called from within
|
||||
:func:`asyncio.get_event_loop().run_in_executor` so that the event loop
|
||||
is not blocked.
|
||||
This is a **synchronous** function intended to be executed via
|
||||
:func:`asyncio.to_thread` so that the event loop is not blocked.
|
||||
|
||||
Transient ``OSError`` conditions (``EAGAIN``, ``ECONNREFUSED``,
|
||||
``ENOBUFS``) are retried up to :data:`_RETRY_MAX_ATTEMPTS` times with
|
||||
@@ -299,15 +298,13 @@ class Fail2BanClient:
|
||||
|
||||
async with self._command_semaphore:
|
||||
log.debug("fail2ban_sending_command", command=command)
|
||||
loop: asyncio.AbstractEventLoop = asyncio.get_event_loop()
|
||||
try:
|
||||
response: object = await loop.run_in_executor(
|
||||
None,
|
||||
_send_command_sync,
|
||||
self.socket_path,
|
||||
command,
|
||||
self.timeout,
|
||||
)
|
||||
response: object = await asyncio.to_thread(
|
||||
_send_command_sync,
|
||||
self.socket_path,
|
||||
command,
|
||||
self.timeout,
|
||||
)
|
||||
except Fail2BanConnectionError:
|
||||
log.warning(
|
||||
"fail2ban_connection_error",
|
||||
|
||||
Reference in New Issue
Block a user