From 79202ecda7c76c634d61d1601123fa8d6936a55a Mon Sep 17 00:00:00 2001 From: Davide Marano Date: Thu, 5 Sep 2024 20:47:42 +0200 Subject: [PATCH 1/2] Perform checks in parallel --- README.md | 2 +- tinystatus.py | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c47e35b..6668bb5 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ TinyStatus is a simple, customizable status page generator that allows you to mo ## Prerequisites -- Python 3.7 or higher +- Python 3.8 or higher - pip (Python package manager) ## Installation diff --git a/tinystatus.py b/tinystatus.py index bc9a3e0..ad1a029 100644 --- a/tinystatus.py +++ b/tinystatus.py @@ -49,15 +49,23 @@ async def check_port(host, port): return False async def run_checks(checks): + background_tasks = set() + task = None + async with asyncio.TaskGroup() as tg: + for check in checks: + if check['type'] == 'http': + task = tg.create_task(check_http(check['host'], check['expected_code']), name=check['name']) + elif check['type'] == 'ping': + task = tg.create_task(check_ping(check['host']), name=check['name']) + elif check['type'] == 'port': + task = tg.create_task(check_port(check['host'], check['port']), name=check['name']) + + if task: + background_tasks.add(task) + results = [] - for check in checks: - if check['type'] == 'http': - status = await check_http(check['host'], check['expected_code']) - elif check['type'] == 'ping': - status = await check_ping(check['host']) - elif check['type'] == 'port': - status = await check_port(check['host'], check['port']) - results.append({'name': check['name'], 'status': status}) + for task in background_tasks: + results.append({'name': task.get_name(), 'status': task.result()}) return results # History management @@ -98,6 +106,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")) From dc68735f5d2dbaca235d144605a73cdda2fb9837 Mon Sep 17 00:00:00 2001 From: Davide Marano Date: Thu, 5 Sep 2024 20:59:26 +0200 Subject: [PATCH 2/2] Fix prerequisites --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6668bb5..fe7ff69 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ TinyStatus is a simple, customizable status page generator that allows you to mo ## Prerequisites -- Python 3.8 or higher +- Python 3.11 or higher - pip (Python package manager) ## Installation