Files
romm/backend/handler/rq_worker.py
Georges-Antoine Assi b62dbb5d9c Trim the noise-filter comments and cover the worker wiring
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-07 21:43:11 -04:00

37 lines
1.3 KiB
Python

import logging
from typing import Any, Final
import rq.scheduler
from rq import Worker
# 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 silences RQ's noisy 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__))