mirror of
https://github.com/StefBuwalda/dashboard_test.git
synced 2025-10-30 11:19:58 +00:00
Refactor check_service, split the different ping types to a function called ping
This commit is contained in:
@@ -6,31 +6,31 @@ from models import log, service
|
|||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
|
||||||
|
async def ping(client: aiohttp.ClientSession, s: service) -> int:
|
||||||
|
match s.ping_method:
|
||||||
|
case 0:
|
||||||
|
r = await client.head(
|
||||||
|
url=s.url,
|
||||||
|
ssl=True if s.public_access else False,
|
||||||
|
allow_redirects=True,
|
||||||
|
)
|
||||||
|
case 1:
|
||||||
|
r = await client.get(
|
||||||
|
url=s.url,
|
||||||
|
ssl=True if s.public_access else False,
|
||||||
|
allow_redirects=True,
|
||||||
|
)
|
||||||
|
case _:
|
||||||
|
raise Exception("UNKNOWN PING METHOD")
|
||||||
|
return r.status
|
||||||
|
|
||||||
|
|
||||||
async def check_service(client: aiohttp.ClientSession, s: service) -> log:
|
async def check_service(client: aiohttp.ClientSession, s: service) -> log:
|
||||||
try:
|
try:
|
||||||
timeout = aiohttp.client.ClientTimeout(total=4)
|
|
||||||
before = time.perf_counter()
|
before = time.perf_counter()
|
||||||
match s.ping_method:
|
status = await ping(client=client, s=s)
|
||||||
case 0:
|
|
||||||
r = await client.head(
|
|
||||||
url=s.url,
|
|
||||||
ssl=True if s.public_access else False,
|
|
||||||
allow_redirects=True,
|
|
||||||
timeout=timeout,
|
|
||||||
auto_decompress=False,
|
|
||||||
)
|
|
||||||
case 1:
|
|
||||||
r = await client.get(
|
|
||||||
url=s.url,
|
|
||||||
ssl=True if s.public_access else False,
|
|
||||||
allow_redirects=True,
|
|
||||||
timeout=timeout,
|
|
||||||
auto_decompress=False,
|
|
||||||
)
|
|
||||||
case _:
|
|
||||||
raise Exception("UNKNOWN PING TYPE")
|
|
||||||
after = time.perf_counter()
|
after = time.perf_counter()
|
||||||
if r.status == 200:
|
if status == 200:
|
||||||
return log(service_id=s.id, ping=int((after - before) * 1000))
|
return log(service_id=s.id, ping=int((after - before) * 1000))
|
||||||
else:
|
else:
|
||||||
return log(service_id=s.id, ping=None)
|
return log(service_id=s.id, ping=None)
|
||||||
@@ -49,9 +49,11 @@ def start_async_loop():
|
|||||||
|
|
||||||
async def update_services(loop: asyncio.AbstractEventLoop):
|
async def update_services(loop: asyncio.AbstractEventLoop):
|
||||||
print("Starting service updates...")
|
print("Starting service updates...")
|
||||||
|
# Create new session
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
WorkerSession = sessionmaker(bind=db.engine)
|
WorkerSession = sessionmaker(bind=db.engine)
|
||||||
client = aiohttp.ClientSession()
|
timeout = aiohttp.client.ClientTimeout(total=4)
|
||||||
|
client = aiohttp.ClientSession(timeout=timeout, auto_decompress=False)
|
||||||
while True:
|
while True:
|
||||||
session = WorkerSession()
|
session = WorkerSession()
|
||||||
sleeptask = asyncio.create_task(asyncio.sleep(5))
|
sleeptask = asyncio.create_task(asyncio.sleep(5))
|
||||||
|
|||||||
Reference in New Issue
Block a user