summaryrefslogtreecommitdiffstats
path: root/python/knot_resolver/manager
diff options
context:
space:
mode:
Diffstat (limited to 'python/knot_resolver/manager')
-rw-r--r--python/knot_resolver/manager/files/watchdog.py22
-rw-r--r--python/knot_resolver/manager/metrics/prometheus.py12
2 files changed, 14 insertions, 20 deletions
diff --git a/python/knot_resolver/manager/files/watchdog.py b/python/knot_resolver/manager/files/watchdog.py
index 64547192..5dcc2d5e 100644
--- a/python/knot_resolver/manager/files/watchdog.py
+++ b/python/knot_resolver/manager/files/watchdog.py
@@ -1,19 +1,15 @@
-import importlib
import logging
from pathlib import Path
from threading import Timer
from typing import List, Optional
+from knot_resolver.constants import WATCHDOG_LIB
from knot_resolver.controller.registered_workers import command_registered_workers
from knot_resolver.datamodel import KresConfig
from knot_resolver.datamodel.types import File
from knot_resolver.manager.config_store import ConfigStore, only_on_real_changes_update
from knot_resolver.utils import compat
-_watchdog = False
-if importlib.util.find_spec("watchdog"):
- _watchdog = True
-
logger = logging.getLogger(__name__)
@@ -25,7 +21,7 @@ def tls_cert_paths(config: KresConfig) -> List[str]:
return [str(file) for file in files if file is not None]
-if _watchdog:
+if WATCHDOG_LIB:
from watchdog.events import (
FileSystemEvent,
FileSystemEventHandler,
@@ -112,13 +108,16 @@ if _watchdog:
self._observer.stop()
self._observer.join()
- @only_on_real_changes_update(tls_cert_paths)
- async def _init_tls_cert_watchdog(config: KresConfig) -> None:
+
+@only_on_real_changes_update(tls_cert_paths)
+async def _init_tls_cert_watchdog(config: KresConfig) -> None:
+ if WATCHDOG_LIB:
global _tls_cert_watchdog
+
if _tls_cert_watchdog:
_tls_cert_watchdog.stop()
- if config.network.tls.cert_file and config.network.tls.key_file:
+ if config.network.tls.files_watchdog and config.network.tls.cert_file and config.network.tls.key_file:
logger.info("Initializing TLS certificate files WatchDog")
_tls_cert_watchdog = TLSCertWatchDog(
config.network.tls.cert_file.to_path(),
@@ -128,6 +127,5 @@ if _watchdog:
async def init_files_watchdog(config_store: ConfigStore) -> None:
- if _watchdog:
- # watchdog for TLS certificate files
- await config_store.register_on_change_callback(_init_tls_cert_watchdog)
+ # watchdog for TLS certificate files
+ await config_store.register_on_change_callback(_init_tls_cert_watchdog)
diff --git a/python/knot_resolver/manager/metrics/prometheus.py b/python/knot_resolver/manager/metrics/prometheus.py
index 5dd0d171..4242d960 100644
--- a/python/knot_resolver/manager/metrics/prometheus.py
+++ b/python/knot_resolver/manager/metrics/prometheus.py
@@ -1,8 +1,8 @@
import asyncio
-import importlib
import logging
from typing import Any, Dict, Generator, List, Optional, Tuple
+from knot_resolver.constants import PROMETHEUS_LIB
from knot_resolver.controller.interface import KresID
from knot_resolver.controller.registered_workers import get_registered_workers_kresids
from knot_resolver.datamodel.config_schema import KresConfig
@@ -12,13 +12,9 @@ from knot_resolver.utils.functional import Result
from .collect import collect_kresd_workers_metrics
-_prometheus_client = False
-if importlib.util.find_spec("prometheus_client"):
- _prometheus_client = True
-
logger = logging.getLogger(__name__)
-if _prometheus_client:
+if PROMETHEUS_LIB:
from prometheus_client import exposition # type: ignore
from prometheus_client.bridge.graphite import GraphiteBridge # type: ignore
from prometheus_client.core import (
@@ -359,7 +355,7 @@ async def init_prometheus(config_store: ConfigStore) -> None:
"""
Initialize metrics collection. Must be called before any other function from this module.
"""
- if _prometheus_client:
+ if PROMETHEUS_LIB:
# init and register metrics collector
global _metrics_collector
_metrics_collector = KresPrometheusMetricsCollector(config_store)
@@ -371,7 +367,7 @@ async def init_prometheus(config_store: ConfigStore) -> None:
async def report_prometheus() -> Optional[bytes]:
- if _prometheus_client:
+ if PROMETHEUS_LIB:
# manually trigger stat collection so that we do not have to wait for it
if _metrics_collector is not None:
await _metrics_collector.collect_kresd_stats()