33
44import sys
55import os
6+ import json
67import requests
78from pathlib import Path
9+ import time
810
911def check_web_server ():
1012 """Check if web server is responding."""
11- if os .environ .get ('ENABLE_WEB' , 'true' ).lower () == 'true' :
12- port = os .environ .get ('WEB_PORT' , '8888' )
13- try :
14- response = requests .get (f'http://localhost:{ port } /data.json' , timeout = 3 )
15- return response .status_code == 200
16- except :
13+ if os .environ .get ('ENABLE_WEB' , 'true' ).lower () != 'true' :
14+ return True # Web server disabled, skip check
15+
16+ port = os .environ .get ('WEB_PORT' , '8888' )
17+ try :
18+ # Check health endpoint
19+ response = requests .get (f'http://localhost:{ port } /api/health' , timeout = 3 )
20+ if response .status_code != 200 :
21+ return False
22+
23+ # Verify response is valid JSON and check status
24+ data = response .json ()
25+ if not isinstance (data , dict ):
1726 return False
18- return True
27+
28+ # Check the reported status
29+ status = data .get ('status' , 'unknown' )
30+ if status == 'healthy' :
31+ return True
32+ elif status == 'degraded' :
33+ print (f"Warning: Service is degraded - { data .get ('checks' , {})} " )
34+ return True # Still return True for degraded state
35+ else :
36+ print (f"Error: Service status is { status } " )
37+ return False
38+
39+ except Exception as e :
40+ print (f"Web server check error: { e } " )
41+ return False
1942
2043def check_process ():
2144 """Check if main process is running."""
@@ -25,23 +48,72 @@ def check_process():
2548 result = subprocess .run (['pgrep' , '-f' , 'python3.*app.py' ],
2649 capture_output = True , text = True )
2750 return result .returncode == 0
28- except :
51+ except Exception as e :
52+ print (f"Process check error: { e } " )
53+ return False
54+
55+ def check_tar1090_connection ():
56+ """Check if we can connect to TAR1090."""
57+ tar1090_url = os .environ .get ('TAR1090_URL' , 'http://tar1090:80' )
58+ try :
59+ # Allow this check to fail gracefully as TAR1090 might be temporarily unavailable
60+ response = requests .get (f'{ tar1090_url } /data/aircraft.json' , timeout = 5 )
61+ if response .status_code == 200 :
62+ data = response .json ()
63+ # Check if we got valid aircraft data
64+ if 'aircraft' in data or 'ac' in data :
65+ return True
66+ # Don't fail health check if TAR1090 is temporarily unavailable
67+ # Just log a warning
68+ print (f"Warning: TAR1090 at { tar1090_url } not responding (status: { response .status_code } )" )
69+ return True # Return True to not fail the container
70+ except requests .exceptions .RequestException as e :
71+ print (f"Warning: Cannot connect to TAR1090 at { tar1090_url } : { e } " )
72+ return True # Return True to not fail the container
73+ except Exception as e :
74+ print (f"Warning: TAR1090 check error: { e } " )
75+ return True # Return True to not fail the container
76+
77+ def check_log_files ():
78+ """Check if log files are writable."""
79+ try :
80+ # Check if we can write to log directory
81+ log_dir = Path ('/app' )
82+ if not log_dir .exists ():
83+ return False
84+
85+ # Test write access
86+ test_file = log_dir / '.healthcheck_test'
87+ test_file .write_text (str (time .time ()))
88+ test_file .unlink ()
89+ return True
90+ except Exception as e :
91+ print (f"Log file check error: { e } " )
2992 return False
3093
3194def main ():
3295 """Run health checks."""
96+ # Define checks with their names and criticality
3397 checks = [
34- ("Process" , check_process ()),
35- ("Web Server" , check_web_server ()),
98+ ("Process" , check_process (), True ), # Critical
99+ ("Web Server" , check_web_server (), True ), # Critical
100+ ("Log Files" , check_log_files (), True ), # Critical
101+ ("TAR1090 Connection" , check_tar1090_connection (), False ), # Non-critical
36102 ]
37103
38- failed = [name for name , status in checks if not status ]
104+ # Separate critical and non-critical failures
105+ critical_failures = [name for name , status , critical in checks if not status and critical ]
106+ warnings = [name for name , status , critical in checks if not status and not critical ]
107+
108+ # Print status
109+ if warnings :
110+ print (f"Health check warnings: { ', ' .join (warnings )} " )
39111
40- if failed :
41- print (f"Health check failed : { ', ' .join (failed )} " )
112+ if critical_failures :
113+ print (f"Health check FAILED : { ', ' .join (critical_failures )} " )
42114 sys .exit (1 )
43115
44- print ("Health check passed " )
116+ print ("Health check PASSED " )
45117 sys .exit (0 )
46118
47119if __name__ == "__main__" :
0 commit comments