better timing with concurrency

This commit is contained in:
Jörn-Michael Miehe 2023-09-01 00:19:04 +00:00
parent 94191f3b5d
commit f23d7f2e37

View file

@ -9,37 +9,40 @@ from . import metrics
from .settings import SETTINGS from .settings import SETTINGS
async def run_metrics() -> None: def handle_report() -> None:
while True: # create single report from metrics
interval = asyncio.sleep(SETTINGS.interval) report = metrics.Report.summary(
metrics.cpu(),
metrics.memory(),
metrics.disk(),
)
# create single report from metrics # maybe print this to stdout
report = metrics.Report.summary( if SETTINGS.log.enabled:
metrics.cpu(), print(report)
metrics.memory(),
metrics.disk(), # maybe push this to a webhook
if (url := SETTINGS.webhook.get_url(failed=report.failed)) is not None:
requests.get(
url=str(url).format(
urllib.parse.quote_plus(report.result)
),
verify=not SETTINGS.webhook.insecure,
) )
# maybe print this to stdout
if SETTINGS.log.enabled:
print(report)
# maybe push this to a webhook async def run_metrics() -> None:
if (url := SETTINGS.webhook.get_url(failed=report.failed)) is not None: loop = asyncio.get_running_loop()
requests.get(
url=str(url).format(
urllib.parse.quote_plus(report.result)
),
verify=not SETTINGS.webhook.insecure,
)
await interval while True:
await asyncio.gather(
asyncio.sleep(SETTINGS.interval),
loop.run_in_executor(None, handle_report),
)
def main() -> None: def main() -> None:
loop = asyncio.get_event_loop() asyncio.run(run_metrics())
loop.create_task(run_metrics())
loop.run_forever()
if __name__ == "__main__": if __name__ == "__main__":