Added option to ping with a different method (HEAD and GET)

This commit is contained in:
2025-08-31 16:28:38 +02:00
parent b8c5114335
commit 93bc1d7f04
2 changed files with 28 additions and 10 deletions

View File

@@ -2,7 +2,7 @@ from typing import Any, Optional
class service: class service:
id = int id: int
url: str url: str
status: Optional[int] status: Optional[int]
online: bool online: bool
@@ -10,6 +10,7 @@ class service:
error: Optional[str] error: Optional[str]
ping: Optional[int] ping: Optional[int]
icon_filetype: str icon_filetype: str
ping_type: int
def __init__( def __init__(
self, self,
@@ -18,11 +19,13 @@ class service:
label: str = "", label: str = "",
public: bool = True, public: bool = True,
icon_filetype: str = "svg", icon_filetype: str = "svg",
ping_type: int = 0,
): ):
self.id = id self.id = id
self.url = url self.url = url
self.public = public self.public = public
self.label = label self.label = label
self.ping_type = ping_type
self.online = False self.online = False
self.status = None self.status = None
@@ -41,6 +44,7 @@ class service:
"label": self.label, "label": self.label,
"icon_filetype": self.icon_filetype, "icon_filetype": self.icon_filetype,
"id": self.id, "id": self.id,
"ping_type": self.ping_type,
} }
def set_status(self, status: Optional[int]): def set_status(self, status: Optional[int]):
@@ -64,7 +68,9 @@ services: list[service] = [
service(4, "https://request.ihatemen.uk/", "Overseerr"), service(4, "https://request.ihatemen.uk/", "Overseerr"),
service(5, "https://id.ihatemen.uk/", "PocketID"), service(5, "https://id.ihatemen.uk/", "PocketID"),
service(6, "http://tautulli.local", "Tautulli", False), service(6, "http://tautulli.local", "Tautulli", False),
service(7, "https://transmission.local", "Transmission", False), service(
7, "https://transmission.local", "Transmission", False, ping_type=1
),
service(8, "https://vault.ihatemen.uk", "Vault Warden"), service(8, "https://vault.ihatemen.uk", "Vault Warden"),
service(9, "https://nginx.local", "Nginx (NPM)", False), service(9, "https://nginx.local", "Nginx (NPM)", False),
] ]

View File

@@ -7,11 +7,22 @@ import time
async def check_service(client: httpx.AsyncClient, s: service): async def check_service(client: httpx.AsyncClient, s: service):
try: try:
before = time.perf_counter() before = time.perf_counter()
match s.ping_type:
case 0:
r = await client.head(
url=s.url,
follow_redirects=True,
timeout=1,
)
case 1:
r = await client.get( r = await client.get(
url=s.url, url=s.url,
follow_redirects=True, follow_redirects=True,
timeout=1, timeout=1,
) )
case _:
raise httpx.HTTPError("Unknown ping type")
after = time.perf_counter() after = time.perf_counter()
s.set_error(None) s.set_error(None)
s.set_online(r.status_code == 200) s.set_online(r.status_code == 200)
@@ -33,9 +44,10 @@ 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...")
async with httpx.AsyncClient() as public_client, httpx.AsyncClient( async with (
verify=False httpx.AsyncClient() as public_client,
) as local_client: httpx.AsyncClient(verify=False) as local_client,
):
while True: while True:
tasks = [ tasks = [
check_service(public_client if s.public else local_client, s) check_service(public_client if s.public else local_client, s)