mirror of
https://github.com/outbackdingo/patroni.git
synced 2026-08-25 14:53:37 +00:00
Start the http request handler for load balancing as a thread in Governor.
When not running standalone, some better referencing to the Postgresql instance was needed.
This commit is contained in:
@@ -2,10 +2,12 @@
|
||||
|
||||
import sys, os, yaml, time, urllib2, atexit
|
||||
import logging
|
||||
import threading
|
||||
|
||||
from helpers.etcd import Etcd
|
||||
from helpers.postgresql import Postgresql
|
||||
from helpers.ha import Ha
|
||||
from helpers.statuspage import StatusPage, getHTTPServer
|
||||
|
||||
INSTANCE_METADATA_URL = "http://169.254.169.254/latest/meta-data/"
|
||||
|
||||
@@ -30,6 +32,12 @@ etcd = Etcd(config["etcd"])
|
||||
postgresql = Postgresql(config["postgresql"], aws_host_address)
|
||||
ha = Ha(postgresql, etcd)
|
||||
|
||||
## Start the http_server to serve a simple healthcheck
|
||||
http_server = getHTTPServer(postgresql, http_port=8080, listen_address='0.0.0.0')
|
||||
http_thread = threading.Thread(target=http_server.serve_forever, args=())
|
||||
http_thread.daemon = True
|
||||
http_thread.start()
|
||||
|
||||
# stop postgresql on script exit
|
||||
def stop_postgresql():
|
||||
postgresql.stop()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from BaseHTTPServer import BaseHTTPRequestHandler
|
||||
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
|
||||
|
||||
|
||||
class StatusPage(BaseHTTPRequestHandler):
|
||||
@@ -9,19 +9,21 @@ class StatusPage(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
try:
|
||||
if self.path == '/pg_master':
|
||||
response = (200 if postgresql.is_leader else 503)
|
||||
response = (200 if self.server.postgresql.is_leader else 503)
|
||||
self.send_response(response)
|
||||
elif self.path == '/pg_slave':
|
||||
response = (503 if postgresql.is_leader else 200)
|
||||
response = (503 if self.server.postgresql.is_leader else 200)
|
||||
self.send_response(response)
|
||||
elif self.path == '/pg_status':
|
||||
self.send_response(200)
|
||||
self.end_headers()
|
||||
self.wfile.write(postgresql.status())
|
||||
self.wfile.write(self.server.postgresql.status())
|
||||
else:
|
||||
self.send_response(404)
|
||||
except:
|
||||
except Exception, e:
|
||||
self.send_response(500)
|
||||
self.end_headers()
|
||||
self.wfile.write(repr(e))
|
||||
|
||||
|
||||
def getHTTPServer(postgresql, http_port=8081, listen_address='0.0.0.0'):
|
||||
|
||||
Reference in New Issue
Block a user