From 8f6a7a37fd738af8fe27de2056ccac5353bb8f7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Thu, 31 Aug 2023 22:55:31 +0000 Subject: [PATCH] periodic task using `asyncio` --- .vscode/launch.json | 1 + kiwi_simple_metrics/main.py | 24 +++++++++++++++++++----- kiwi_simple_metrics/settings.py | 3 +++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index f702240..3f25227 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,6 +10,7 @@ "request": "launch", "module": "kiwi_simple_metrics.main", "env": { + "METRIC__INTERVAL": "5", "METRIC__DISK__PATHS": "[\"/var\", \"/\", \"/dev\"]", }, "justMyCode": true diff --git a/kiwi_simple_metrics/main.py b/kiwi_simple_metrics/main.py index 4891e7f..82a6bb7 100644 --- a/kiwi_simple_metrics/main.py +++ b/kiwi_simple_metrics/main.py @@ -1,14 +1,28 @@ #!/usr/bin/python3 +import asyncio + from . import metrics +from .settings import SETTINGS + + +async def run_metrics() -> None: + while True: + interval = asyncio.sleep(SETTINGS.interval) + + print(metrics.Report.concat( + metrics.cpu(), + metrics.memory(), + metrics.disk(), + )) + + await interval def main() -> None: - print(metrics.Report.concat( - metrics.cpu(), - metrics.memory(), - metrics.disk(), - )) + loop = asyncio.get_event_loop() + loop.create_task(run_metrics()) + loop.run_forever() if __name__ == "__main__": diff --git a/kiwi_simple_metrics/settings.py b/kiwi_simple_metrics/settings.py index 3d2bf11..26106c2 100644 --- a/kiwi_simple_metrics/settings.py +++ b/kiwi_simple_metrics/settings.py @@ -88,6 +88,9 @@ class Settings(BaseSettings): env_nested_delimiter="__", ) + # time between gathering reports + interval: float = 600 + # separates metrics and values in reports separator: str = ", "