mirror of
https://github.com/rommapp/romm.git
synced 2026-12-04 12:52:19 +01:00
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>
138 lines
4.1 KiB
Python
138 lines
4.1 KiB
Python
from abc import ABC, abstractmethod
|
|
from enum import Enum
|
|
from typing import Any
|
|
|
|
import httpx
|
|
from rq import get_current_job
|
|
|
|
from config import TASK_TIMEOUT
|
|
from exceptions.task_exceptions import TaskNotFoundException
|
|
from logger.logger import log
|
|
from utils.context import ctx_httpx_client
|
|
|
|
# Marks the cron job that only enqueues a scan, so the task history can tell it
|
|
# apart from the scan it creates.
|
|
SCAN_DISPATCH_META_KEY = "scan_dispatch"
|
|
|
|
|
|
async def run_task_by_name(name: str, task_kwargs: dict[str, Any] | None = None) -> Any:
|
|
"""Run the task registered under ``name``.
|
|
|
|
Every scheduled and manually triggered task is enqueued through here, so a
|
|
job payload holds a name rather than a pickled task, and nothing in Redis
|
|
depends on where the code that runs it lives.
|
|
|
|
Args:
|
|
name: The key the task is registered under.
|
|
task_kwargs: Forwarded to the task's ``run``, nested so that they cannot
|
|
collide with the name of the task to run.
|
|
|
|
Returns:
|
|
Whatever the task returns.
|
|
"""
|
|
# Imported here because the registry imports every task module, and those
|
|
# modules import this one.
|
|
from tasks.registry import get_task
|
|
|
|
task = get_task(name)
|
|
if task is None:
|
|
raise TaskNotFoundException(name)
|
|
|
|
return await task.run(**(task_kwargs or {}))
|
|
|
|
|
|
def update_job_meta(metadata: dict[str, Any]) -> None:
|
|
"""Update the current RQ job's meta data with update stats information"""
|
|
try:
|
|
current_job = get_current_job()
|
|
if current_job:
|
|
current_job.meta.update(metadata)
|
|
current_job.save_meta()
|
|
except Exception as e:
|
|
# Silently fail if we can't update meta (e.g., not running in RQ context)
|
|
log.debug(f"Could not update job meta: {e}")
|
|
|
|
|
|
class TaskType(str, Enum):
|
|
"""Enumeration of task types for categorization and UI display."""
|
|
|
|
SCAN = "scan"
|
|
CONVERSION = "conversion"
|
|
CLEANUP = "cleanup"
|
|
UPDATE = "update"
|
|
SYNC = "sync"
|
|
WATCHER = "watcher"
|
|
GENERIC = "generic"
|
|
|
|
|
|
class Task(ABC):
|
|
"""Base class for all RQ tasks."""
|
|
|
|
title: str
|
|
description: str
|
|
enabled: bool
|
|
manual_run: bool
|
|
cron_string: str | None = None
|
|
task_type: TaskType
|
|
timeout: int
|
|
|
|
def __init__(
|
|
self,
|
|
title: str,
|
|
description: str,
|
|
task_type: TaskType,
|
|
enabled: bool = False,
|
|
manual_run: bool = False,
|
|
cron_string: str | None = None,
|
|
timeout: int = TASK_TIMEOUT,
|
|
):
|
|
self.title = title
|
|
self.description = description or title
|
|
self.task_type = task_type
|
|
self.enabled = enabled
|
|
self.manual_run = manual_run
|
|
self.cron_string = cron_string
|
|
self.timeout = timeout
|
|
|
|
@property
|
|
def can_run_manually(self) -> bool:
|
|
"""Whether an admin can trigger this task on demand."""
|
|
return self.manual_run and self.enabled
|
|
|
|
@property
|
|
def job_meta(self) -> dict[str, Any]:
|
|
"""What a job of this task carries so the API can describe it."""
|
|
return {"task_name": self.title, "task_type": self.task_type.value}
|
|
|
|
@abstractmethod
|
|
async def run(self, *args: Any, **kwargs: Any) -> Any: ...
|
|
|
|
|
|
class PeriodicTask(Task, ABC):
|
|
"""Base class for tasks the cron scheduler runs on a schedule."""
|
|
|
|
|
|
class RemoteFilePullTask(PeriodicTask, ABC):
|
|
"""Base class for tasks that pull files from a remote URL."""
|
|
|
|
def __init__(self, *args: Any, url: str, **kwargs: Any):
|
|
super().__init__(*args, **kwargs)
|
|
self.url = url
|
|
|
|
async def run(self, force: bool = False) -> Any:
|
|
if not self.enabled and not force:
|
|
log.info(f"Scheduled {self.description} not enabled, skipping...")
|
|
return None
|
|
|
|
log.info(f"Scheduled {self.description} started...")
|
|
|
|
httpx_client = ctx_httpx_client.get()
|
|
try:
|
|
response = await httpx_client.get(self.url, timeout=120)
|
|
response.raise_for_status()
|
|
return response.content
|
|
except httpx.HTTPError as e:
|
|
log.error(f"Scheduled {self.description} failed", exc_info=True)
|
|
log.error(e)
|
|
return None
|