mirror of
https://github.com/rommapp/romm.git
synced 2026-12-02 10:08:40 +01:00
Quiet RQ's per-tick scheduler heartbeat log line
Each worker started with --with-scheduler forks a scheduler that logs "Scheduler sending heartbeat to <queues>" on every 1s tick. With LOGLEVEL=DEBUG that is two lines per second, which drowns out the rest of the log. Generalize the existing registry-cleanup filter to cover RQ's periodic noise and install it on the rq.scheduler logger before the worker forks the scheduler off. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,25 +1,38 @@
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import rq.scheduler
|
||||
from rq import Worker
|
||||
|
||||
# Lines RQ emits on every maintenance sweep or scheduler tick. The work itself
|
||||
# still runs (crash recovery for orphaned jobs, TTL reaping, stale worker
|
||||
# pruning, scheduler lock renewal); only the records are dropped so they do not
|
||||
# flood the logs, which at DEBUG means two heartbeat lines per second.
|
||||
_PERIODIC_NOISE = (
|
||||
"cleaning registries for queue",
|
||||
"scheduler sending heartbeat to",
|
||||
)
|
||||
|
||||
class _DropRegistryCleanupFilter(logging.Filter):
|
||||
"""Drops RQ's periodic "cleaning registries for queue" INFO line.
|
||||
|
||||
The maintenance sweep still runs (crash recovery for orphaned jobs, TTL
|
||||
reaping, stale worker pruning); only its log record is suppressed so it
|
||||
does not flood logs on every maintenance interval.
|
||||
"""
|
||||
class _DropPeriodicNoiseFilter(logging.Filter):
|
||||
"""Drops RQ's per-tick maintenance and scheduler-heartbeat log lines."""
|
||||
|
||||
def filter(self, record: logging.LogRecord) -> bool:
|
||||
return "cleaning registries for queue" not in record.getMessage().lower()
|
||||
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 the noisy registry-cleanup log line."""
|
||||
"""RQ worker that silences RQ's noisy periodic log lines."""
|
||||
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
if not any(isinstance(f, _DropRegistryCleanupFilter) for f in self.log.filters):
|
||||
self.log.addFilter(_DropRegistryCleanupFilter())
|
||||
_silence_periodic_noise(self.log)
|
||||
# --with-scheduler forks a scheduler off this process, so the filter it
|
||||
# inherits here is the only chance to configure that logger.
|
||||
_silence_periodic_noise(logging.getLogger(rq.scheduler.__name__))
|
||||
|
||||
42
backend/tests/handler/test_rq_worker.py
Normal file
42
backend/tests/handler/test_rq_worker.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
import rq.scheduler
|
||||
|
||||
from handler.rq_worker import _DropPeriodicNoiseFilter, _silence_periodic_noise
|
||||
|
||||
|
||||
def record(message: str, *args: object) -> logging.LogRecord:
|
||||
return logging.LogRecord("rq", logging.DEBUG, "f", 1, message, args, None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"message,args",
|
||||
[
|
||||
("Scheduler sending heartbeat to %s", ("low, default, high",)),
|
||||
("Cleaning registries for queue: %s", ("default",)),
|
||||
],
|
||||
)
|
||||
def test_drops_periodic_noise(message: str, args: tuple[object, ...]) -> None:
|
||||
assert not _DropPeriodicNoiseFilter().filter(record(message, *args))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"message,args",
|
||||
[
|
||||
("Acquired scheduler lock for %s", ("default",)),
|
||||
("Scheduler for %s started with PID %s", ("default", 1)),
|
||||
("Scheduler lock for %s had expired; re-acquired", ("default",)),
|
||||
],
|
||||
)
|
||||
def test_keeps_everything_else(message: str, args: tuple[object, ...]) -> None:
|
||||
assert _DropPeriodicNoiseFilter().filter(record(message, *args))
|
||||
|
||||
|
||||
def test_silencing_a_logger_is_idempotent() -> None:
|
||||
log = logging.getLogger(f"{rq.scheduler.__name__}.test")
|
||||
|
||||
_silence_periodic_noise(log)
|
||||
_silence_periodic_noise(log)
|
||||
|
||||
assert len(log.filters) == 1
|
||||
Reference in New Issue
Block a user