From 90ace496e01cfd1265827a61c9523f67f7d2b8c0 Mon Sep 17 00:00:00 2001 From: Harry Suryapambagya Date: Fri, 6 Sep 2024 01:32:26 +0000 Subject: [PATCH] feat: map order checks --- tinystatus.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tinystatus.py b/tinystatus.py index ad1a029..f140b42 100644 --- a/tinystatus.py +++ b/tinystatus.py @@ -49,8 +49,7 @@ async def check_port(host, port): return False async def run_checks(checks): - background_tasks = set() - task = None + background_tasks = {} async with asyncio.TaskGroup() as tg: for check in checks: if check['type'] == 'http': @@ -61,11 +60,13 @@ async def run_checks(checks): task = tg.create_task(check_port(check['host'], check['port']), name=check['name']) if task: - background_tasks.add(task) + background_tasks[check['name']] = task + + results = [ + {"name": check["name"], "status": background_tasks[check["name"]].result()} + for check in checks + ] - results = [] - for task in background_tasks: - results.append({'name': task.get_name(), 'status': task.result()}) return results # History management @@ -106,7 +107,7 @@ async def monitor_services(): history_template = Template(f.read()) results = await run_checks(checks) - print(results) + update_history(results) html = template.render(checks=results, incidents=incidents, last_updated=datetime.now().strftime("%Y-%m-%d %H:%M:%S")) @@ -146,4 +147,4 @@ def main(): if __name__ == "__main__": logging.basicConfig(level=getattr(logging, LOG_LEVEL), format='%(asctime)s - %(levelname)s - %(message)s') - asyncio.run(monitor_services()) + asyncio.run(monitor_services()) \ No newline at end of file