Files
romm/backend/tasks/manual/cleanup_missing_firmware.py
T
zurdiandClaude Fable 5.1 be81b1c89b refactor(tasks): apply the simplify pass
`has_live_worker` asks RQ for the worker count and the suspension flag,
two constant-cost reads, instead of hydrating every registered worker.
The cleanup stats serialize through `dataclasses.asdict`, so a new
counter is declared in two places rather than three. The task endpoint
test takes the shared task fixture, the legacy-stats shim says when it
can go, and the two Settings specs share one axios-error helper under
`src/test-utils` while the games spec starts from a non-empty list.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9ta8rXUTP9aMdatPYKX4K
2026-09-16 16:16:00 +02:00

87 lines
2.9 KiB
Python

from dataclasses import asdict, dataclass
from typing import cast
from endpoints.responses import MissingFirmwareCleanupStats
from handler.database import db_firmware_handler
from logger.logger import log
from tasks.tasks import Task, TaskType, update_job_meta
from utils.context import initialize_context
@dataclass
class CleanupMissingFirmwareStats:
"""Statistics for missing firmware cleanup operations."""
platform_ids: list[int] | None = None
firmware_found: int = 0
firmware_deleted: int = 0
errors: int = 0
def update(self, **kwargs) -> None:
for key, value in kwargs.items():
if hasattr(self, key):
setattr(self, key, value)
update_job_meta({"cleanup_stats": self.to_dict()})
def to_dict(self) -> MissingFirmwareCleanupStats:
return cast(MissingFirmwareCleanupStats, asdict(self))
class CleanupMissingFirmwareTask(Task):
def __init__(self):
super().__init__(
title="Cleanup missing firmware",
description="Delete all firmware flagged as missing from the filesystem from the database",
task_type=TaskType.CLEANUP,
enabled=True,
manual_run=True,
cron_string=None,
)
@initialize_context()
async def run(
self, platform_ids: list[int] | None = None
) -> MissingFirmwareCleanupStats:
"""Clean up firmware that is flagged as missing from the filesystem."""
log.info(f"Starting {self.title} task...")
stats = CleanupMissingFirmwareStats(platform_ids=platform_ids)
missing_firmware = db_firmware_handler.list_firmware(
platform_ids=platform_ids, missing=True
)
stats.update(firmware_found=len(missing_firmware))
log.info(
f"Found {len(missing_firmware)} missing firmware file(s) to clean up"
+ (
f" for platform ID(s) {', '.join(map(str, platform_ids))}"
if platform_ids
else ""
)
)
# The row is stale because the file is already gone, so there is
# nothing to remove from disk here.
for firmware in missing_firmware:
try:
log.info(
f"Deleting missing firmware '{firmware.file_name}' [ID: {firmware.id}] from database"
)
db_firmware_handler.delete_firmware(firmware.id)
except Exception as e:
log.error(f"Failed to delete missing firmware {firmware.id}: {e}")
stats.update(errors=stats.errors + 1)
continue
stats.update(firmware_deleted=stats.firmware_deleted + 1)
log.info(
f"Cleanup of missing firmware completed: {stats.firmware_deleted} deleted, {stats.errors} error(s)"
)
return stats.to_dict()
cleanup_missing_firmware_task = CleanupMissingFirmwareTask()