def _establish_connection()

in cdsreaper/journal.py [0:0]


    def _establish_connection(self, attempt=1):
        """
        internal method that sets up a connection object and pings the server to check we can talk to it.
        called automatically during construction
        retries if a connection can't be established
        can raise redis exceptions or python base connection exceptions
        :param attempt: don't set this, it's used internally to count retries
        :return: nothing
        """
        try:
            self._conn = redis.Redis(self.redis_host, self.redis_port, self.redis_db, password=self.redis_pw)
            self._conn.ping()
        except (redis.RedisError, ConnectionError) as err:
            if attempt>=self.max_retries:
                logger.error("Could not connect to redis at {0}:{1} after {2} attempts: {3}".format(self.redis_host, self.redis_port, attempt, str(err)))
                raise

            retry_delay = 2*attempt
            logger.warning("Could not connect to redis at {0}:{1} on attempt {2}, retrying in {3}s...".format(self.redis_host, self.redis_port, attempt, retry_delay))
            time.sleep(retry_delay)
            return self._establish_connection(attempt+1)