Files
romm/backend/tests/tasks/test_task_func_paths.py
Georges-Antoine Assi ae46dc64c2 fix(tasks): make the upload tmp cleanup importable by the RQ worker
Importing the task module first, as the worker does when it resolves the job,
pulled endpoints.roms in and closed an import cycle through decorators.database.
RQ hides the ImportError behind "Invalid attribute name: run", so the cleanup
never ran and orphaned chunked-upload directories piled up.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-28 18:41:51 -04:00

48 lines
1.4 KiB
Python

import os
import pkgutil
import subprocess
import sys
from importlib import import_module
from pathlib import Path
import pytest
import tasks
from tasks.tasks import PeriodicTask
BACKEND_ROOT = Path(__file__).resolve().parents[2]
def _periodic_task_funcs() -> list[str]:
funcs = set()
for module_info in pkgutil.walk_packages(tasks.__path__, prefix="tasks."):
module = import_module(module_info.name)
for value in vars(module).values():
if isinstance(value, PeriodicTask):
funcs.add(value.func)
return sorted(funcs)
@pytest.mark.parametrize("func", _periodic_task_funcs())
def test_task_func_resolves_in_a_fresh_interpreter(func):
"""The RQ worker resolves a job by importing its func path into a process
where the task module is the first application module imported. An import
cycle that stays hidden in the web process breaks the job there, so resolve
each func the way the worker does, in a clean interpreter."""
result = subprocess.run(
[
sys.executable,
"-c",
"import sys\n"
"from rq.utils import import_attribute\n"
"assert callable(import_attribute(sys.argv[1]))\n",
func,
],
cwd=BACKEND_ROOT,
env={**os.environ, "PYTHONPATH": str(BACKEND_ROOT)},
capture_output=True,
text=True,
)
assert result.returncode == 0, f"{func} is not importable by RQ:\n{result.stderr}"