def has_snapshot(self, timeout=600): """ Ask Consul for 'last backup' key. """ while timeout > 0: try: result = self.client.kv.get(LAST_BACKUP_KEY) if result[1]: return json.loads(result[1]['Value'])['id'] return None except pyconsul.ConsulException: # Consul isn't up yet timeout -= 1 time.sleep(1) except (KeyError, TypeError, ValueError): raise # unexpected value / invalid JSON in Consul raise WaitTimeoutError('Could not contact Consul to check ' 'for snapshot after %s seconds', timeout)
def get_primary(self, timeout=10): """ Returns the (name, IP) tuple for the instance that Consul thinks is the healthy primary. """ while timeout > 0: try: nodes = self.client.health.service(PRIMARY_KEY, passing=True)[1] log.debug(nodes) instances = [service['Service'] for service in nodes] if len(instances) > 1: raise UnknownPrimary('Multiple primaries detected! %s', instances) return instances[0]['ID'], instances[0]['Address'] except pyconsul.ConsulException as ex: log.debug(ex) timeout = timeout - 1 time.sleep(1) except (IndexError, KeyError): raise UnknownPrimary('No primary found') raise WaitTimeoutError('Could not find primary before timeout.')
def wait_for_connection(self, user='******', password=None, database=None, timeout=10): """ Polls mysqld socket until we get a connection or the timeout expires (raise WaitTimeoutError). Defaults to root empty/password. """ while timeout > 0: try: sock = '/var/run/mysqld/mysqld.sock' return mysqlconn.connect(unix_socket=sock, user=user, password=password, database=database, charset='utf8', connection_timeout=timeout) except MySQLError as ex: timeout = timeout - 1 if timeout == 0: raise WaitTimeoutError(ex) time.sleep(1)