- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_loop_output.py
More file actions
Latest commit
58 lines (42 loc) · 1.68 KB
/
Copy pathpython_loop_output.py
File metadata and controls
58 lines (42 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Kevin test loop
# Used for debugging and testing Kubernetes pods
importos
importsignal
importsys
importthreading
fromdatetimeimportdatetime, timezone
fromhttp.serverimportBaseHTTPRequestHandler, HTTPServer
version="4.0"
POD_NAME=os.getenv("POD_NAME", "unknown")
POD_NAMESPACE=os.getenv("POD_NAMESPACE", "unknown")
NODE_NAME=os.getenv("NODE_NAME", "unknown")
shutdown_event=threading.Event()
defhandle_shutdown(signum, frame):
current_time=datetime.now(timezone.utc).strftime("%H:%M:%S")
print(f"[{current_time}] Received signal {signum}, shutting down gracefully...")
shutdown_event.set()
signal.signal(signal.SIGTERM, handle_shutdown)
signal.signal(signal.SIGINT, handle_shutdown)
classHealthHandler(BaseHTTPRequestHandler):
defdo_GET(self):
ifself.path=="/health":
self.send_response(200)
self.end_headers()
self.wfile.write(b"ok")
else:
self.send_response(404)
self.end_headers()
deflog_message(self, format, *args):
pass# suppress HTTP access logs
threading.Thread(target=lambda: HTTPServer(("", 8080), HealthHandler).serve_forever(), daemon=True).start()
print(f"Version {version} starting | pod={POD_NAME} namespace={POD_NAMESPACE} node={NODE_NAME}")
print(f"Health endpoint listening on :8080/health")
i=0
whileTrue:
current_time=datetime.now(timezone.utc).strftime("%H:%M:%S")
print(f"Version: {version}: [{i}] pod={POD_NAME} node={NODE_NAME} Current Time = {current_time}")
i+=1
ifshutdown_event.wait(timeout=60):
break
print(f"[{datetime.now(timezone.utc).strftime('%H:%M:%S')}] Shutdown complete.")
sys.exit(0)