mirror of
https://github.com/outbackdingo/patroni.git
synced 2026-08-25 14:53:37 +00:00
Process http requests in a threads with ThreadingMixIn
This commit is contained in:
+16
-16
@@ -9,6 +9,7 @@ if sys.hexversion >= 0x03000000:
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
else:
|
||||
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
|
||||
from SocketServer import ThreadingMixIn
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -28,16 +29,14 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
||||
|
||||
def get_postgresql_status(self):
|
||||
try:
|
||||
cursor = self.server._cursor()
|
||||
cursor.execute("""SELECT to_char(pg_postmaster_start_time(), 'YYYY-MM-DD HH24:MI:SS.MS TZ'),
|
||||
pg_is_in_recovery(),
|
||||
CASE WHEN pg_is_in_recovery()
|
||||
THEN null
|
||||
ELSE pg_current_xlog_location() END,
|
||||
pg_last_xlog_receive_location(),
|
||||
pg_last_xlog_replay_location(),
|
||||
pg_is_in_recovery() AND pg_is_xlog_replay_paused()""")
|
||||
row = cursor.fetchone()
|
||||
row = self.server.query("""SELECT to_char(pg_postmaster_start_time(), 'YYYY-MM-DD HH24:MI:SS.MS TZ'),
|
||||
pg_is_in_recovery(),
|
||||
CASE WHEN pg_is_in_recovery()
|
||||
THEN null
|
||||
ELSE pg_current_xlog_location() END,
|
||||
pg_last_xlog_receive_location(),
|
||||
pg_last_xlog_replay_location(),
|
||||
pg_is_in_recovery() AND pg_is_xlog_replay_paused()""")[0]
|
||||
return {
|
||||
'running': True,
|
||||
'postmaster_start_time': row[0],
|
||||
@@ -54,7 +53,7 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
||||
return {'running': self.server.governor.postgresql.is_running()}
|
||||
|
||||
|
||||
class RestApiServer(HTTPServer, Thread):
|
||||
class RestApiServer(ThreadingMixIn, HTTPServer, Thread):
|
||||
|
||||
def __init__(self, governor, config):
|
||||
self.connection_string = 'http://{}/governor'.format(config.get('connect_address', None) or config['listen'])
|
||||
@@ -62,10 +61,11 @@ class RestApiServer(HTTPServer, Thread):
|
||||
HTTPServer.__init__(self, (host, int(port)), RestApiHandler)
|
||||
Thread.__init__(self, target=self.serve_forever)
|
||||
self.governor = governor
|
||||
self._cursor_holder = None
|
||||
self.daemon = True
|
||||
|
||||
def _cursor(self):
|
||||
if not self._cursor_holder or self._cursor_holder.closed:
|
||||
self._cursor_holder = self.governor.postgresql.connection().cursor()
|
||||
return self._cursor_holder
|
||||
def query(self, sql, *params):
|
||||
cursor = self.governor.postgresql.connection().cursor()
|
||||
cursor.execute(sql, params)
|
||||
ret = [r for r in cursor]
|
||||
cursor.close()
|
||||
return ret
|
||||
|
||||
+1
-2
@@ -44,8 +44,7 @@ class MockRestApiServer(RestApiServer):
|
||||
def __init__(self, Handler, path, *args):
|
||||
self.governor = MockGovernor()
|
||||
if len(args) > 0:
|
||||
self._cursor = args[0]
|
||||
self._cursor_holder = None
|
||||
self.query = args[0]
|
||||
Handler(MockRequest(path), ('0.0.0.0', 8080), self)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user