Files
romm/backend/tests/tasks/test_cleanup_missing_firmware.py
Claude 1e6dd1a2ac perf: keep task code out of the cron scheduler and idle RQ workers
The registry imported every task module to read each task's title,
schedule, queue and timeout, so `rq cron` and every RQ worker parent
loaded the whole app. Task metadata now lives in the registry as
`TaskSpec`s with a dotted path to the implementation, which `get_task`
imports only when a job runs. Cron, the tasks API, enqueueing and the
worker's failure reporting read specs only.

Every log line also imported handler.metadata.base_handler, and with
it every metadata provider, for the redaction regex. The sensitive keys
and their regex now live in logger.formatter.

A test pins that importing the cron config or the worker class loads
neither handler.database, the auth handlers nor fastapi.

Measured RSS at LOGLEVEL=INFO, master -> this branch:
- rq cron: 164 MB -> 72 MB
- idle RQ worker (x3): 133 MB -> 68 MB
- gunicorn web worker: 214 MB -> 206 MB

Each job's work horse now imports its task module itself, 0.1-0.5s
more per job on an x86 host.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gh1SRMfuK7BZmf6YxMqSyT
2026-10-01 01:32:21 +00:00

87 lines
3.1 KiB
Python

"""Tests for CleanupMissingFirmwareTask."""
import pytest
from tests.factories import make_firmware
from handler.database import db_firmware_handler
from tasks.manual.cleanup_missing_firmware import (
CleanupMissingFirmwareTask,
cleanup_missing_firmware_task,
)
class TestCleanupMissingFirmwareTask:
@pytest.fixture
def task(self) -> CleanupMissingFirmwareTask:
return CleanupMissingFirmwareTask()
def test_module_singleton_exists(self):
assert isinstance(cleanup_missing_firmware_task, CleanupMissingFirmwareTask)
def test_configuration(self, task):
assert task.spec.enabled is True
assert task.spec.manual_run is True
assert task.spec.can_run_manually is True
assert task.spec.cron_string is None
async def test_deletes_only_missing_firmware(self, task, platform):
present = make_firmware(platform, "present.bin")
gone = make_firmware(platform, "gone.bin", missing=True)
stats = await task.run()
assert stats["firmware_found"] == 1
assert stats["firmware_deleted"] == 1
assert stats["errors"] == 0
assert db_firmware_handler.get_firmware(gone.id) is None
assert db_firmware_handler.get_firmware(present.id) is not None
async def test_scopes_to_the_given_platforms(self, task, platform, other_platform):
mine = make_firmware(platform, "gone.bin", missing=True)
theirs = make_firmware(other_platform, "other-gone.bin", missing=True)
stats = await task.run(platform_ids=[platform.id])
assert stats["platform_ids"] == [platform.id]
assert stats["firmware_deleted"] == 1
assert db_firmware_handler.get_firmware(mine.id) is None
assert db_firmware_handler.get_firmware(theirs.id) is not None
async def test_scopes_to_several_platforms_at_once(
self, task, platform, other_platform
):
mine = make_firmware(platform, "gone.bin", missing=True)
theirs = make_firmware(other_platform, "other-gone.bin", missing=True)
stats = await task.run(platform_ids=[platform.id, other_platform.id])
assert stats["firmware_deleted"] == 2
assert db_firmware_handler.get_firmware(mine.id) is None
assert db_firmware_handler.get_firmware(theirs.id) is None
async def test_counts_delete_failures(self, task, platform, mocker):
make_firmware(platform, "gone-a.bin", missing=True)
make_firmware(platform, "gone-b.bin", missing=True)
mocker.patch(
"tasks.manual.cleanup_missing_firmware.db_firmware_handler.delete_firmware",
side_effect=RuntimeError("boom"),
)
stats = await task.run()
assert stats["firmware_found"] == 2
assert stats["firmware_deleted"] == 0
assert stats["errors"] == 2
async def test_no_missing_firmware_is_a_no_op(self, task, platform):
make_firmware(platform, "present.bin")
stats = await task.run()
assert stats == {
"platform_ids": None,
"firmware_found": 0,
"firmware_deleted": 0,
"errors": 0,
}