mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
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>
134 lines
3.9 KiB
Python
134 lines
3.9 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
|
|
|
|
|
|
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
|