mirror of
https://github.com/TrezOne/docker-mods-uptime-kuma-timeout-fix.git
synced 2026-06-15 04:43:01 -04:00
Test #3.
This commit is contained in:
@@ -24,50 +24,19 @@ class UptimeKumaService:
|
||||
def __init__(self, config_service):
|
||||
self.config_service = config_service
|
||||
|
||||
def _retry_api_call(self, func, *args, retries=5, delay=5, **kwargs):
|
||||
"""
|
||||
Generic retry wrapper for Uptime Kuma API calls that may timeout.
|
||||
"""
|
||||
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):
|
||||
def connect(self, url, username, password):
|
||||
response = requests.get(url, allow_redirects=True, timeout=5)
|
||||
if response.status_code != 200:
|
||||
Log.info(
|
||||
f"Uptime Kuma already contains Monitor '{monitor_data['name']}'"
|
||||
f" for container '{container_name}', skipping..."
|
||||
f"Unable to connect to UptimeKuma at '{url}' (Status code: {response.status_code})."
|
||||
" Please check if the host is running."
|
||||
)
|
||||
return None
|
||||
return False
|
||||
|
||||
Log.info(
|
||||
f"Adding Monitor '{monitor_data['name']}' for container '{container_name}'"
|
||||
)
|
||||
self.api = UptimeKumaApi(url)
|
||||
self.api.login(username, password)
|
||||
|
||||
monitor = self._retry_api_call(self.api.add_monitor, **monitor_data)
|
||||
|
||||
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
|
||||
return True
|
||||
|
||||
def disconnect(self):
|
||||
"""
|
||||
@@ -152,6 +121,8 @@ class UptimeKumaService:
|
||||
|
||||
def create_monitor(self, 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):
|
||||
Log.info(
|
||||
f"Uptime Kuma already contains Monitor '{monitor_data['name']}'"
|
||||
@@ -159,11 +130,17 @@ class UptimeKumaService:
|
||||
)
|
||||
return None
|
||||
|
||||
Log.info(
|
||||
f"Adding Monitor '{monitor_data['name']}' for container '{container_name}'"
|
||||
)
|
||||
Log.info(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(
|
||||
tag_id=self.get_swag_tag()["id"],
|
||||
@@ -172,7 +149,6 @@ class UptimeKumaService:
|
||||
)
|
||||
|
||||
self.config_service.create_config(container_name, monitor_data)
|
||||
|
||||
monitor = self.api.get_monitor(monitor["monitorID"])
|
||||
self.monitors.append(monitor)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user