Skip to content

Commit

Permalink
Perform checks in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Marano committed Sep 5, 2024
1 parent 9652925 commit 79202ec
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 17 additions & 8 deletions tinystatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))
Expand Down

0 comments on commit 79202ec

Please sign in to comment.