Polling of services is done based on services in DB

This commit is contained in:
2025-09-03 18:32:02 +02:00
parent 5ddafd3b98
commit 39b4ee7a00

View File

@@ -1,8 +1,8 @@
from mem import services, service, db, app from mem import db, app
import aiohttp import aiohttp
import asyncio import asyncio
import time import time
from models import log from models import log, service
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
@@ -10,18 +10,18 @@ async def check_service(client: aiohttp.ClientSession, s: service) -> log:
try: try:
timeout = aiohttp.client.ClientTimeout(total=4) timeout = aiohttp.client.ClientTimeout(total=4)
before = time.perf_counter() before = time.perf_counter()
match s.ping_type: match s.ping_method:
case 0: case 0:
r = await client.head( r = await client.head(
url=s.url, url=s.url,
ssl=True if s.public else False, ssl=True if s.public_access else False,
allow_redirects=True, allow_redirects=True,
timeout=timeout, timeout=timeout,
) )
case 1: case 1:
r = await client.get( r = await client.get(
url=s.url, url=s.url,
ssl=True if s.public else False, ssl=True if s.public_access else False,
allow_redirects=True, allow_redirects=True,
timeout=timeout, timeout=timeout,
) )
@@ -29,13 +29,13 @@ async def check_service(client: aiohttp.ClientSession, s: service) -> log:
raise Exception("UNKNOWN PING TYPE") raise Exception("UNKNOWN PING TYPE")
after = time.perf_counter() after = time.perf_counter()
if r.status == 200: if r.status == 200:
return log(service_id=s.id + 1, ping=int((after - before) * 1000)) return log(service_id=s.id, ping=int((after - before) * 1000))
else: else:
return log(service_id=s.id + 1, ping=None) return log(service_id=s.id, ping=None)
except aiohttp.ConnectionTimeoutError: except aiohttp.ConnectionTimeoutError:
return log(service_id=s.id + 1, ping=None) return log(service_id=s.id, ping=None)
except Exception: except Exception:
return log(service_id=s.id + 1, ping=None) return log(service_id=s.id, ping=None)
def start_async_loop(): def start_async_loop():
@@ -53,7 +53,10 @@ async def update_services(loop: asyncio.AbstractEventLoop):
while True: while True:
session = WorkerSession() session = WorkerSession()
sleeptask = asyncio.create_task(asyncio.sleep(5)) sleeptask = asyncio.create_task(asyncio.sleep(5))
tasks = [check_service(client=client, s=s) for s in services] tasks = [
check_service(client=client, s=s)
for s in session.query(service).all()
]
logs = await asyncio.gather(*tasks) logs = await asyncio.gather(*tasks)
await sleeptask await sleeptask
try: try: