* 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>
`has_live_worker` asks RQ for the worker count and the suspension flag,
two constant-cost reads, instead of hydrating every registered worker.
The cleanup stats serialize through `dataclasses.asdict`, so a new
counter is declared in two places rather than three. The task endpoint
test takes the shared task fixture, the legacy-stats shim says when it
can go, and the two Settings specs share one axios-error helper under
`src/test-utils` while the games spec starts from a non-empty list.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9ta8rXUTP9aMdatPYKX4K
Follow-up to #4548 from the code-review pass. Stats a 5.2.0 release
stored under `platform_id` are widened to `platform_ids` when a job is
reported, so listing old finished cleanups validates again. The live
worker check moves next to the queues as `has_live_worker`, with its
own tests, and the endpoint names the queue it enqueues on so the check
and the enqueue cannot drift apart. The cleanup tasks return the typed
stats the response schemas declare. The developer setup points the RQ
commands at the backend's own Redis settings through `-c config`, and
the architecture doc mentions the 503.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9ta8rXUTP9aMdatPYKX4K
rq.cron names an entry after its function unless told otherwise, and
every entry here runs the task runner, so all of them shared one cron
identity and one job history zset. It also always passes a result_ttl,
so leaving it out meant its 500s default rather than the worker's
setting: a scheduled run vanished from the task list eight minutes after
finishing while a manual run of the same task was kept for a day.
Stopping a scan twice raised out of the socket handler. The first Stop
marks the job cancelled but the worker holds it until it unwinds, so the
second reaches it again and RQ refuses to cancel a cancelled job.
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>
A scan can be scoped by platform id, by folder slug for a directory with
no database row, or by rom. Treating anything without platform ids as a
scan of the whole library meant a queued slug-scoped or rom-scoped scan
suppressed every rescan the watcher would otherwise schedule.
Summarise what the pending scans cover instead, and match a changed
folder against the slugs as well as the ids. A scan whose payload cannot
be read is left out: its scope is unknowable, and a duplicate scan costs
less than a rescan that never happens.
Adds the tests this file never had, plus tests for the job accessors that
tolerate an unreadable payload. Four of them fail against the previous
scope check.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>