periodic task using asyncio

This commit is contained in:
Jörn-Michael Miehe 2023-08-31 22:55:31 +00:00
parent 862e78eec0
commit 8f6a7a37fd
3 changed files with 23 additions and 5 deletions

1
.vscode/launch.json vendored
View file

@ -10,6 +10,7 @@
"request": "launch", "request": "launch",
"module": "kiwi_simple_metrics.main", "module": "kiwi_simple_metrics.main",
"env": { "env": {
"METRIC__INTERVAL": "5",
"METRIC__DISK__PATHS": "[\"/var\", \"/\", \"/dev\"]", "METRIC__DISK__PATHS": "[\"/var\", \"/\", \"/dev\"]",
}, },
"justMyCode": true "justMyCode": true

View file

@ -1,15 +1,29 @@
#!/usr/bin/python3 #!/usr/bin/python3
import asyncio
from . import metrics from . import metrics
from .settings import SETTINGS
def main() -> None: async def run_metrics() -> None:
while True:
interval = asyncio.sleep(SETTINGS.interval)
print(metrics.Report.concat( print(metrics.Report.concat(
metrics.cpu(), metrics.cpu(),
metrics.memory(), metrics.memory(),
metrics.disk(), metrics.disk(),
)) ))
await interval
def main() -> None:
loop = asyncio.get_event_loop()
loop.create_task(run_metrics())
loop.run_forever()
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View file

@ -88,6 +88,9 @@ class Settings(BaseSettings):
env_nested_delimiter="__", env_nested_delimiter="__",
) )
# time between gathering reports
interval: float = 600
# separates metrics and values in reports # separates metrics and values in reports
separator: str = ", " separator: str = ", "