Files
romm/backend/tasks/update_switch_titledb.py
Michael Manganiello fe5759fec7 fix: Initialize context on scheduled task
Only for this scheduled task, the run was failing because there was no
context initialization that created the httpx session.

Other schedules work as expected, because they do have context
initialization in the functions they call to (e.g. `scan_platforms`).
2024-07-27 01:22:29 -03:00

55 lines
2.0 KiB
Python

import json
from typing import Final
from config import (
ENABLE_SCHEDULED_UPDATE_SWITCH_TITLEDB,
SCHEDULED_UPDATE_SWITCH_TITLEDB_CRON,
)
from handler.redis_handler import async_cache
from logger.logger import log
from tasks.tasks import RemoteFilePullTask
from utils.context import initialize_context
from utils.iterators import batched
SWITCH_TITLEDB_INDEX_KEY: Final = "romm:switch_titledb"
SWITCH_PRODUCT_ID_KEY: Final = "romm:switch_product_id"
class UpdateSwitchTitleDBTask(RemoteFilePullTask):
def __init__(self):
super().__init__(
func="tasks.update_switch_titledb.update_switch_titledb_task.run",
description="switch titledb update",
enabled=ENABLE_SCHEDULED_UPDATE_SWITCH_TITLEDB,
cron_string=SCHEDULED_UPDATE_SWITCH_TITLEDB_CRON,
url="https://raw.githubusercontent.com/blawar/titledb/master/US.en.json",
)
@initialize_context()
async def run(self, force: bool = False) -> None:
content = await super().run(force)
if content is None:
return
index_json = json.loads(content)
relevant_data = {k: v for k, v in index_json.items() if k and v}
async with async_cache.pipeline() as pipe:
for data_batch in batched(relevant_data.items(), 2000):
titledb_map = {k: json.dumps(v) for k, v in dict(data_batch).items()}
await pipe.hset(SWITCH_TITLEDB_INDEX_KEY, mapping=titledb_map)
for data_batch in batched(relevant_data.items(), 2000):
product_map = {
v["id"]: json.dumps(v)
for v in dict(data_batch).values()
if v.get("id")
}
if product_map:
await pipe.hset(SWITCH_PRODUCT_ID_KEY, mapping=product_map)
await pipe.execute()
log.info("Scheduled switch titledb update completed!")
update_switch_titledb_task = UpdateSwitchTitleDBTask()