Refactor periodic tasks to use injected scheduler resources
This commit is contained in:
@@ -24,6 +24,8 @@ from app.utils.runtime_state import get_effective_settings
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import aiosqlite
|
||||
from aiohttp import ClientSession
|
||||
from app.config import Settings
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from fastapi import FastAPI
|
||||
@@ -34,25 +36,19 @@ log: structlog.stdlib.BoundLogger = structlog.get_logger()
|
||||
JOB_ID: str = "blocklist_import"
|
||||
|
||||
|
||||
async def _get_db(app: Any) -> tuple[aiosqlite.Connection, bool]:
|
||||
settings = get_effective_settings(app)
|
||||
async def _get_db(settings: "Settings") -> tuple[aiosqlite.Connection, bool]:
|
||||
db = await open_db(settings.database_path)
|
||||
return db, True
|
||||
|
||||
|
||||
async def _run_import(app: Any) -> None:
|
||||
async def _run_import_with_resources(settings: "Settings", http_session: "ClientSession") -> None:
|
||||
"""APScheduler callback that imports all enabled blocklist sources.
|
||||
|
||||
Reads shared resources from ``app.state`` and delegates to
|
||||
:func:`~app.services.blocklist_service.import_all`.
|
||||
|
||||
Args:
|
||||
app: The :class:`fastapi.FastAPI` application instance passed via
|
||||
APScheduler ``kwargs``.
|
||||
settings: The resolved application settings used for database access.
|
||||
http_session: The shared aiohttp session used for blocklist downloads.
|
||||
"""
|
||||
db, close_db = await _get_db(app)
|
||||
settings = get_effective_settings(app)
|
||||
http_session = app.state.http_session
|
||||
db, close_db = await _get_db(settings)
|
||||
socket_path: str = settings.fail2ban_socket
|
||||
|
||||
log.info("blocklist_import_starting")
|
||||
@@ -75,6 +71,10 @@ async def _run_import(app: Any) -> None:
|
||||
await db.close()
|
||||
|
||||
|
||||
async def _run_import(app: FastAPI) -> None:
|
||||
await _run_import_with_resources(get_effective_settings(app), app.state.http_session)
|
||||
|
||||
|
||||
async def register(app: FastAPI) -> None:
|
||||
"""Add (or replace) the blocklist import job in the application scheduler.
|
||||
|
||||
@@ -88,7 +88,8 @@ async def register(app: FastAPI) -> None:
|
||||
app: The :class:`fastapi.FastAPI` application instance whose
|
||||
``app.state.scheduler`` will receive the job.
|
||||
"""
|
||||
db, close_db = await _get_db(app)
|
||||
settings = get_effective_settings(app)
|
||||
db, close_db = await _get_db(settings)
|
||||
try:
|
||||
config = await blocklist_service.get_schedule(db)
|
||||
finally:
|
||||
@@ -110,7 +111,8 @@ def reschedule(app: FastAPI) -> None:
|
||||
import asyncio # noqa: PLC0415
|
||||
|
||||
async def _do_reschedule() -> None:
|
||||
db, close_db = await _get_db(app)
|
||||
settings = get_effective_settings(app)
|
||||
db, close_db = await _get_db(settings)
|
||||
try:
|
||||
config = await blocklist_service.get_schedule(db)
|
||||
finally:
|
||||
@@ -130,7 +132,10 @@ def _apply_schedule(app: FastAPI, config: Any) -> None:
|
||||
"""
|
||||
scheduler = app.state.scheduler
|
||||
|
||||
kwargs: dict[str, Any] = {"app": app}
|
||||
kwargs: dict[str, Any] = {
|
||||
"settings": get_effective_settings(app),
|
||||
"http_session": app.state.http_session,
|
||||
}
|
||||
trigger_type: str
|
||||
trigger_kwargs: dict[str, Any]
|
||||
|
||||
@@ -156,7 +161,7 @@ def _apply_schedule(app: FastAPI, config: Any) -> None:
|
||||
scheduler.remove_job(JOB_ID)
|
||||
|
||||
scheduler.add_job(
|
||||
_run_import,
|
||||
_run_import_with_resources,
|
||||
trigger=trigger_type,
|
||||
id=JOB_ID,
|
||||
kwargs=kwargs,
|
||||
|
||||
Reference in New Issue
Block a user