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>
This commit is contained in:
Georges-Antoine Assi
2026-09-03 07:36:51 -04:00
parent 24cf0a47c3
commit 12b67f1c2c
8 changed files with 30 additions and 45 deletions

View File

@@ -41,8 +41,7 @@ router = APIRouter(
# Scheduled tasks an admin can see and trigger. The rest of the catalog runs on
# its schedule without being surfaced. Resolved here so a registry rename fails
# at import rather than on a request.
# its schedule without being surfaced.
VISIBLE_SCHEDULED_TASKS: Final[dict[str, Task]] = {
name: SCHEDULED_TASKS[name]
for name in (

View File

@@ -110,10 +110,8 @@ def get_job_kwargs(job: Job) -> dict[str, Any] | None:
def get_worker_current_job(worker: Worker) -> Job | None:
"""Safely get the job a worker is holding.
A worker killed mid-job keeps pointing at it until its own registration
expires, by which time the job can be gone.
"""Safely get the job a worker is holding, which can be gone before the
worker's own registration expires.
Args:
worker: The RQ Worker to read

View File

@@ -1,8 +1,4 @@
"""Finding and pruning the RQ jobs that run a library scan.
Kept out of the socket endpoint so the watcher and the startup script can ask
about scans without importing the scan implementation and everything under it.
"""
"""Finding and pruning the RQ jobs that run a library scan."""
from datetime import datetime, timedelta, timezone
from itertools import chain
@@ -23,9 +19,8 @@ from handler.redis_handler import (
from logger.logger import log
from tasks.tasks import TaskType
# The name RQ records for a directly enqueued scan. Spelled out rather than
# derived from the function, which lives in the module that imports this one;
# a test holds the two together.
# The name RQ records for a directly enqueued scan, kept in step with the
# function by a test rather than an import, which would be a cycle.
SCAN_PLATFORMS_FUNC: Final = "endpoints.sockets.scan.scan_platforms"
# A delayed watcher scan this far past due was left behind by an instance that
@@ -36,9 +31,8 @@ STALE_SCHEDULED_SCAN_AGE: Final = timedelta(hours=1)
def is_scan_job(job: Job) -> bool:
"""Whether this job runs a scan.
Socket and watcher scans enqueue scan_platforms itself. Task-driven scans go
through the task runner, which every task shares, so they are recognised by
the type their job carries instead.
Task-driven scans carry the task runner's func name, which every task
shares, so those are recognised by the type in their meta instead.
"""
if get_job_func_name(job) == SCAN_PLATFORMS_FUNC:
return True
@@ -117,8 +111,7 @@ def drop_stale_scheduled_scans() -> int:
registry = _scheduled_scan_registry()
cutoff = datetime.now(timezone.utc) - STALE_SCHEDULED_SCAN_AGE
# The registry is scored by due time, so it can hand back only what is past
# due rather than every delayed job.
# The registry is scored by due time, so it can hand back only what is due.
stale_ids = registry.get_jobs_to_schedule(int(cutoff.timestamp()))
jobs = Job.fetch_many(stale_ids, connection=redis_client)
dropped = 0
@@ -129,6 +122,8 @@ def drop_stale_scheduled_scans() -> int:
job.cancel()
dropped += 1
log.warning(f"Dropped scan {job.id}, due over {STALE_SCHEDULED_SCAN_AGE} ago")
log.warning(
f"Dropped scan {job.id}, overdue by more than {STALE_SCHEDULED_SCAN_AGE}"
)
return dropped

View File

@@ -58,11 +58,7 @@ def _enqueue_backfill(task_name: str, job_id: str) -> None:
def _enqueue_recompute_save_hashes_if_needed() -> None:
"""Backfill content_hash for saves uploaded before the path-resolution fix.
A single COUNT query gates it, so a run that already completed costs nothing
on later restarts.
"""
"""Backfill content_hash for saves uploaded before the path-resolution fix."""
try:
missing = db_save_handler.count_saves_missing_content_hash()
except Exception:
@@ -83,10 +79,8 @@ def _enqueue_recompute_save_hashes_if_needed() -> None:
def _enqueue_convert_images_to_webp() -> None:
"""Backfill .webp covers when WebP conversion is enabled.
The frontend rewrites cover URLs to .webp as soon as the feature flag is on,
but the scheduled task only runs at its next cron time and the inline
conversion in the resources handler only fires for covers fetched after
enabling, so without this every existing cover 404s until then.
The frontend rewrites cover URLs to .webp as soon as the flag is on, so
without this every cover fetched before it 404s until the next cron run.
"""
_enqueue_backfill("convert_images_to_webp", CONVERT_IMAGES_TO_WEBP_JOB_ID)

View File

@@ -65,7 +65,7 @@ def enqueue_task(
task_kwargs: dict[str, Any] | None = None,
**job_options: Any,
) -> Job:
"""Enqueue a registered task by name, on the queue a worker picks it up from.
"""Enqueue a registered task by name.
Args:
name: The key the task is registered under.

View File

@@ -6,8 +6,7 @@ import socketio
from rq.exceptions import InvalidJobOperation
from rq.job import JobStatus
from tests.scan_job_stubs import (
CLEANUP_FUNC,
SCAN_PLATFORMS_FUNC,
NON_SCAN_FUNC,
make_job,
make_task_job,
patch_scan_jobs,
@@ -37,6 +36,7 @@ from handler.filesystem.roms_handler import (
)
from handler.metadata.base_handler import UniversalPlatformSlug as UPS
from handler.scan_handler import MetadataSource, ScanType
from handler.scan_jobs import SCAN_PLATFORMS_FUNC
from models.firmware import Firmware
from models.platform import Platform
from models.rom import Rom
@@ -1834,10 +1834,10 @@ class TestScanConcurrency:
# Only scans block scans; a cleanup or metadata task must not.
patch_scan_jobs(
mocker,
running=make_job(CLEANUP_FUNC),
high_queued=[make_job(CLEANUP_FUNC)],
low_queued=[make_job(CLEANUP_FUNC)],
scheduled=[make_job(CLEANUP_FUNC, status=JobStatus.SCHEDULED)],
running=make_job(NON_SCAN_FUNC),
high_queued=[make_job(NON_SCAN_FUNC)],
low_queued=[make_job(NON_SCAN_FUNC)],
scheduled=[make_job(NON_SCAN_FUNC, status=JobStatus.SCHEDULED)],
)
enqueue = mocker.patch.object(scan_module.high_prio_queue, "enqueue")

View File

@@ -2,8 +2,7 @@ from datetime import datetime, timezone
from rq.job import JobStatus
from tests.scan_job_stubs import (
CLEANUP_FUNC,
SCAN_PLATFORMS_FUNC,
NON_SCAN_FUNC,
make_job,
make_task_job,
patch_scan_jobs,
@@ -11,6 +10,7 @@ from tests.scan_job_stubs import (
import handler.scan_jobs as scan_jobs
from endpoints.sockets.scan import scan_platforms
from handler.scan_jobs import SCAN_PLATFORMS_FUNC
def test_the_scan_func_name_matches_the_function():
@@ -29,7 +29,7 @@ class TestIsScanJob:
assert scan_jobs.is_scan_job(make_task_job())
def test_ignores_another_task(self):
assert not scan_jobs.is_scan_job(make_job(CLEANUP_FUNC))
assert not scan_jobs.is_scan_job(make_job(NON_SCAN_FUNC))
class TestGetQueuedScanJobs:
@@ -41,7 +41,7 @@ class TestGetQueuedScanJobs:
assert scan_jobs.get_queued_scan_jobs() == [high, low]
def test_leaves_out_jobs_that_are_not_scans(self, mocker):
patch_scan_jobs(mocker, high_queued=[make_job(CLEANUP_FUNC)])
patch_scan_jobs(mocker, high_queued=[make_job(NON_SCAN_FUNC)])
assert scan_jobs.get_queued_scan_jobs() == []
@@ -113,7 +113,7 @@ class TestDropStaleScheduledScans:
job.cancel.assert_not_called()
def test_leaves_jobs_that_are_not_scans_alone(self, mocker):
job = make_job(CLEANUP_FUNC, status=JobStatus.SCHEDULED)
job = make_job(NON_SCAN_FUNC, status=JobStatus.SCHEDULED)
registry = patch_scan_jobs(mocker, scheduled=[job])
registry.get_jobs_to_schedule.return_value = [job.id]

View File

@@ -7,11 +7,10 @@ from rq.exceptions import NoSuchJobError
from rq.job import Job, JobStatus
import handler.scan_jobs as scan_jobs_module
from tasks.tasks import TaskType
from tasks.tasks import TaskType, run_task_by_name
SCAN_PLATFORMS_FUNC = "endpoints.sockets.scan.scan_platforms"
TASK_RUNNER_FUNC = "tasks.tasks.run_task_by_name"
CLEANUP_FUNC = "tasks.scheduled.cleanup_zip_cache.cleanup_zip_cache_task.run"
TASK_RUNNER_FUNC = f"{run_task_by_name.__module__}.{run_task_by_name.__name__}"
NON_SCAN_FUNC = "tasks.tasks.not_a_scan"
_job_ids = count()