Cuts the rationale that had grown past two lines, and drops the wording
that described the change rather than the behaviour: a scan on one of the
other queues is one an older release enqueued, not one from "before scans
had their own queue".
The result_ttl note moves onto the argument it explains, and in the
entrypoint the log line moves next to the call it announces rather than
sitting above the function definition.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The dispatch job was hidden from the task list by a meta flag the endpoint
had to look for on every job in every queue and registry. RQ already has
the rule the flag was implementing: `result_ttl=0` deletes a job the moment
it succeeds, while a failed one still lands in the failed registry under
its own TTL. Registering the scan entry with it drops the marker, the
predicate, its four filter sites and the two Redis reads each one cost.
The concurrency guard is now one call. `get_blocking_library_scans()` says
which scans a second one has to wait for, so the socket handler no longer
assembles that rule out of two helpers in two shapes, and `ALL_QUEUES` sits
with the queues it lists rather than in the API layer.
Also merges the finished and failed registry sweeps, which were the same
loop, trims comments that explained the design rather than the code, and
drops two socket tests that duplicated their neighbours.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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>
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>
Also stops the scan job test stubs keeping their own copy of the func
name the handler already declares.
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 library scan runs for hours on the same worker as everything else, so
every cleanup, conversion and metadata refresh waited behind it. Scans go
on a queue of their own, consumed by a second worker, and the queues that
serve the rest of the app keep moving while a scan runs.
One worker on one queue also makes "one scan at a time" structural rather
than something the socket handler has to enforce, and it leaves another
worker running to notice when the scan worker dies.
Which is what the second half of this does. A worker killed mid-scan
never reaches the handler that reports the scan ended, so clients kept
showing a scan that no longer existed. RQ hands an abandoned job to its
failure callback when a surviving worker reaps it, so scans carry one
that emits `scan:done_ko`. Only for AbandonedJobError: every other
failure is already reported by the scan as it unwinds, and emitting here
too would report it twice. The scheduled rescan is not covered, because
rq cron cannot attach callbacks.
Scan discovery still reads the other queues, so a scan enqueued before
this change is still found and still runs.
Co-Authored-By: Claude Opus 5 <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>
Every scan request has been refused with "A scan is already in progress"
on instances whose rq-scheduler cannot make progress, and waiting does
not help because nothing is running to wait for.
The scheduler reads a job's function name before it takes the job out of
its registry, so one job left behind by an older version, holding an
argument that no longer deserializes, crashes it on every poll and stays
there for the next one to trip on. Nothing scheduled runs again, and the
watcher's delayed rescans pile up behind it. `_get_queued_scan_jobs`
counted those as scans waiting to start, so the guard added in 99e214a8b
refused every manual scan for as long as the scheduler stayed broken.
Clear jobs the scheduler cannot read at startup, before it polls them
again, and split scan discovery so the guard consults only what sits on
a worker queue. Delayed scans still block nothing: the scheduler is the
only thing that releases them, so one that is down must not be able to
refuse scans. A recovered scheduler would release its whole backlog at
once and run the same library scan over and over, so delayed scans more
than an hour past due go too.
Stopping a scan now drops delayed scans out of the scheduler's registry
rather than only cancelling the job, which left the id in the registry
for the scheduler to queue anyway once the delay was up.
A worker killed mid-scan points at a job that can already be gone, which
raised NoSuchJobError out of the socket handler with no reply to the
client, and out of GET /tasks/status as a 500. Reading a job's status has
the same problem once its hash expires, so it goes through a wrapper.
Finally, name the scan in the way when refusing, and say when it is
stopping rather than running, so the message is actionable.
Fixes#4186
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This change replaces the creation of Redis URL, from a simple string
interpolation, to using `yarl.URL`. The main benefit, besides not
forgetting to set all five different variables on every Redis client
initialization, is that user credentials are correctly URL-encoded, if
present.
Up until now, if a password had special characters, it could break the
generated URL.
This change also introduces support for a `REDIS_SSL` setting, which
allows the user to specify if the Redis connection should use SSL or not.
Pytest v8.2 introduced the `PYTEST_VERSION` environment variable [1],
that can be used to check if code is running from within a pytest run.
This way, we can avoid checking the loaded `sys` modules.
[1] https://docs.pytest.org/en/stable/changelog.html#id57
Introduce an asynchronous Redis instance to be used in async functions.
Also, this change migrates most of the sync cache usage to the new async
cache.