-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappup.py
More file actions
81 lines (72 loc) · 2.87 KB
/
appup.py
File metadata and controls
81 lines (72 loc) · 2.87 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import time
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
datefmt="[%Y-%m-%d][%H:%M:%S]:",
level=logging.INFO)
logger = logging.getLogger(__name__)
"""
this simple webserver is intended to provide a nonblocking endpoint to control application availability.
"""
class UptimeHandler(BaseHTTPRequestHandler):
#Handler for the GET requests
def do_GET(self):
upfile = os.getenv('UP_FILE', '/pod/up.txt')
if self.path == '/up':
try:
if not os.path.exists(upfile):
logger.info("creating upfile")
os.mknod(upfile)
else:
logger.info("app is already up")
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes("app is up", "utf-8"))
except Exception as e:
logger.error(e)
self.send_response(503)
elif self.path == '/down':
try:
if os.path.exists(upfile):
logger.info("removing upfile")
os.remove(upfile)
else:
logger.info("app is already down")
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes("app is down", "utf-8"))
except Exception as e:
logger.error(e)
self.send_response(503)
elif self.path == '/health':
try:
if os.path.exists(upfile):
logger.info("app is up")
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes("app is up", "utf-8"))
else:
logger.info("app is down")
self.send_response(503)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes("app is down", "utf-8"))
except Exception as e:
logger.error(e)
self.send_response(503)
else:
self.send_response(404)
def main(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
## set variables
upfile = os.getenv('UP_FILE', '/pod/up.txt')
server_port = os.getenv('UP_PORT', 9999)
server = HTTPServer(('', int(server_port)), UptimeHandler)
logger.info("starting appup service on {0}".format(server_port))
#Wait forever for incoming http requests
server.serve_forever()
if __name__== "__main__":
main()