diff --git a/README.md b/README.md index fa94cd1..a54657d 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Check out an online demo https://status.harry.id ## Prerequisites -- Python 3.7 or higher +- Python 3.11 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"))