Format code according to pep8.

replace get_client_path("/members?recursive=true") with members()
replace do-plpgsq block by simple sql
This commit is contained in:
Alexander Kukushkin
2015-05-08 12:44:28 +02:00
parent 61f0cb2fb4
commit 810808a68a
+12 -5
View File
@@ -1,6 +1,10 @@
#!/usr/bin/env python
import sys, os, yaml, time, urllib2, atexit
import sys
import yaml
import time
import urllib2
import atexit
import logging
from helpers.etcd import Etcd
@@ -19,6 +23,8 @@ postgresql = Postgresql(config["postgresql"])
ha = Ha(postgresql, etcd)
# stop postgresql on script exit
def stop_postgresql():
postgresql.stop()
atexit.register(stop_postgresql)
@@ -63,9 +69,10 @@ while True:
# create replication slots
if postgresql.is_leader():
for node in etcd.get_client_path("/members?recursive=true")["node"]["nodes"]:
member = node["key"].split('/')[-1]
if member != postgresql.name:
postgresql.query("DO LANGUAGE plpgsql $$DECLARE somevar VARCHAR; BEGIN SELECT slot_name INTO somevar FROM pg_replication_slots WHERE slot_name = '%(slot)s' LIMIT 1; IF NOT FOUND THEN PERFORM pg_create_physical_replication_slot('%(slot)s'); END IF; END$$;" % {"slot": member})
for member in etcd.members():
if member['hostname'] != postgresql.name:
postgresql.query("""SELECT pg_create_physical_replication_slot(%s)
WHERE NOT EXISTS (SELECT 1 FROM pg_replication_slots
WHERE slot_name = %s)""", member['hostname'], member['hostname'])
time.sleep(config["loop_wait"])