Standardize async offloading behind shared executor helper
This commit is contained in:
@@ -7,6 +7,7 @@ for fail2ban action configurations.
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from app.utils.async_utils import run_blocking
|
||||
import configparser
|
||||
import contextlib
|
||||
import io
|
||||
@@ -602,10 +603,10 @@ async def list_actions(
|
||||
action_d = Path(config_dir) / "action.d"
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
raw_actions: list[tuple[str, str, str, bool, str]] = await loop.run_in_executor(None, _parse_actions_sync, action_d)
|
||||
raw_actions: list[tuple[str, str, str, bool, str]] = await run_blocking( _parse_actions_sync, action_d)
|
||||
|
||||
all_jails_result, active_names = await asyncio.gather(
|
||||
loop.run_in_executor(None, _parse_jails_sync, Path(config_dir)),
|
||||
run_blocking( _parse_jails_sync, Path(config_dir)),
|
||||
_get_active_jail_names(socket_path),
|
||||
)
|
||||
all_jails, _source_files = all_jails_result
|
||||
@@ -699,12 +700,12 @@ async def get_action(
|
||||
else:
|
||||
raise ActionNotFoundError(base_name)
|
||||
|
||||
content, has_local, source_path = await loop.run_in_executor(None, _read)
|
||||
content, has_local, source_path = await run_blocking( _read)
|
||||
|
||||
cfg = conffile_parser.parse_action_file(content, name=base_name, filename=f"{base_name}.conf")
|
||||
|
||||
all_jails_result, active_names = await asyncio.gather(
|
||||
loop.run_in_executor(None, _parse_jails_sync, Path(config_dir)),
|
||||
run_blocking( _parse_jails_sync, Path(config_dir)),
|
||||
_get_active_jail_names(socket_path),
|
||||
)
|
||||
all_jails, _source_files = all_jails_result
|
||||
@@ -787,7 +788,7 @@ async def update_action(
|
||||
|
||||
action_d = Path(config_dir) / "action.d"
|
||||
loop = asyncio.get_event_loop()
|
||||
await loop.run_in_executor(None, _write_action_local_sync, action_d, base_name, content)
|
||||
await run_blocking( _write_action_local_sync, action_d, base_name, content)
|
||||
|
||||
if do_reload:
|
||||
try:
|
||||
@@ -840,7 +841,7 @@ async def create_action(
|
||||
raise ActionAlreadyExistsError(req.name)
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
await loop.run_in_executor(None, _check_not_exists)
|
||||
await run_blocking( _check_not_exists)
|
||||
|
||||
cfg = ActionConfig(
|
||||
name=req.name,
|
||||
@@ -856,7 +857,7 @@ async def create_action(
|
||||
)
|
||||
content = conffile_parser.serialize_action_config(cfg)
|
||||
|
||||
await loop.run_in_executor(None, _write_action_local_sync, action_d, req.name, content)
|
||||
await run_blocking( _write_action_local_sync, action_d, req.name, content)
|
||||
|
||||
if do_reload:
|
||||
try:
|
||||
@@ -921,7 +922,7 @@ async def delete_action(
|
||||
|
||||
log.info("action_local_deleted", action=base_name, path=str(local_path))
|
||||
|
||||
await loop.run_in_executor(None, _delete)
|
||||
await run_blocking( _delete)
|
||||
|
||||
|
||||
async def assign_action_to_jail(
|
||||
@@ -958,7 +959,7 @@ async def assign_action_to_jail(
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
all_jails, _src = await loop.run_in_executor(None, _parse_jails_sync, Path(config_dir))
|
||||
all_jails, _src = await run_blocking( _parse_jails_sync, Path(config_dir))
|
||||
if jail_name not in all_jails:
|
||||
raise JailNotFoundInConfigError(jail_name)
|
||||
|
||||
@@ -971,7 +972,7 @@ async def assign_action_to_jail(
|
||||
):
|
||||
raise ActionNotFoundError(req.action_name)
|
||||
|
||||
await loop.run_in_executor(None, _check_action)
|
||||
await run_blocking( _check_action)
|
||||
|
||||
# Build the action string with optional parameters.
|
||||
if req.params:
|
||||
@@ -980,7 +981,7 @@ async def assign_action_to_jail(
|
||||
else:
|
||||
action_entry = req.action_name
|
||||
|
||||
await loop.run_in_executor(
|
||||
await run_blocking(
|
||||
None,
|
||||
_append_jail_action_sync,
|
||||
Path(config_dir),
|
||||
@@ -1038,11 +1039,11 @@ async def remove_action_from_jail(
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
all_jails, _src = await loop.run_in_executor(None, _parse_jails_sync, Path(config_dir))
|
||||
all_jails, _src = await run_blocking( _parse_jails_sync, Path(config_dir))
|
||||
if jail_name not in all_jails:
|
||||
raise JailNotFoundInConfigError(jail_name)
|
||||
|
||||
await loop.run_in_executor(
|
||||
await run_blocking(
|
||||
None,
|
||||
_remove_jail_action_sync,
|
||||
Path(config_dir),
|
||||
|
||||
Reference in New Issue
Block a user