mirror of
https://github.com/rommapp/romm.git
synced 2026-12-02 10:08:40 +01:00
report_task_failure can run in the worker parent when a work horse is killed, so it now enqueues notify_task_failure on the default queue instead of loading the notification stack there. enqueue_task defaults to the queue its spec names. The streaming worker runs as PreloadingWorker, which imports the enabled scheduled tasks of its queues, so the reaper's per-minute horse inherits its stack. Spec tests now read config in a fresh interpreter instead of comparing a value with itself. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
73 lines
2.8 KiB
Python
73 lines
2.8 KiB
Python
import contextlib
|
|
import logging
|
|
from typing import Any, Final
|
|
|
|
import rq.scheduler
|
|
from rq import Worker
|
|
from rq.exceptions import AbandonedJobError
|
|
from rq.job import Job
|
|
|
|
from tasks.tasks import report_task_failure
|
|
|
|
# Fragments of the lines RQ logs on every tick. The maintenance sweep and the
|
|
# scheduler heartbeat still run; only their log records are dropped.
|
|
_PERIODIC_NOISE: Final[tuple[str, ...]] = (
|
|
"cleaning registries for queue",
|
|
"scheduler sending heartbeat to",
|
|
)
|
|
|
|
|
|
class _DropPeriodicNoiseFilter(logging.Filter):
|
|
"""Drops RQ's per-tick maintenance and scheduler-heartbeat log lines."""
|
|
|
|
def filter(self, record: logging.LogRecord) -> bool:
|
|
message = record.getMessage().lower()
|
|
return not any(fragment in message for fragment in _PERIODIC_NOISE)
|
|
|
|
|
|
def _silence_periodic_noise(logger: logging.Logger) -> None:
|
|
if not any(isinstance(f, _DropPeriodicNoiseFilter) for f in logger.filters):
|
|
logger.addFilter(_DropPeriodicNoiseFilter())
|
|
|
|
|
|
class RomMWorker(Worker):
|
|
"""RQ worker that reports failed tasks and silences RQ's periodic log lines."""
|
|
|
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
super().__init__(*args, **kwargs)
|
|
_silence_periodic_noise(self.log)
|
|
# --with-scheduler forks the scheduler off this process, so a filter
|
|
# added here is the only one that logger inherits.
|
|
_silence_periodic_noise(logging.getLogger(rq.scheduler.__name__))
|
|
self.push_exc_handler(report_task_failure)
|
|
|
|
def handle_work_horse_killed(
|
|
self, job: Job, retpid: int, ret_val: int, rusage: Any
|
|
) -> None:
|
|
# mypy's untyped_calls_exclude cannot name the receiver of a super() call.
|
|
super().handle_work_horse_killed(job, retpid, ret_val, rusage) # type: ignore[no-untyped-call]
|
|
# RQ runs neither the failure callback nor the exception handlers for a
|
|
# killed horse, only for a job whose whole worker died.
|
|
exc_info = (AbandonedJobError, AbandonedJobError(), None)
|
|
# RQ logs a callback that raises.
|
|
with contextlib.suppress(Exception):
|
|
job.execute_failure_callback(self.death_penalty_class, *exc_info)
|
|
self.handle_exception(job, *exc_info)
|
|
|
|
|
|
class PreloadingWorker(RomMWorker):
|
|
"""RomMWorker that imports the enabled scheduled tasks of its queues up front.
|
|
|
|
For a dedicated queue whose tasks run often enough that every work horse
|
|
importing its task again costs more than holding it in the parent.
|
|
"""
|
|
|
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
super().__init__(*args, **kwargs)
|
|
from tasks.registry import SCHEDULED_TASKS, get_task
|
|
|
|
queues = set(self.queue_names())
|
|
for name, spec in SCHEDULED_TASKS.items():
|
|
if spec.enabled and spec.queue_name in queues:
|
|
get_task(name)
|