* 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>
* perf(rq): keep SQLAlchemy out of the RQ worker and cron processes
The RQ worker parents and rq cron imported SQLAlchemy and the ORM models at
start-up, through config/__init__.py (safe_* helpers in utils.database) and
config_manager (sqlalchemy.URL and the rom-converto adapter). None of them
query the database themselves; each job's work horse loads what it needs.
- Move safe_int, safe_str_to_bool and get_nested from utils.database to utils.
- Import sqlalchemy.URL inside get_db_engine.
- Import the rom-converto normalizer only when converto.platform_formats is set.
- Add a test that tasks.cron_config and handler.rq_worker start without
loading SQLAlchemy.
- Run a killed work horse's failure callback and exception handlers in a
short-lived child, so the job code they import never stays in the parent.
Measured on the production image: idle PSS of rq_worker, rq_scan_worker and
rq_cron fell by 98 MB in total.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* perf(rq): move to constants file, tests prevent huge imports
* perf(rq) revert forked failure report.
* pr-ready: code-review
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* pr-ready: simplify
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* pr-ready: review-polish
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* pr-ready: plain-prose
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* refactor(db): build the database URL in utils.database
ConfigManager.get_db_engine becomes utils.database.get_db_url, so
config_manager no longer needs SQLAlchemy at all and the function-local
import goes away. Every caller already loads SQLAlchemy.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(roms): clear a match id that is not a number instead of storing 0
safe_int_or_none fell back to safe_int's default of 0, so sgdb_id,
hasheous_id and tgdb_id (which have no refetch to reset them) were saved
as 0. Use utils.int_or_none instead.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* docs(models): describe where each shared column limit is used
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
report_task_failure can run in the worker parent when a work horse is
killed, so it now enqueues notify_task_failure on the default queue
instead of loading the notification stack there.
enqueue_task defaults to the queue its spec names. The streaming worker
runs as PreloadingWorker, which imports the enabled scheduled tasks of
its queues, so the reaper's per-minute horse inherits its stack. Spec
tests now read config in a fresh interpreter instead of comparing a
value with itself.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Loads every model before a job resolves its task, so the mappers
configure in a work horse that imported a single task module. Moves the
httpx2 import into RemoteFilePullTask.run, builds the push-pull job meta
from its spec, gates the scheduled scan on its spec, and widens the
loaded-modules test to cover task modules and httpx2.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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
Master landed #4274 with the review changes that came out of it, and those
moved the code this branch was built on. Scan job bookkeeping now lives in
`handler/scan_jobs.py`, every enqueue goes through `enqueue_task`, cron
entries carry their own name and result TTL, and the watcher builds its two
rescans from one helper.
Resolved by putting this branch's changes where that code now lives:
- The scan queue, the scoped-scan check and the second scheduled registry
moved into `handler/scan_jobs.py`, keeping master's single-round-trip
reads (`get_job_ids` plus one `fetch_many`, and `get_jobs_to_schedule`
for the stale sweep) across both registries.
- `enqueue_scheduled_scan` sits next to `enqueue_task` in the registry and
is built from it, rather than hand-rolling the same enqueue in
`tasks/tasks.py`. Its tests moved with it.
- Cron keeps master's per-entry name and result TTL, and adds the scan
dispatch on top: `TASK_TIMEOUT` for the dispatch, the task's own timeout
(now `SCAN_TIMEOUT`) for the scan it enqueues.
- The watcher's `schedule_rescan` helper enqueues on the scan queue with
the abandoned-job callback.
- `PeriodicTask.func` is gone from master, so the scheduled rescan declares
only its timeout. `tasks.registry.enqueue_scheduled_scan` joins the func
paths an RQ worker has to be able to import.
- The job stubs shared by the discovery tests grew the scan queue and a
scoped-scan job; the duplicated stale-sweep tests are master's.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Scheduling moved onto RQ, which left the task catalog with two identity
schemes: the registry key a job payload now carries, and the dotted func
path each PeriodicTask still declared. Nothing read the latter, so it is
gone, along with the test that guarded paths no payload names.
Four sites built the same enqueue by hand and had already drifted: the
startup backfills passed the global timeout and no result_ttl, so an
auto-enqueued conversion fell out of the task list after RQ's default
500s while the same task triggered by an admin lived for TASK_RESULT_TTL.
They now go through enqueue_task, which derives timeout, ttl and meta
from the task itself.
Scan discovery paid a Redis round trip per queued job to re-read a status
the fetch already carried, and the stale sweep read every delayed job to
find the few past due. Both now ask Redis once.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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>