28
28
import socket
29
29
import fcntl
30
30
import struct
31
+ from checkbox_support .helpers .retry import retry
31
32
32
33
33
- def send_request_to_wol_server (url , data = None , retry = 3 ):
34
+ @retry (max_attempts = 3 , delay = 2 )
35
+ def send_request_to_wol_server (url , data = None ):
34
36
# Convert data to JSON format
35
37
data_encoded = json .dumps (data ).encode ("utf-8" )
36
38
37
39
# Construct request
38
40
headers = {"Content-Type" : "application/json" }
39
41
req = urllib .request .Request (url , data = data_encoded , headers = headers )
40
42
41
- attempts = 0
42
- while attempts < retry :
43
- try :
44
- with urllib .request .urlopen (req ) as response :
45
- logging .info ("in the urllib request." )
46
- response_data = json .loads (response .read ().decode ("utf-8" ))
47
- logging .debug (
48
- "Response message: {}" .format (response_data ["message" ])
43
+ logging .info ("Sending request to Wake-on-LAN server." )
44
+
45
+ try :
46
+ with urllib .request .urlopen (req ) as response :
47
+ logging .info ("In the urllib request." )
48
+ response_data = json .loads (response .read ().decode ("utf-8" ))
49
+ logging .debug (
50
+ "Response message: {}" .format (response_data ["message" ])
51
+ )
52
+ status_code = response .status
53
+ logging .debug ("Status code: {}" .format (status_code ))
54
+
55
+ if status_code == 200 :
56
+ logging .info (
57
+ "Request to Wake-on-LAN server sent successfully."
49
58
)
50
- status_code = response .status
51
- logging .debug ("Status code: {}" .format (status_code ))
52
- if status_code == 200 :
53
- logging .info (
54
- "Request to Wake-on-LAN server sent successfully."
59
+ return
60
+ else :
61
+ # If the status code is not 200, we regard the attempt as
62
+ # failed and throw an exception so that the decorator can
63
+ # catch it and retry.
64
+ raise RuntimeError (
65
+ "WOL server returned non-200 status: {}" .format (
66
+ status_code
55
67
)
56
- return
57
- else :
58
- logging .error (
59
- "Failed to send request to Wake-on-LAN server."
60
- )
61
- except Exception as e :
62
- logging .error ("An unexpected error occurred: {}" .format (e ))
68
+ )
63
69
64
- attempts += 1
65
- time .sleep (1 ) # Wait for a second before retrying
66
- logging .debug ("Retrying... ({}/{})" .format (attempts , retry ))
70
+ except urllib .error .URLError as e :
71
+ # Handle connection errors like "Connection refused"
72
+ error_msg = "Failed to connect to WOL server {}: {}" .format (
73
+ url , e .reason
74
+ )
67
75
68
- raise SystemExit (
69
- "Failed to send request to WOL server. "
70
- "Please ensure the WOL server setup correctlly."
71
- )
76
+ # Provide more specific error messages for common connection issues
77
+ if hasattr (e .reason , "errno" ):
78
+ if e .reason .errno == 111 : # Connection refused
79
+ error_msg = (
80
+ "Connection refused - WOL server may not be "
81
+ "running at {}" .format (url )
82
+ )
83
+ elif e .reason .errno == 110 : # Connection timed out
84
+ error_msg = (
85
+ "Connection timed out - server took too long to "
86
+ "respond at {}" .format (url )
87
+ )
88
+
89
+ logging .error (error_msg )
90
+ # Re-raise as RuntimeError to trigger retry mechanism
91
+ raise RuntimeError (error_msg ) from e
92
+
93
+ except json .JSONDecodeError as e :
94
+ # Handle JSON parsing errors
95
+ error_msg = "Failed to parse server response as JSON: {}" .format (e )
96
+ logging .error (error_msg )
97
+ raise RuntimeError (error_msg ) from e
98
+
99
+ except Exception as e :
100
+ # Catch any other unexpected exceptions
101
+ error_msg = (
102
+ "Unexpected error while sending request to WOL server: {}" .format (
103
+ e
104
+ )
105
+ )
106
+ logging .error (error_msg )
107
+ raise RuntimeError (error_msg ) from e
72
108
73
109
74
110
def check_wakeup (interface ):
@@ -241,7 +277,7 @@ def main():
241
277
)
242
278
243
279
delay = args .delay
244
- retry = args .retry
280
+ num_retry = args .retry
245
281
246
282
ip = get_ip_address (args .interface )
247
283
mac = get_mac_address (args .interface )
@@ -260,9 +296,9 @@ def main():
260
296
"wake_type" : args .waketype ,
261
297
}
262
298
263
- send_request_to_wol_server (url , data = req , retry = retry )
299
+ send_request_to_wol_server (url , data = req )
264
300
265
- bring_up_system ("rtc" , delay * retry * 2 )
301
+ bring_up_system ("rtc" , delay * num_retry * 2 )
266
302
267
303
# write the time stamp
268
304
write_timestamp (args .timestamp_file )
0 commit comments