Store in etcd endpoint of restapi

In order to have backward compatibility connection url of restapi is
passed as application_name parameter in connection url of database.
Value in etcd will look like:
postgres://user:passwd@host:5432/postgres?application_name=http://host:8080/governor
Sure, this is a hack, but I guess it worth it.
This commit is contained in:
Alexander Kukushkin
2015-05-27 14:46:35 +02:00
parent 77911144c7
commit 5a817fad0d
2 changed files with 11 additions and 7 deletions
+5 -3
View File
@@ -35,9 +35,12 @@ class Governor:
self.etcd = Etcd(config['etcd'])
self.postgresql = Postgresql(config['postgresql'])
self.ha = Ha(self.postgresql, self.etcd)
host, port = config['restapi']['listen'].split(':')
self.api = RestApiServer(self, config['restapi'])
def touch_member(self, ttl=None):
return self.etcd.touch_member(self.postgresql.name, self.postgresql.connection_string, ttl)
connection_string = self.postgresql.connection_string + '?application_name=' + self.api.connection_string
return self.etcd.touch_member(self.postgresql.name, connection_string, ttl)
def initialize(self):
# wait for etcd to be available
@@ -65,6 +68,7 @@ class Governor:
self.postgresql.load_replication_slots()
def run(self):
self.api.start()
while True:
self.touch_member()
logging.info(self.ha.run_cycle())
@@ -86,8 +90,6 @@ def main():
governor = Governor(config)
try:
governor.initialize()
host, port = config['restapi']['listen'].split(':')
RestApiServer(governor, host, int(port)).start()
governor.run()
finally:
governor.touch_member(300) # schedule member removal
+6 -4
View File
@@ -34,7 +34,7 @@ class RestApiHandler(BaseHTTPRequestHandler):
def get_postgresql_status(self):
if not self.server.governor.postgresql.is_running():
return {'running': False}
cursor = self.server.cursor()
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()
@@ -59,14 +59,16 @@ class RestApiHandler(BaseHTTPRequestHandler):
class RestApiServer(HTTPServer, Thread):
def __init__(self, governor, listen_address='0.0.0.0', listen_port=8080):
HTTPServer.__init__(self, (listen_address, listen_port), RestApiHandler)
def __init__(self, governor, config):
self.connection_string = 'http://{}/governor'.format(config.get('connect_address', None) or config['listen'])
host, port = config['listen'].split(':')
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):
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