Commit Graph

33 Commits

Author SHA1 Message Date
Georges-Antoine Assi
e0f0cec435 style(scan): trim the comments to what the code cannot say
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>
2026-09-03 13:42:20 -04:00
Georges-Antoine Assi
dc7d14f31d refactor(scan): let RQ discard the scan dispatch, and say the guard once
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>
2026-09-03 12:29:58 -04:00
Georges-Antoine Assi
ffc8dffc20 Merge upstream master into claude/rq-scan-queue-split
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>
2026-09-03 12:16:44 -04:00
Georges-Antoine Assi
92c25d8db3 fix(tasks): give each cron entry its own identity and result ttl
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>
2026-09-03 08:11:02 -04:00
Georges-Antoine Assi
12b67f1c2c chore(tasks): trim the comments down to what the code cannot say
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>
2026-09-03 07:36:51 -04:00
Georges-Antoine Assi
787bbba2b4 refactor(tasks): give every enqueue one home
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>
2026-09-02 21:03:30 -04:00
Sam Dornan
3593d2398a perf(scan): give scans their own queue and worker
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>
2026-09-02 15:02:16 -05:00
Sam Dornan
4e59217275 refactor(tasks): schedule with RQ instead of rq-scheduler
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>
2026-09-02 15:02:04 -05:00
Sam Dornan
9f1f97d301 fix(scan): stop refusing scans a stalled scheduler left pending
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>
2026-09-02 15:02:04 -05:00
Georges-Antoine Assi
5c0bd9c418 [ROMM-2657] Safe access env vars with defaults 2025-11-15 14:54:21 -05:00
Georges-Antoine Assi
acad44aa43 fixes from bot reivew 2025-11-10 18:05:42 -05:00
Georges-Antoine Assi
b2dea510c4 [ROMM-2628] Fix desirialize job func_name 2025-11-10 17:57:28 -05:00
Michael Manganiello
e4e3928d1b misc: Apply import sorting 2025-09-04 11:17:00 -03:00
zurdi
53f9386208 refactor: enhance logging messages and formatting for improved clarity and consistency 2025-05-08 23:35:33 +00:00
zurdi
d5bb63c06f refactor: improve logging in redis_handler and init scripts for better debugging and process management 2025-05-08 16:34:23 +00:00
Michael Manganiello
b81fbc5820 fix: Improve handling of Redis URL
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.
2024-10-11 21:41:19 -03:00
Georges-Antoine Assi
8dae0a503c add simple emojis to cards 2024-09-08 13:59:10 -04:00
Michael Manganiello
749e4d65c1 misc: Use PYTEST_VERSION variable to detect Pytest runs
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
2024-07-27 12:03:44 -03:00
Michael Manganiello
6eb8e6aeff misc: Add Redis async cache
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.
2024-07-21 18:23:19 -03:00
Georges-Antoine Assi
92d5c60f43 run trunk fmt 2024-06-23 15:09:55 +00:00
Michael Manganiello
ed635d69cc misc: Move fakeredis to test dependencies
`fakeredis` is only needed when running tests, so we can move it to the
`test` dependency group.
2024-06-23 11:10:25 -03:00
Georges-Antoine Assi
a7cf0d389a run trunk format on all files 2024-05-21 10:18:13 -04:00
Georges-Antoine Assi
880345f20c ruff-only pre-commit 2024-05-18 12:05:57 -04:00
HellLord
98f60fb001 adds more configuration for external redis
add env vars: REDIS_USERNAME, REDIS_DB

Co-Authored-By: Zurdi <34356590+zurdi15@users.noreply.github.com>
Co-Authored-By: Georges-Antoine Assi <3247106+gantoine@users.noreply.github.com>
2024-04-14 22:01:31 +06:00
Georges-Antoine Assi
fd196165b8 add fake redis for tests 2024-03-26 20:37:38 -04:00
Georges-Antoine Assi
4415884608 add fallback cache entries for hget and hset 2024-03-26 19:51:26 -04:00
Georges-Antoine Assi
41d0d2eb46 more dir creating up in dockerfile + add redis password 2024-03-25 15:30:27 +00:00
Georges-Antoine Assi
a36d49979e [WIP] build redis into container
expose port right
2024-03-01 10:36:02 -05:00
Georges-Antoine Assi
1c8b86c664 More cleanup of docker-compose 2024-03-01 09:59:02 -05:00
Georges-Antoine Assi
bf48b9037f simplify example compose 2024-02-02 00:03:30 -05:00
Georges-Antoine Assi
6bcadec702 everything we need for 3.0 2024-02-01 18:13:16 -05:00
Zurdi
0fe8bf6a74 redis scan fixed 2024-01-17 01:47:22 +01:00
Zurdi
4b9e76f550 backend structure refactor 2024-01-12 23:44:27 +01:00