mirror of
https://github.com/StefBuwalda/dashboard_test.git
synced 2025-10-30 03:09:59 +00:00
Creates logs when services are pinged
This commit is contained in:
1
app.py
1
app.py
@@ -49,7 +49,6 @@ def favicon():
|
||||
|
||||
# Only run if directly running file
|
||||
if __name__ == "__main__":
|
||||
|
||||
t = threading.Thread(target=start_async_loop, daemon=True)
|
||||
t.start()
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ from typing import Any, Optional
|
||||
|
||||
|
||||
class log(db.Model):
|
||||
id: int = db.Column(db.Integer, primary_key=True)
|
||||
id: int = db.Column(db.Integer, primary_key=True) # TODO: Switch to UUID
|
||||
dateCreated: datetime = db.Column(db.DateTime, nullable=False, index=True)
|
||||
service_id: int = db.Column(
|
||||
db.Integer,
|
||||
@@ -14,8 +14,10 @@ class log(db.Model):
|
||||
)
|
||||
ping: Optional[int] = db.Column(db.Integer, nullable=True)
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, service_id: int, ping: Optional[int]):
|
||||
super().__init__()
|
||||
self.service_id = service_id
|
||||
self.ping = ping
|
||||
|
||||
self.dateCreated = datetime.now(timezone.utc)
|
||||
|
||||
|
||||
@@ -31,6 +31,9 @@ async def check_service(client: aiohttp.ClientSession, s: service) -> log:
|
||||
s.set_error(None)
|
||||
s.set_online(r.status == 200)
|
||||
s.set_status(r.status)
|
||||
if r.status != 200:
|
||||
s.set_ping(None)
|
||||
else:
|
||||
s.set_ping(int((after - before) * 1000))
|
||||
except aiohttp.ConnectionTimeoutError:
|
||||
s.set_error("Connection Timeout")
|
||||
@@ -43,7 +46,7 @@ async def check_service(client: aiohttp.ClientSession, s: service) -> log:
|
||||
s.set_online(False)
|
||||
s.set_status(None)
|
||||
s.set_ping(None)
|
||||
return log()
|
||||
return log(service_id=s.id, ping=s.ping)
|
||||
|
||||
|
||||
def start_async_loop():
|
||||
@@ -53,11 +56,6 @@ def start_async_loop():
|
||||
loop.run_forever()
|
||||
|
||||
|
||||
async def sleepTask():
|
||||
await asyncio.sleep(5)
|
||||
return log()
|
||||
|
||||
|
||||
async def update_services(loop: asyncio.AbstractEventLoop):
|
||||
print("Starting service updates...")
|
||||
with app.app_context():
|
||||
@@ -65,10 +63,10 @@ async def update_services(loop: asyncio.AbstractEventLoop):
|
||||
client = aiohttp.ClientSession()
|
||||
while True:
|
||||
session = WorkerSession()
|
||||
sleeptask = asyncio.create_task(asyncio.sleep(5))
|
||||
tasks = [check_service(client=client, s=s) for s in services]
|
||||
tasks.append(sleepTask())
|
||||
logs = await asyncio.gather(*tasks)
|
||||
logs = logs[:-1]
|
||||
await sleeptask
|
||||
try:
|
||||
session.add_all(logs)
|
||||
session.commit()
|
||||
|
||||
Reference in New Issue
Block a user