mirror of
https://github.com/StefBuwalda/dashboard_test.git
synced 2025-10-30 11:19:58 +00:00
Added logs for each ping. Contains no data yet and estimates 15.7 mil rows a year. Considering adding log aggregation. Moved some folders and files into mem folder due to circular import.
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
from mem import services, service
|
||||
from mem import services, service, db, app
|
||||
import httpx
|
||||
import asyncio
|
||||
import time
|
||||
from models import log
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
|
||||
async def check_service(client: httpx.AsyncClient, s: service):
|
||||
async def check_service(client: httpx.AsyncClient, s: service) -> log:
|
||||
try:
|
||||
before = time.perf_counter()
|
||||
match s.ping_type:
|
||||
@@ -22,7 +24,6 @@ async def check_service(client: httpx.AsyncClient, s: service):
|
||||
)
|
||||
case _:
|
||||
raise httpx.HTTPError("Unknown ping type")
|
||||
|
||||
after = time.perf_counter()
|
||||
s.set_error(None)
|
||||
s.set_online(r.status_code == 200)
|
||||
@@ -33,6 +34,7 @@ async def check_service(client: httpx.AsyncClient, s: service):
|
||||
s.set_online(False)
|
||||
s.set_status(None)
|
||||
s.set_ping(None)
|
||||
return log()
|
||||
|
||||
|
||||
def start_async_loop():
|
||||
@@ -44,14 +46,25 @@ def start_async_loop():
|
||||
|
||||
async def update_services(loop: asyncio.AbstractEventLoop):
|
||||
print("Starting service updates...")
|
||||
with app.app_context():
|
||||
WorkerSession = sessionmaker(bind=db.engine)
|
||||
async with (
|
||||
httpx.AsyncClient() as public_client,
|
||||
httpx.AsyncClient(verify=False) as local_client,
|
||||
):
|
||||
while True:
|
||||
session = WorkerSession()
|
||||
tasks = [
|
||||
check_service(public_client if s.public else local_client, s)
|
||||
for s in services
|
||||
]
|
||||
await asyncio.gather(*tasks)
|
||||
logs = await asyncio.gather(*tasks)
|
||||
try:
|
||||
session.add_all(logs)
|
||||
session.commit()
|
||||
except Exception as e:
|
||||
session.rollback()
|
||||
raise e
|
||||
finally:
|
||||
session.close()
|
||||
await asyncio.sleep(2)
|
||||
|
||||
Reference in New Issue
Block a user