Skip to content

Commit

Permalink
Merge pull request #10 from m2dtech/master
Browse files Browse the repository at this point in the history
Perform checks in parallel
  • Loading branch information
harsxv authored Sep 6, 2024
2 parents bdc3e72 + dc68735 commit d3a1ab9
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 @@ -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
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 d3a1ab9

Please sign in to comment.