@@ -24,50 +24,19 @@ class UptimeKumaService:
2424 def __init__ (self , config_service ):
2525 self .config_service = config_service
2626
27- def _retry_api_call (self , func , * args , retries = 5 , delay = 5 , ** kwargs ):
28- """
29- Generic retry wrapper for Uptime Kuma API calls that may timeout.
30- """
31- for attempt in range (1 , retries + 1 ):
32- try :
33- return func (* args , ** kwargs )
34- except socketio .exceptions .TimeoutError :
35- if attempt == retries :
36- Log .info (f"[auto-uptime-kuma] Final attempt { attempt } failed. Giving up." )
37- raise
38- Log .info (
39- f"[auto-uptime-kuma] Attempt { attempt } failed with TimeoutError. Retrying in { delay } seconds..."
40- )
41- time .sleep (delay )
42-
43- def create_monitor (self , container_name , monitor_data ):
44- monitor_data = self .build_monitor_data (container_name , monitor_data )
45- if self .monitor_exists (container_name ):
27+ def connect (self , url , username , password ):
28+ response = requests .get (url , allow_redirects = True , timeout = 5 )
29+ if response .status_code != 200 :
4630 Log .info (
47- f"Uptime Kuma already contains Monitor ' { monitor_data [ 'name' ] } ' "
48- f" for container ' { container_name } ', skipping.. ."
31+ f"Unable to connect to UptimeKuma at ' { url } ' (Status code: { response . status_code } ). "
32+ " Please check if the host is running ."
4933 )
50- return None
34+ return False
5135
52- Log .info (
53- f"Adding Monitor '{ monitor_data ['name' ]} ' for container '{ container_name } '"
54- )
36+ self .api = UptimeKumaApi (url )
37+ self .api .login (username , password )
5538
56- monitor = self ._retry_api_call (self .api .add_monitor , ** monitor_data )
57-
58- self ._retry_api_call (
59- self .api .add_monitor_tag ,
60- tag_id = self .get_swag_tag ()["id" ],
61- monitor_id = monitor ["monitorID" ],
62- value = container_name .lower (),
63- )
64-
65- self .config_service .create_config (container_name , monitor_data )
66-
67- monitor = self ._retry_api_call (self .api .get_monitor , monitor ["monitorID" ])
68- self .monitors .append (monitor )
69-
70- return monitor
39+ return True
7140
7241 def disconnect (self ):
7342 """
@@ -152,18 +121,26 @@ def monitor_exists(self, container_name):
152121
153122 def create_monitor (self , container_name , monitor_data ):
154123 monitor_data = self .build_monitor_data (container_name , monitor_data )
124+ self .validate_monitor_data (monitor_data )
125+
155126 if self .monitor_exists (container_name ):
156127 Log .info (
157128 f"Uptime Kuma already contains Monitor '{ monitor_data ['name' ]} '"
158129 f" for container '{ container_name } ', skipping..."
159130 )
160131 return None
161132
162- Log .info (
163- f"Adding Monitor '{ monitor_data ['name' ]} ' for container '{ container_name } '"
164- )
133+ Log .info (f"Adding Monitor '{ monitor_data ['name' ]} ' for container '{ container_name } '" )
134+ Log .debug (f"Sending monitor data: { monitor_data } " )
165135
166- monitor = self .api .add_monitor (** monitor_data )
136+ try :
137+ monitor = self .api .add_monitor (** monitor_data )
138+ except socketio .exceptions .TimeoutError :
139+ Log .error ("Timeout while trying to add monitor to Uptime Kuma. Is the server responsive?" )
140+ return None
141+ except Exception as e :
142+ Log .error (f"Failed to create monitor due to unexpected error: { e } " )
143+ return None
167144
168145 self .api .add_monitor_tag (
169146 tag_id = self .get_swag_tag ()["id" ],
@@ -172,7 +149,6 @@ def create_monitor(self, container_name, monitor_data):
172149 )
173150
174151 self .config_service .create_config (container_name , monitor_data )
175-
176152 monitor = self .api .get_monitor (monitor ["monitorID" ])
177153 self .monitors .append (monitor )
178154
0 commit comments