From a6ef9c0c2bc871b2be74463dc696385db949aa11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sat, 2 Sep 2023 01:56:20 +0000 Subject: [PATCH] main blocking function: `run_metrics(*_metrics)` --- kiwi_simple_metrics/main.py | 23 +++++++++++++---------- kiwi_simple_metrics/metrics/__init__.py | 5 +++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/kiwi_simple_metrics/main.py b/kiwi_simple_metrics/main.py index 6301fea..18b2656 100644 --- a/kiwi_simple_metrics/main.py +++ b/kiwi_simple_metrics/main.py @@ -6,14 +6,11 @@ from . import metrics from .settings import SETTINGS -def handle_report() -> None: +def run_metrics(*_metrics: metrics.Metric) -> None: + reports = (metric() for metric in _metrics) + # create single report from metrics - report = metrics.Report.summary( - metrics.cpu(), - metrics.memory(), - metrics.disk(), - metrics.external(), - ) + report = metrics.Report.summary(*reports) # maybe print this to stdout if SETTINGS.log.enabled: @@ -23,18 +20,24 @@ def handle_report() -> None: report.push_webhook() -async def run_metrics() -> None: +async def async_main() -> None: loop = asyncio.get_running_loop() while True: await asyncio.gather( asyncio.sleep(SETTINGS.interval), - loop.run_in_executor(None, handle_report), + loop.run_in_executor( + None, run_metrics, + metrics.cpu, + metrics.memory, + metrics.disk, + metrics.external, + ), ) def main() -> None: - asyncio.run(run_metrics()) + asyncio.run(async_main()) if __name__ == "__main__": diff --git a/kiwi_simple_metrics/metrics/__init__.py b/kiwi_simple_metrics/metrics/__init__.py index cf1a0c0..8a2a250 100644 --- a/kiwi_simple_metrics/metrics/__init__.py +++ b/kiwi_simple_metrics/metrics/__init__.py @@ -1,11 +1,16 @@ +from typing import Callable, TypeAlias + from ._report import Report from .cpu import cpu from .disk import disk from .external import external from .memory import memory +Metric: TypeAlias = Callable[[], Report | None] + __all__ = [ "Report", + "Metric", "cpu", "disk",