Skip to content

Commit

Permalink
Cosmetic changes: avoid magic strings in action names, name the param…
Browse files Browse the repository at this point in the history
…eter passed to the callback 'scope' for consistency.
  • Loading branch information
alexeyklyukin committed Jul 30, 2015
1 parent 71a1200 commit a0fdd63
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions helpers/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

logger = logging.getLogger(__name__)

ACTION_ON_START = "on_start"
ACTION_ON_STOP = "on_stop"
ACTION_ON_RESTART = "on_restart"
ACTION_ON_RELOAD = "on_reload"
ACTION_ON_ROLE_CHANGE = "on_role_change"


def parseurl(url):
r = urlparse(url)
Expand Down Expand Up @@ -253,12 +259,12 @@ def call_nowait(self, cb_name, is_leader=None):
except psycopg2.OperationalError as e:
logger.warning("unable to perform {0} action, cannot obtain the cluster role: {1}".format(cb_name, e))
return False
name = self.scope
scope = self.scope
try:
role = "master" if is_leader else "replica"
subprocess.Popen(shlex.split(os.path.abspath(cmd))+[cb_name, role, name])
subprocess.Popen(shlex.split(os.path.abspath(cmd))+[cb_name, role, scope])
except Exception as e:
logger.warning("callback {0} {1} {2} {3} failed: {4}".format(os.path.abspath(cmd), cb_name, role, name, e))
logger.warning("callback {0} {1} {2} {3} failed: {4}".format(os.path.abspath(cmd), cb_name, role, scope, e))
return False
return True

Expand All @@ -275,8 +281,8 @@ def start(self):
ret = subprocess.call(self._pg_ctl + ['start', '-o', self.server_options()]) == 0
ret and self.load_replication_slots()
self.save_configuration_files()
if ret and 'on_start' in self.callback:
self.call_nowait('on_start')
if ret and ACTION_ON_START in self.callback:
self.call_nowait(ACTION_ON_START)
return ret

def stop(self):
Expand All @@ -286,14 +292,14 @@ def stop(self):
is_leader = None
pass
ret = subprocess.call(self._pg_ctl + ['stop', '-m', 'fast'])
if ret == 0 and 'on_stop' in self.callback:
self.call_nowait('on_stop', is_leader=is_leader)
if ret == 0 and ACTION_ON_STOP in self.callback:
self.call_nowait(ACTION_ON_STOP, is_leader=is_leader)
return ret == 0

def reload(self):
ret = subprocess.call(self._pg_ctl + ['reload'])
if ret == 0 and 'on_reload' in self.callback:
self.call_nowait('on_reload')
if ret == 0 and ACTION_ON_RELOAD in self.callback:
self.call_nowait(ACTION_ON_RELOAD)
return ret == 0

def restart(self):
Expand All @@ -303,8 +309,8 @@ def restart(self):
is_leader = None
pass
ret = subprocess.call(self._pg_ctl + ['restart', '-m', 'fast'])
if ret == 0 and 'on_restart' in self.callback:
self.call_nowait('on_restart', is_leader=is_leader)
if ret == 0 and ACTION_ON_RESTART in self.callback:
self.call_nowait(ACTION_ON_RESTART, is_leader=is_leader)
return ret == 0

def server_options(self):
Expand Down Expand Up @@ -392,8 +398,8 @@ def follow_the_leader(self, leader):
if not self.check_recovery_conf(leader):
self.write_recovery_conf(leader)
self.restart()
if 'on_role_change' in self.callback:
self.call_nowait('on_role_change')
if ACTION_ON_ROLE_CHANGE in self.callback:
self.call_nowait(ACTION_ON_ROLE_CHANGE)

def save_configuration_files(self):
"""
Expand All @@ -413,8 +419,8 @@ def restore_configuration_files(self):

def promote(self):
self.is_promoted = subprocess.call(self._pg_ctl + ['promote']) == 0
if self.is_promoted and 'on_role_change' in self.callback:
self.call_nowait('on_role_change')
if self.is_promoted and ACTION_ON_ROLE_CHANGE in self.callback:
self.call_nowait(ACTION_ON_ROLE_CHANGE)
return self.is_promoted

def demote(self, leader):
Expand Down

0 comments on commit a0fdd63

Please sign in to comment.