mirror of
https://github.com/StefBuwalda/dashboard_test.git
synced 2025-10-30 03:09:59 +00:00
Made polling services async
This commit is contained in:
6
main.py
6
main.py
@@ -1,6 +1,6 @@
|
||||
# import requests as r
|
||||
from flask import jsonify, Flask
|
||||
from poll_services import update_services
|
||||
from poll_services import start_async_loop
|
||||
from mem import services
|
||||
import threading
|
||||
|
||||
@@ -16,7 +16,9 @@ def status():
|
||||
|
||||
# Only run if directly running file
|
||||
if __name__ == "__main__":
|
||||
threading.Thread(target=update_services, daemon=True).start()
|
||||
|
||||
t = threading.Thread(target=start_async_loop, daemon=True)
|
||||
t.start()
|
||||
|
||||
# Run flask app
|
||||
app.run(debug=True, use_reloader=False)
|
||||
|
||||
@@ -25,6 +25,9 @@ class service:
|
||||
def set_status(self, status: int):
|
||||
self.status = status
|
||||
|
||||
def set_online(self, b: bool):
|
||||
self.online = b
|
||||
|
||||
|
||||
services: list[service] = [
|
||||
service("https://git.ihatemen.uk"),
|
||||
|
||||
@@ -1,24 +1,41 @@
|
||||
from mem import services
|
||||
import requests
|
||||
from mem import services, service
|
||||
import httpx
|
||||
import urllib3
|
||||
import time
|
||||
import asyncio
|
||||
|
||||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||||
|
||||
|
||||
def update_services() -> None:
|
||||
print("Updating Service Status")
|
||||
while True:
|
||||
for s in services:
|
||||
try:
|
||||
r = requests.head(
|
||||
url=s.url,
|
||||
verify=s.public,
|
||||
allow_redirects=True,
|
||||
timeout=1,
|
||||
)
|
||||
s.set_status(r.ok)
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(e)
|
||||
s.set_status(False)
|
||||
time.sleep(3)
|
||||
async def check_service(client: httpx.AsyncClient, s: service):
|
||||
try:
|
||||
r = await client.head(
|
||||
url=s.url,
|
||||
follow_redirects=True,
|
||||
timeout=1,
|
||||
)
|
||||
print(r.status_code)
|
||||
s.set_online(r.status_code == 200)
|
||||
except httpx.RequestError as e:
|
||||
print(e)
|
||||
s.set_online(False)
|
||||
|
||||
|
||||
def start_async_loop():
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
asyncio.run_coroutine_threadsafe(update_services(loop=loop), loop=loop)
|
||||
loop.run_forever()
|
||||
|
||||
|
||||
async def update_services(loop: asyncio.AbstractEventLoop):
|
||||
print("Starting service updates...")
|
||||
async with httpx.AsyncClient() as public_client, httpx.AsyncClient(
|
||||
verify=False
|
||||
) as local_client:
|
||||
while True:
|
||||
tasks = [
|
||||
check_service(public_client if s.public else local_client, s)
|
||||
for s in services
|
||||
]
|
||||
await asyncio.gather(*tasks)
|
||||
await asyncio.sleep(2)
|
||||
|
||||
Reference in New Issue
Block a user