Invert blocklist scheduler dependency to task callback
This commit is contained in:
@@ -48,6 +48,7 @@ from app.models.blocklist import (
|
||||
ScheduleInfo,
|
||||
)
|
||||
from app.services import blocklist_service, geo_service, jail_service
|
||||
from app.tasks.blocklist_import import run_import_with_resources
|
||||
|
||||
router: APIRouter = APIRouter(prefix="/api/blocklists", tags=["Blocklists"])
|
||||
|
||||
@@ -203,6 +204,7 @@ async def update_schedule(
|
||||
http_session,
|
||||
settings,
|
||||
payload,
|
||||
run_import_with_resources,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -525,9 +525,9 @@ def schedule_blocklist_job(
|
||||
settings: Settings,
|
||||
http_session: aiohttp.ClientSession,
|
||||
config: ScheduleConfig,
|
||||
run_import_callback: Callable[[Settings, aiohttp.ClientSession], Awaitable[None]],
|
||||
) -> None:
|
||||
"""Register or replace the scheduled blocklist import job."""
|
||||
from app.tasks import blocklist_import as blocklist_import_task
|
||||
|
||||
if scheduler.get_job(JOB_ID):
|
||||
scheduler.remove_job(JOB_ID)
|
||||
@@ -555,7 +555,7 @@ def schedule_blocklist_job(
|
||||
}
|
||||
|
||||
scheduler.add_job(
|
||||
blocklist_import_task._run_import_with_resources,
|
||||
run_import_callback,
|
||||
trigger=trigger_type,
|
||||
id=JOB_ID,
|
||||
kwargs=kwargs,
|
||||
@@ -654,10 +654,17 @@ async def update_schedule(
|
||||
http_session: aiohttp.ClientSession,
|
||||
settings: Settings,
|
||||
config: ScheduleConfig,
|
||||
run_import_callback: Callable[[Settings, aiohttp.ClientSession], Awaitable[None]],
|
||||
) -> ScheduleInfo:
|
||||
"""Persist a new schedule config and re-register the scheduled job."""
|
||||
await set_schedule(db, config)
|
||||
schedule_blocklist_job(scheduler, settings, http_session, config)
|
||||
schedule_blocklist_job(
|
||||
scheduler,
|
||||
settings,
|
||||
http_session,
|
||||
config,
|
||||
run_import_callback,
|
||||
)
|
||||
return await get_schedule_info(db, _get_job_next_run_at(scheduler))
|
||||
|
||||
|
||||
|
||||
@@ -70,6 +70,9 @@ async def _run_import_with_resources(settings: Settings, http_session: ClientSes
|
||||
await db.close()
|
||||
|
||||
|
||||
run_import_with_resources = _run_import_with_resources
|
||||
|
||||
|
||||
async def _run_import(app: FastAPI) -> None:
|
||||
await _run_import_with_resources(get_effective_settings(app), app.state.http_session)
|
||||
|
||||
@@ -136,4 +139,5 @@ def _apply_schedule(app: FastAPI, config: Any) -> None:
|
||||
get_effective_settings(app),
|
||||
app.state.http_session,
|
||||
config,
|
||||
run_import_with_resources,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user