mirror of
https://github.com/TrezOne/docker-mods-uptime-kuma-timeout-fix.git
synced 2026-07-15 15:50:22 -04:00
Test #3.
This commit is contained in:
@@ -24,50 +24,19 @@ class UptimeKumaService:
|
|||||||
def __init__(self, config_service):
|
def __init__(self, config_service):
|
||||||
self.config_service = config_service
|
self.config_service = config_service
|
||||||
|
|
||||||
def _retry_api_call(self, func, *args, retries=5, delay=5, **kwargs):
|
def connect(self, url, username, password):
|
||||||
"""
|
response = requests.get(url, allow_redirects=True, timeout=5)
|
||||||
Generic retry wrapper for Uptime Kuma API calls that may timeout.
|
if response.status_code != 200:
|
||||||
"""
|
|
||||||
for attempt in range(1, retries + 1):
|
|
||||||
try:
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
except socketio.exceptions.TimeoutError:
|
|
||||||
if attempt == retries:
|
|
||||||
Log.info(f"[auto-uptime-kuma] Final attempt {attempt} failed. Giving up.")
|
|
||||||
raise
|
|
||||||
Log.info(
|
|
||||||
f"[auto-uptime-kuma] Attempt {attempt} failed with TimeoutError. Retrying in {delay} seconds..."
|
|
||||||
)
|
|
||||||
time.sleep(delay)
|
|
||||||
|
|
||||||
def create_monitor(self, container_name, monitor_data):
|
|
||||||
monitor_data = self.build_monitor_data(container_name, monitor_data)
|
|
||||||
if self.monitor_exists(container_name):
|
|
||||||
Log.info(
|
Log.info(
|
||||||
f"Uptime Kuma already contains Monitor '{monitor_data['name']}'"
|
f"Unable to connect to UptimeKuma at '{url}' (Status code: {response.status_code})."
|
||||||
f" for container '{container_name}', skipping..."
|
" Please check if the host is running."
|
||||||
)
|
)
|
||||||
return None
|
return False
|
||||||
|
|
||||||
Log.info(
|
self.api = UptimeKumaApi(url)
|
||||||
f"Adding Monitor '{monitor_data['name']}' for container '{container_name}'"
|
self.api.login(username, password)
|
||||||
)
|
|
||||||
|
|
||||||
monitor = self._retry_api_call(self.api.add_monitor, **monitor_data)
|
return True
|
||||||
|
|
||||||
self._retry_api_call(
|
|
||||||
self.api.add_monitor_tag,
|
|
||||||
tag_id=self.get_swag_tag()["id"],
|
|
||||||
monitor_id=monitor["monitorID"],
|
|
||||||
value=container_name.lower(),
|
|
||||||
)
|
|
||||||
|
|
||||||
self.config_service.create_config(container_name, monitor_data)
|
|
||||||
|
|
||||||
monitor = self._retry_api_call(self.api.get_monitor, monitor["monitorID"])
|
|
||||||
self.monitors.append(monitor)
|
|
||||||
|
|
||||||
return monitor
|
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
"""
|
"""
|
||||||
@@ -152,6 +121,8 @@ class UptimeKumaService:
|
|||||||
|
|
||||||
def create_monitor(self, container_name, monitor_data):
|
def create_monitor(self, container_name, monitor_data):
|
||||||
monitor_data = self.build_monitor_data(container_name, monitor_data)
|
monitor_data = self.build_monitor_data(container_name, monitor_data)
|
||||||
|
self.validate_monitor_data(monitor_data)
|
||||||
|
|
||||||
if self.monitor_exists(container_name):
|
if self.monitor_exists(container_name):
|
||||||
Log.info(
|
Log.info(
|
||||||
f"Uptime Kuma already contains Monitor '{monitor_data['name']}'"
|
f"Uptime Kuma already contains Monitor '{monitor_data['name']}'"
|
||||||
@@ -159,11 +130,17 @@ class UptimeKumaService:
|
|||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
Log.info(
|
Log.info(f"Adding Monitor '{monitor_data['name']}' for container '{container_name}'")
|
||||||
f"Adding Monitor '{monitor_data['name']}' for container '{container_name}'"
|
Log.debug(f"Sending monitor data: {monitor_data}")
|
||||||
)
|
|
||||||
|
|
||||||
monitor = self.api.add_monitor(**monitor_data)
|
try:
|
||||||
|
monitor = self.api.add_monitor(**monitor_data)
|
||||||
|
except socketio.exceptions.TimeoutError:
|
||||||
|
Log.error("Timeout while trying to add monitor to Uptime Kuma. Is the server responsive?")
|
||||||
|
return None
|
||||||
|
except Exception as e:
|
||||||
|
Log.error(f"Failed to create monitor due to unexpected error: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
self.api.add_monitor_tag(
|
self.api.add_monitor_tag(
|
||||||
tag_id=self.get_swag_tag()["id"],
|
tag_id=self.get_swag_tag()["id"],
|
||||||
@@ -172,7 +149,6 @@ class UptimeKumaService:
|
|||||||
)
|
)
|
||||||
|
|
||||||
self.config_service.create_config(container_name, monitor_data)
|
self.config_service.create_config(container_name, monitor_data)
|
||||||
|
|
||||||
monitor = self.api.get_monitor(monitor["monitorID"])
|
monitor = self.api.get_monitor(monitor["monitorID"])
|
||||||
self.monitors.append(monitor)
|
self.monitors.append(monitor)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user