예제 #1
0
 def retrieve(cls):
     try:
         return pickle.loads(CacheService.get(cls.cache_key))
     except KeyError:
         LogService.warning("Attempted to retrieve '%s' but it was empty. Repopulating..." % cls.cache_key)
         cls.populate()
         return pickle.loads(CacheService.get(cls.cache_key))
예제 #2
0
파일: zookeeper.py 프로젝트: cohoe/barbados
    def _connect(self):
        """
        [re]Connect to the ZooKeeper host(s). Create a local attribute to this class
        that acts as the client and does the actual interactions with ZK.
        :return: None
        """
        # If we don't have a self.zk attribute then we've never connected.
        if not hasattr(self, 'zk'):
            self.zk = KazooClient(hosts=self.hosts,
                                  read_only=self.read_only,
                                  timeout=5,
                                  connection_retry=self._get_retry())
        # Any state not connected is a bad thing. Warn and continue with execution below.
        elif self.zk.state != KazooState.CONNECTED:
            LogService.warning("ZooKeeper state is %s" % self.zk.state)
            pass
        # Connected state is good. Do nothing.
        elif self.zk.state == KazooState.CONNECTED:
            return
        # I'm not sure if this actually does anything...
        else:
            raise Exception("We in a weird state. %s" % self.zk.state)

        # Start the connection now that it's guaranteed to exist.
        try:
            return self.zk.start()
        except KazooTimeoutError as e:
            raise FatalException("Timeout connecting to ZooKeeper (%s)" % e)
예제 #3
0
파일: base.py 프로젝트: cohoe/barbados
 def retrieve(cls):
     """
     Retrieve the cache's value
     :return: Various
     """
     try:
         return CacheService.get(cls.cache_key)
     except KeyError:
         LogService.warning("Attempted to retrieve '%s' but it was empty. Repopulating..." % cls.cache_key)
         cls.populate()
         return CacheService.get(cls.cache_key)
예제 #4
0
파일: __init__.py 프로젝트: cohoe/barbados
 def init(self):
     """
     Re-initialize all indexes. This calls rebuild on every registered
     index class. There be dragons here.
     :return: None
     """
     for name in self._indexes.keys():
         LogService.debug("Init on %s" % name)
         try:
             self.rebuild(self._indexes.get(name))
         except NotFoundError or KeyError or AttributeError as e:
             LogService.warning("Error re-initing index %s: %s" % (name, e))
예제 #5
0
파일: __init__.py 프로젝트: cohoe/barbados
    def rebuild(self, index_class):
        """
        Re-create an index. This deletes the entire index (not just the contents,
        but the Whole Damn Thing(tm). and re-creates it.
        :param index_class: elasticsearch_dsl.Document child representing this index.
        :return: None
        """
        try:
            index_class._index.delete()
        except NotFoundError:
            LogService.warning("Index %s did not exist." %
                               index_class.Index.name)

        # Proceed with rebuild.
        index_class.init()
        LogService.info("Successfully rebuilt index %s" %
                        index_class.Index.name)
예제 #6
0
    def __init__(self, username, password, host, port, database, debug_sql):
        self.username = username
        self.password = password
        self.host = host
        self.port = port
        self.database = database
        self.debug_sql = debug_sql

        connection_string = "postgres://%s:%s@%s:%i/%s" % (
            self.username, self.password, self.host, self.port, self.database)

        # https://stackoverflow.com/questions/48995979/how-to-replace-all-characters-in-a-string-with-one-character/48996018
        masked_connection_string = connection_string.replace(
            self.password, '*' * len(self.password))
        LogService.info("Postgres string: %s" % masked_connection_string)
        LogService.warning('Starting PostgreSQL connection!')

        self.engine = sqlalchemy.create_engine(connection_string,
                                               echo=self.debug_sql)
        self.Session = sessionmaker(bind=self.engine)
        self.ScopedSession = scoped_session(self.Session)

        self._setup_events()