* Add a REST endpoint to start a library scan
POST /api/tasks/scan queues a scan with the options of the `scan` socket
event, so clients authenticating with a bearer or client token can
start one without a Socket.IO session. It answers 202 with the queued job,
409 while a library scan is queued or running, and 503 when no worker
listens on the scan queue.
The enqueue path moves into queue_scan, shared with the socket handler.
A short Redis lock now covers the in-flight check and the enqueue, so two
requests landing on different web workers cannot both queue a library
scan. The socket handler also answers scan:done_ko for an unknown scan
type instead of raising.
* Default a scan without apis to the enabled sources, lease the request lock
A scan request that leaves out `apis` now uses every enabled metadata
source, on the REST endpoint and the socket alike, as the scheduled scan
and the watcher do. An explicit empty list still means no sources. The
web UI always sends `apis`, so its scans are unchanged.
redis_lock takes an optional lease_seconds, and the library scan request
lock holds its key for 30s while waiters give up after 5s, so a slow
holder cannot lose the lock to a waiter that then queues a second scan.
* Narrow optional mock call args in the new scan tests
* pr-ready step 2 (code-review): narrow the scan lock timeout handling to acquiring the lock
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* pr-ready step 3 (simplify): share the held-lock stub, read the task name off the queued job
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* pr-ready step 4 (review-polish): drop a test comment that recorded old behaviour
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* Validate socket scan options with ScanPayload and list workers once per scan request
The socket handler now parses its options with the same ScanPayload the REST
endpoint uses, so an unknown key, scan type or metadata source answers
scan:done_ko instead of falling back to a scan of everything. v1 callers that
could send a null platform id now send a real one or none.
queue_scan checks for a live scan worker itself, so the socket also refuses a
scan no worker would pick up, and it lists the workers once for that check and
the in-flight check. The blocking RQ calls run in a thread, off the event loop.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* Rename useScanProviders' ScanPayload to ScanSourceOptions
It shared its name with the generated ScanPayload request type while holding
only the metadata source options.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
pr-ready step 2 (code-review xhigh --fix): byte-check screenshot, rename
and ROM fs_name paths, fit streaming save archive names, cap email and
collection names, log duplicate collections at warning.
A duplicate collection name raised a 500. Names compare
case-insensitively in the database, so "TRUE" collides with "True".
Return 409 instead, as the existing test's name already said.
Memory card name and emulator, and the RetroAchievements username,
had no length check before the insert, so an overlong value failed in
the database with a 500. Validate them against their column lengths,
which returns 422.
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>
rq-scheduler has one release since 2023, 109 open issues, and RomM ran a
fork of it for Redis username and SSL options that upstream never took.
Everything it was here for now ships with RQ.
Its two failure modes were the reason for the workarounds in 9e9f59c04.
It reads a job's function name before taking the job out of its registry,
so one unreadable job stalls it for good; RQ's scheduler removes every id
it looked at whether or not the job could be read. And cancelling a
delayed job needed both `Scheduler.cancel` and `Job.cancel`, because the
first left the status alone and the second left the registry entry, which
the scheduler then queued anyway; `Job.cancel` on its own now clears the
scheduled registry.
Delayed jobs, which is how the watcher defers a rescan, go onto the queue
with `Queue.enqueue_in` and are released by the worker running with
`--with-scheduler`. Periodic tasks are declared in `tasks/cron_config.py`
and loaded by an `rq cron` process, replacing `rqscheduler`. A task is
registered only when it is enabled and has a cron string, so the
schedule-then-unschedule dance around the env toggles is gone, and with
it `PeriodicTask.init/schedule/unschedule`.
Jobs are enqueued by task name through `run_task_by_name`, resolved
against a catalog in `tasks/registry.py`. Nothing pickles a task instance
into Redis any more, which is what made a payload unreadable across
upgrades in the first place. The catalog also gives the tasks endpoint
one home for what a task is, rather than a list of its own.
Scan discovery keys off the type a job carries rather than the scheduled
rescan's function name, since every task now shares one entry point. That
also drops SCAN_LIBRARY_TASK_FUNC, which only existed to name a function
across an import cycle.
The watcher's dedupe worked on `job.args[0]` while scans are enqueued with
keyword arguments, so it never matched and every filesystem change
scheduled another scan. It reads `platform_ids` from the payload now, and
deliberately ignores a scan already running: that one may have walked past
the folder that just changed.
Startup clears what the old scheduler left in Redis, once.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Spotify-style library features for soundtrack tracks: per-user favorites
(liked songs) and user-owned playlists with manual ordering and optional
public read-only sharing.
Track references are stored as (rom_id, md5_hash) rather than rom_file_id:
scans purge and re-insert rom_files rows, so file ids churn while rom ids
and content hashes are stable. Entries survive rescans and renames, and
resolve to the current file at query time; entries whose content is gone
are omitted from listings until the file returns.
To make the content hash reliably present, soundtrack audio files are now
always hashed individually during scans (including NON_HASHABLE_PLATFORMS
and SKIP_HASH_CALCULATION, whose file-size concerns don't apply to audio),
and soundtrack uploads compute crc/md5/sha1 on write. Existing libraries
backfill via a HASHES scan; adding an unhashed track returns 400.
New playlists.read/playlists.write scopes are wired through the granular
permission system (PermEntity.PLAYLISTS); the migration mirrors every
group's collections grants onto the playlists entity so existing groups
keep working without admin intervention.
Endpoints under /api/music: favorites GET/POST/DELETE, is_favorite and
md5_hash on track rows, playlists CRUD, playlist tracks GET/POST/DELETE
and PUT /tracks/order.
These critical log messages are unnecessary and can clutter the logs,
based on user requests. This is even more notorious when RomM is
integrated to an error tracking system like Sentry.
This change mainly refactors the `scan_platforms` function, moving part
of its logic to `_identify_platform`, `_identify_firmware`, and
`_identify_rom`.
The logic is simpler this way, each smaller function returns `ScanStats`
that can be merged by the caller, and it simplifies future performance
improvements.