main blocking function: run_metrics(*_metrics)

This commit is contained in:
Jörn-Michael Miehe 2023-09-02 01:56:20 +00:00
parent cac6129282
commit a6ef9c0c2b
2 changed files with 18 additions and 10 deletions

View file

@ -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__":

View file

@ -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",