mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 01:58:41 +01:00
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>
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
|