mirror of
https://github.com/rommapp/romm.git
synced 2026-12-02 10:08:40 +01:00
* perf(streaming) lazy loading in streaming worker * perf(rq): push _resolved to test container * pr-ready: code-review Move the reaper import guard into test_task_func_paths.py, reusing BACKEND_ROOT and JOB_ONLY_MODULES, watch socket_handler and lifecycle too, check the deferred imports still load, and surface stderr on failure. Explain the lazy socket_handler import. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * pr-ready: simplify Defer the lifecycle import inside _reap instead of wrapping its teardown, share one subprocess helper between the import guards, and check every streaming-queue task the PreloadingWorker parent would load. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * pr-ready: review-polish Keep the import-guard helper's docstring to one sentence. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * pr-ready: prose Write the lazy-import and guard comments as full sentences. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(streaming): move session notices out of session_store record_termination and push_to_user deliver over the socket and as notifications, so they now live in handler/streaming/notices.py with top-level imports. session_store keeps only the Redis tombstone write (save_termination) and loads neither the database nor socketio. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
134 lines
4.3 KiB
Python
134 lines
4.3 KiB
Python
import os
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
BACKEND_ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
# The only callables a job payload names. Everything else runs through
|
|
# run_task_by_name, which resolves it from the registry.
|
|
JOB_FUNC_PATHS = (
|
|
"tasks.tasks.run_task_by_name",
|
|
"tasks.tasks.notify_task_failure",
|
|
"endpoints.sockets.scan.scan_platforms",
|
|
"tasks.registry.enqueue_scheduled_scan",
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("func", JOB_FUNC_PATHS)
|
|
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 that module is the first application module imported. An import cycle
|
|
that stays hidden in the web process breaks the job there."""
|
|
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}"
|
|
|
|
|
|
def _loaded_after(
|
|
setup: str, watched: tuple[str, ...], base_path: Path, then: str = ""
|
|
) -> str:
|
|
"""List the `watched` modules a fresh interpreter loads for `setup`, then run `then`."""
|
|
result = subprocess.run(
|
|
[
|
|
sys.executable,
|
|
"-c",
|
|
"import sys\n"
|
|
f"{setup}\n"
|
|
"watched = sys.argv[1:]\n"
|
|
"loaded = ','.join(m for m in sys.modules\n"
|
|
" if any(m == w or m.startswith(w + '.') for w in watched))\n"
|
|
f"{then}\n"
|
|
"print('LOADED:' + loaded)\n",
|
|
*watched,
|
|
],
|
|
cwd=BACKEND_ROOT,
|
|
env={
|
|
**os.environ,
|
|
"PYTHONPATH": str(BACKEND_ROOT),
|
|
"ROMM_BASE_PATH": str(base_path),
|
|
# A developer's .env could otherwise turn telemetry on.
|
|
"OTEL_SDK_DISABLED": "true",
|
|
},
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
|
|
assert result.returncode == 0, result.stderr
|
|
return result.stdout.rpartition("LOADED:")[2].strip()
|
|
|
|
|
|
# Loaded only by the jobs that need them, so the long-lived scheduler and
|
|
# worker parents stay small. Each name covers its submodules too.
|
|
HEAVY_MODULES = (
|
|
"tasks.manual",
|
|
"tasks.sync_push_pull_task",
|
|
"handler.database",
|
|
"handler.auth.base_handler",
|
|
"fastapi",
|
|
"httpx2",
|
|
"sqlalchemy",
|
|
)
|
|
JOB_ONLY_MODULES = ("tasks.scheduled", *HEAVY_MODULES)
|
|
|
|
|
|
@pytest.mark.parametrize("module", ["tasks.cron_config", "handler.rq_worker"])
|
|
# A converto.platform_formats setting also runs its validation while the config loads.
|
|
@pytest.mark.parametrize(
|
|
"config_yml", ["", "converto:\n platform_formats:\n psx: chd\n"]
|
|
)
|
|
def test_the_scheduler_and_worker_load_no_task_code(
|
|
module: str, config_yml: str, tmp_path: Path
|
|
) -> None:
|
|
(tmp_path / "config").mkdir()
|
|
(tmp_path / "config" / "config.yml").write_text(config_yml)
|
|
loaded = _loaded_after(f"import {module}", JOB_ONLY_MODULES, tmp_path)
|
|
|
|
assert loaded == "", f"{module} loads {loaded}"
|
|
|
|
|
|
# The streaming work horse loads these only when it tears down a session or sends a notice.
|
|
PRELOAD_DEFERRED_MODULES = (
|
|
*HEAVY_MODULES,
|
|
"handler.scan",
|
|
"handler.metadata",
|
|
"handler.streaming.lifecycle",
|
|
"handler.socket_handler",
|
|
"endpoints",
|
|
)
|
|
|
|
|
|
def test_the_streaming_worker_preloads_its_tasks_without_their_teardown(
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""The parent keeps what it preloads, and a work horse can still import the rest."""
|
|
# PreloadingWorker loads only the enabled tasks, and this loads all of them.
|
|
loaded = _loaded_after(
|
|
"import handler.rq_worker\n"
|
|
"from handler.redis_handler import STREAMING_QUEUE_NAME\n"
|
|
"from tasks.registry import SCHEDULED_TASKS, get_task\n"
|
|
"for name, spec in SCHEDULED_TASKS.items():\n"
|
|
" if spec.queue_name == STREAMING_QUEUE_NAME:\n"
|
|
" get_task(name)",
|
|
PRELOAD_DEFERRED_MODULES,
|
|
tmp_path,
|
|
then="import handler.streaming.lifecycle, handler.notification_handler",
|
|
)
|
|
|
|
assert loaded == "", f"the streaming preload loads {loaded}"
|