Compare commits

...

3 Commits

3 changed files with 37 additions and 34 deletions

View File

@@ -65,7 +65,7 @@ class service:
services: list[service] = [
service(0, "https://git.ihatemen.uk/", "Gitea"),
service(1, "https://plex.ihatemen.uk/", "Plex", ping_type=1),
service(1, "https://plex.ihatemen.uk/", "Plex"),
service(2, "https://truenas.local/", "TrueNAS", False),
service(3, "https://cloud.ihatemen.uk/", "NextCloud"),
service(4, "https://request.ihatemen.uk/", "Overseerr"),

View File

@@ -1,36 +1,38 @@
from mem import services, service, db, app
import httpx
import aiohttp
import asyncio
import time
from models import log
from sqlalchemy.orm import sessionmaker
async def check_service(client: httpx.AsyncClient, s: service) -> log:
async def check_service(client: aiohttp.ClientSession, s: service) -> log:
try:
timeout = aiohttp.client.ClientTimeout(total=4)
before = time.perf_counter()
match s.ping_type:
case 0:
r = await client.head(
url=s.url,
follow_redirects=True,
timeout=4,
ssl=True if s.public else False,
allow_redirects=True,
timeout=timeout,
)
case 1:
r = await client.get(
url=s.url,
follow_redirects=True,
timeout=4,
headers={"Host": "plex.ihatemen.uk"},
ssl=True if s.public else False,
allow_redirects=True,
timeout=timeout,
)
case _:
raise httpx.HTTPError("Unknown ping type")
raise Exception("UNKNOWN PING TYPE")
after = time.perf_counter()
s.set_error(None)
s.set_online(r.status_code == 200)
s.set_status(r.status_code)
s.set_online(r.status == 200)
s.set_status(r.status)
s.set_ping(int((after - before) * 1000))
except httpx.ConnectTimeout:
except aiohttp.ConnectionTimeoutError:
s.set_error("Connection Timeout")
s.set_online(False)
s.set_status(None)
@@ -60,24 +62,18 @@ 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
]
tasks.append(sleepTask())
logs = await asyncio.gather(*tasks)
logs = logs[:-1]
try:
session.add_all(logs)
session.commit()
except Exception as e:
session.rollback()
raise e
finally:
session.close()
client = aiohttp.ClientSession()
while True:
session = WorkerSession()
tasks = [check_service(client=client, s=s) for s in services]
tasks.append(sleepTask())
logs = await asyncio.gather(*tasks)
logs = logs[:-1]
try:
session.add_all(logs)
session.commit()
except Exception as e:
session.rollback()
raise e
finally:
session.close()

View File

@@ -1,5 +1,9 @@
aiohappyeyeballs==2.6.1
aiohttp==3.12.15
aiosignal==1.4.0
alembic==1.16.5
anyio==4.10.0
attrs==25.3.0
blinker==1.9.0
certifi==2025.8.3
click==8.2.1
@@ -7,16 +11,19 @@ colorama==0.4.6
Flask==3.1.2
Flask-Migrate==4.1.0
Flask-SQLAlchemy==3.1.1
frozenlist==1.7.0
greenlet==3.2.4
h11==0.16.0
httpcore==1.0.9
httpx==0.28.1
idna==3.10
itsdangerous==2.2.0
Jinja2==3.1.6
Mako==1.3.10
MarkupSafe==3.0.2
multidict==6.6.4
propcache==0.3.2
sniffio==1.3.1
SQLAlchemy==2.0.43
typing_extensions==4.15.0
Werkzeug==3.1.3
yarl==1.20.1