Python redis.exceptions.ConnectionError() Examples

The following are 30 code examples of redis.exceptions.ConnectionError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module redis.exceptions , or try the search function .
Example #1
Source File: nextid.py    From pottery with Apache License 2.0 8 votes vote down vote up
def _current_id(self, value):
        futures, num_masters_set = set(), 0
        with concurrent.futures.ThreadPoolExecutor() as executor:
            for master in self.masters:
                future = executor.submit(
                    self._set_id_script,
                    keys=(self.key,),
                    args=(value,),
                    client=master,
                )
                futures.add(future)
            for future in concurrent.futures.as_completed(futures):
                with contextlib.suppress(TimeoutError, ConnectionError):
                    num_masters_set += future.result() == value
        if num_masters_set < len(self.masters) // 2 + 1:
            raise QuorumNotAchieved(self.masters, self.key) 
Example #2
Source File: action_handler.py    From scrapy-cluster with MIT License 6 votes vote down vote up
def setup(self, settings):
        '''
        Setup redis and tldextract
        '''
        self.extract = tldextract.TLDExtract()
        self.redis_conn = redis.Redis(host=settings['REDIS_HOST'],
                                      port=settings['REDIS_PORT'],
                                      db=settings.get('REDIS_DB'))

        try:
            self.redis_conn.info()
            self.logger.debug("Connected to Redis in ActionHandler")
        except ConnectionError:
            self.logger.error("Failed to connect to Redis in ActionHandler")
            # plugin is essential to functionality
            sys.exit(1) 
Example #3
Source File: sentinel.py    From satori with Apache License 2.0 6 votes vote down vote up
def discover_master(self, service_name):
        """
        Asks sentinel servers for the Redis master's address corresponding
        to the service labeled ``service_name``.

        Returns a pair (address, port) or raises MasterNotFoundError if no
        master is found.
        """
        for sentinel_no, sentinel in enumerate(self.sentinels):
            try:
                masters = sentinel.sentinel_masters()
            except ConnectionError:
                continue
            state = masters.get(service_name)
            if state and self.check_master_state(state, service_name):
                # Put this sentinel at the top of the list
                self.sentinels[0], self.sentinels[sentinel_no] = (
                    sentinel, self.sentinels[0])
                return state['ip'], state['port']
        raise MasterNotFoundError("No master found for %r" % (service_name,)) 
Example #4
Source File: clients.py    From rb with Apache License 2.0 6 votes vote down vote up
def get_connection(self, command_name, shard_hint=None):
        host_id = shard_hint
        if host_id is None:
            raise RuntimeError('The routing pool requires the host id '
                               'as shard hint')

        real_pool = self.cluster.get_pool_for_host(host_id)

        # When we check something out from the real underlying pool it's
        # very much possible that the connection is stale.  This is why we
        # check out up to 10 connections which are either not connected
        # yet or verified alive.
        for _ in xrange(10):
            con = real_pool.get_connection(command_name)
            if con._sock is None or not is_closed(con._sock):
                con.__creating_pool = weakref(real_pool)
                return con

        raise ConnectionError('Failed to check out a valid connection '
                              '(host %s)' % host_id) 
Example #5
Source File: index.py    From watchdog with Apache License 2.0 6 votes vote down vote up
def listManagement(self, vendor=None, product=None):
    try:
      if product is None:
        # no product selected yet, so same function as /browse can be used
        if vendor:
          vendor = urllib.parse.quote_plus(vendor).lower()
        browseList = query.getBrowseList(vendor)
        vendor = browseList["vendor"]
        product = browseList["product"]
        version = None
      else:
        # product selected, product versions required
        version = query.getVersionsOfProduct(urllib.parse.quote_plus(product).lower())
      return render_template('listmanagement.html', vendor=vendor, product=product, version=version)
    except redisExceptions.ConnectionError:
      return render_template('error.html',
                             status={'except':'redis-connection',
                                     'info':{'host':Configuration.getRedisHost(),'port':Configuration.getRedisPort()}})


  # /admin/listmanagement/add 
Example #6
Source File: clients.py    From rb with Apache License 2.0 6 votes vote down vote up
def execute_command(self, *args, **options):
        pool = self.connection_pool
        command_name = args[0]
        command_args = args[1:]
        router = self.connection_pool.cluster.get_router()
        host_id = router.get_host_for_command(command_name, command_args)
        connection = pool.get_connection(command_name, shard_hint=host_id)
        try:
            connection.send_command(*args)
            return self.parse_response(connection, command_name, **options)
        except (ConnectionError, TimeoutError) as e:
            connection.disconnect()
            if not connection.retry_on_timeout and isinstance(e, TimeoutError):
                raise
            connection.send_command(*args)
            return self.parse_response(connection, command_name, **options)
        finally:
            pool.release(connection)

    # Custom Public API 
Example #7
Source File: sentinel.py    From Flask with Apache License 2.0 6 votes vote down vote up
def discover_master(self, service_name):
        """
        Asks sentinel servers for the Redis master's address corresponding
        to the service labeled ``service_name``.

        Returns a pair (address, port) or raises MasterNotFoundError if no
        master is found.
        """
        for sentinel_no, sentinel in enumerate(self.sentinels):
            try:
                masters = sentinel.sentinel_masters()
            except ConnectionError:
                continue
            state = masters.get(service_name)
            if state and self.check_master_state(state, service_name):
                # Put this sentinel at the top of the list
                self.sentinels[0], self.sentinels[sentinel_no] = (
                    sentinel, self.sentinels[0])
                return state['ip'], state['port']
        raise MasterNotFoundError("No master found for %r" % (service_name,)) 
Example #8
Source File: sentinel.py    From gimel with MIT License 6 votes vote down vote up
def discover_master(self, service_name):
        """
        Asks sentinel servers for the Redis master's address corresponding
        to the service labeled ``service_name``.

        Returns a pair (address, port) or raises MasterNotFoundError if no
        master is found.
        """
        for sentinel_no, sentinel in enumerate(self.sentinels):
            try:
                masters = sentinel.sentinel_masters()
            except ConnectionError:
                continue
            state = masters.get(service_name)
            if state and self.check_master_state(state, service_name):
                # Put this sentinel at the top of the list
                self.sentinels[0], self.sentinels[sentinel_no] = (
                    sentinel, self.sentinels[0])
                return state['ip'], state['port']
        raise MasterNotFoundError("No master found for %r" % (service_name,)) 
Example #9
Source File: scraper_handler.py    From scrapy-cluster with MIT License 6 votes vote down vote up
def setup(self, settings):
        '''
        Setup redis and tldextract
        '''
        self.extract = tldextract.TLDExtract()
        self.redis_conn = redis.Redis(host=settings['REDIS_HOST'],
                                      port=settings['REDIS_PORT'],
                                      db=settings.get('REDIS_DB'))

        try:
            self.redis_conn.info()
            self.logger.debug("Connected to Redis in ScraperHandler")
        except ConnectionError:
            self.logger.error("Failed to connect to Redis in ScraperHandler")
            # plugin is essential to functionality
            sys.exit(1) 
Example #10
Source File: zookeeper_handler.py    From scrapy-cluster with MIT License 6 votes vote down vote up
def setup(self, settings):
        '''
        Setup redis and tldextract
        '''
        self.extract = tldextract.TLDExtract()
        self.redis_conn = redis.Redis(host=settings['REDIS_HOST'],
                                      port=settings['REDIS_PORT'],
                                      db=settings.get('REDIS_DB'))

        try:
            self.redis_conn.info()
            self.logger.debug("Connected to Redis in ZookeeperHandler")
        except ConnectionError:
            self.logger.error("Failed to connect to Redis in ZookeeperHandler")
            # plugin is essential to functionality
            sys.exit(1) 
Example #11
Source File: cache.py    From orion-server with MIT License 6 votes vote down vote up
def set(self, key, value, ttl):
        """
        Set the value for a key. Dark writes to the backup in-memory store are always performed
        to synchronize the state of the in-memory store with Redis, so that read failovers do not
        sacrifice the consistency of the underlying data.

        :param key: Raw key.
        :param value: Associated value.
        :param ttl: Time to live, in milliseconds.
        """
        try:
            return self.redis.set(key, value, px=ttl)
        except (ConnectionError, TimeoutError):
            pass
        finally:
            return self.memory.set(key, value, ttl) 
Example #12
Source File: rest_service.py    From scrapy-cluster with MIT License 6 votes vote down vote up
def _report_self(self):
        """
        Reports the crawler uuid to redis
        """
        if self.redis_connected:
            self.logger.debug("Reporting self to redis")
            try:
                key = "stats:rest:self:{m}:{u}".format(
                    m=socket.gethostname(),
                    u=self.my_uuid)
                self.redis_conn.set(key, self.get_time())
                self.redis_conn.expire(key, self.settings['HEARTBEAT_TIMEOUT'])
            except ConnectionError:
                self.logger.error("Lost connection to Redis")
                self._spawn_redis_connection_thread()
        else:
            self.logger.warn("Cannot report self to redis, not connected") 
Example #13
Source File: redlock.py    From pottery with Apache License 2.0 6 votes vote down vote up
def _acquire_masters(self):
        self._value = os.urandom(self.num_random_bytes)
        self._extension_num = 0
        futures, num_masters_acquired = set(), 0
        with ContextTimer() as timer, \
             concurrent.futures.ThreadPoolExecutor() as executor:
            for master in self.masters:
                futures.add(executor.submit(self._acquire_master, master))
            for future in concurrent.futures.as_completed(futures):
                with contextlib.suppress(TimeoutError, ConnectionError):
                    num_masters_acquired += future.result()
            quorum = num_masters_acquired >= len(self.masters) // 2 + 1
            elapsed = timer.elapsed() - self._drift()
            validity_time = self.auto_release_time - elapsed
        if quorum and max(validity_time, 0):
            return True
        else:
            with contextlib.suppress(ReleaseUnlockedLock):
                self.release()
            return False 
Example #14
Source File: test_rest_service.py    From scrapy-cluster with MIT License 6 votes vote down vote up
def test_report_self(self, h):
        # test not connected
        self.rest_service.redis_connected = False
        self.rest_service.logger.warn = MagicMock()
        self.rest_service._report_self()
        self.assertTrue(self.rest_service.logger.warn.called)

        # test connected
        self.rest_service.my_uuid = 'abc999'
        self.rest_service.get_time = MagicMock(return_value=5)
        self.rest_service.redis_connected = True
        self.rest_service.redis_conn = MagicMock()
        self.rest_service.redis_conn.set = MagicMock()
        self.rest_service._report_self()
        self.rest_service.redis_conn.set.assert_called_with('stats:rest:self:host:abc999',
                                                            5)
        # throw error
        self.rest_service._spawn_redis_connection_thread = MagicMock()
        self.rest_service.redis_conn.expire = MagicMock(side_effect=ConnectionError)
        self.rest_service._report_self()
        self.assertTrue(self.rest_service._spawn_redis_connection_thread.called) 
Example #15
Source File: test_rest_service.py    From scrapy-cluster with MIT License 6 votes vote down vote up
def test_send_result_to_redis(self):
        # test not connected
        self.rest_service.redis_connected = False
        self.rest_service.logger.warning = MagicMock()
        self.rest_service._send_result_to_redis('stuff')
        self.assertTrue(self.rest_service.logger.warning.called)

        # test connected
        self.rest_service.redis_connected = True
        self.rest_service.redis_conn = MagicMock()
        self.rest_service.redis_conn.set = MagicMock()
        self.rest_service._send_result_to_redis({'uuid': 'abc'})
        self.rest_service.redis_conn.set.assert_called_with('rest:poll:abc',
                                                            '{"uuid": "abc"}')

        # throw error
        self.rest_service._spawn_redis_connection_thread = MagicMock()
        self.rest_service.redis_conn.set = MagicMock(side_effect=ConnectionError)
        self.rest_service._send_result_to_redis({'uuid': 'abc'})
        self.assertTrue(self.rest_service._spawn_redis_connection_thread.called) 
Example #16
Source File: test_events.py    From CTFd with Apache License 2.0 6 votes vote down vote up
def test_redis_event_manager_installed():
    """Test that RedisEventManager is installed on the Flask app"""

    class RedisConfig(TestingConfig):
        REDIS_URL = "redis://localhost:6379/1"
        CACHE_REDIS_URL = "redis://localhost:6379/1"
        CACHE_TYPE = "redis"

    try:
        app = create_ctfd(config=RedisConfig)
    except ConnectionError:
        print("Failed to connect to redis. Skipping test.")
    else:
        with app.app_context():
            assert isinstance(app.events_manager, RedisEventManager)
        destroy_ctfd(app) 
Example #17
Source File: sentinel.py    From Flask with Apache License 2.0 5 votes vote down vote up
def read_response(self):
        try:
            return super(SentinelManagedConnection, self).read_response()
        except ReadOnlyError:
            if self.connection_pool.is_master:
                # When talking to a master, a ReadOnlyError when likely
                # indicates that the previous master that we're still connected
                # to has been demoted to a slave and there's a new master.
                # calling disconnect will force the connection to re-query
                # sentinel during the next connect() attempt.
                self.disconnect()
                raise ConnectionError('The previous master is now a slave')
            raise 
Example #18
Source File: test_events.py    From CTFd with Apache License 2.0 5 votes vote down vote up
def test_redis_event_manager_publish():
    """Test that RedisEventManager publishing to clients works."""

    class RedisConfig(TestingConfig):
        REDIS_URL = "redis://localhost:6379/3"
        CACHE_REDIS_URL = "redis://localhost:6379/3"
        CACHE_TYPE = "redis"

    try:
        app = create_ctfd(config=RedisConfig)
    except ConnectionError:
        print("Failed to connect to redis. Skipping test.")
    else:
        with app.app_context():
            saved_data = {
                "user_id": None,
                "title": "asdf",
                "content": "asdf",
                "team_id": None,
                "user": None,
                "team": None,
                "date": "2019-01-28T01:20:46.017649+00:00",
                "id": 10,
            }

            event_manager = RedisEventManager()
            event_manager.publish(data=saved_data, type="notification", channel="ctf")
        destroy_ctfd(app) 
Example #19
Source File: sentinel.py    From Flask with Apache License 2.0 5 votes vote down vote up
def connect(self):
        if self._sock:
            return  # already connected
        if self.connection_pool.is_master:
            self.connect_to(self.connection_pool.get_master_address())
        else:
            for slave in self.connection_pool.rotate_slaves():
                try:
                    return self.connect_to(slave)
                except ConnectionError:
                    continue
            raise SlaveNotFoundError  # Never be here 
Example #20
Source File: sentinel.py    From Flask with Apache License 2.0 5 votes vote down vote up
def connect_to(self, address):
        self.host, self.port = address
        super(SentinelManagedConnection, self).connect()
        if self.connection_pool.check_connection:
            self.send_command('PING')
            if nativestr(self.read_response()) != 'PONG':
                raise ConnectionError('PING failed') 
Example #21
Source File: sentinel.py    From Flask with Apache License 2.0 5 votes vote down vote up
def discover_slaves(self, service_name):
        "Returns a list of alive slaves for service ``service_name``"
        for sentinel in self.sentinels:
            try:
                slaves = sentinel.sentinel_slaves(service_name)
            except (ConnectionError, ResponseError):
                continue
            slaves = self.filter_slaves(slaves)
            if slaves:
                return slaves
        return [] 
Example #22
Source File: base.py    From eNMS with GNU General Public License v3.0 5 votes vote down vote up
def redis(self, operation, *args, **kwargs):
        try:
            return getattr(self.redis_queue, operation)(*args, **kwargs)
        except (ConnectionError, TimeoutError) as exc:
            self.log("error", f"Redis Queue Unreachable ({exc})", change_log=False) 
Example #23
Source File: RedisAdapter.py    From wrapcache with MIT License 5 votes vote down vote up
def test_memory_adapter(self):
			# test redis error
			self.assertRaises(DBNotSetException, self.test_class.get, 'test_key')
			
			REDIS_CACHE_POOL = redis.ConnectionPool(host = '162.211.225.208', port = 6739, password = '123456', db = 2)
			REDIS_CACHE_INST = redis.Redis(connection_pool = REDIS_CACHE_POOL, charset = 'utf8')
			RedisAdapter.db = REDIS_CACHE_INST #初始化装饰器缓存
			self.assertRaises(ConnectionError, self.test_class.get, 'test_key')
			
			REDIS_CACHE_POOL = redis.ConnectionPool(host = '162.211.225.209', port = 6739, password = 'wzwacxl', db = 2)
			REDIS_CACHE_INST = redis.Redis(connection_pool = REDIS_CACHE_POOL, charset = 'utf8')
			RedisAdapter.db = REDIS_CACHE_INST #初始化装饰器缓存
			
			key = 'test_key_1'
			value = str(time.time())
			
			#test set / get
			self.test_class.set(key, value)
			self.assertEqual(self.test_class.get(key).decode('utf-8'), value)
			
			#test remove
			self.test_class.set(key, value)
			self.test_class.remove(key)
			self.assertRaises(CacheExpiredException, self.test_class.get, key)
			
			#test flush
			self.test_class.set(key, value)
			self.test_class.flush()
			self.assertRaises(CacheExpiredException, self.test_class.get, key) 
Example #24
Source File: impl_redis.py    From taskflow with Apache License 2.0 5 votes vote down vote up
def _translate_failures():
    """Translates common redis exceptions into taskflow exceptions."""
    try:
        yield
    except redis_exceptions.ConnectionError:
        exc.raise_with_cause(exc.JobFailure, "Failed to connect to redis")
    except redis_exceptions.TimeoutError:
        exc.raise_with_cause(exc.JobFailure,
                             "Failed to communicate with redis, connection"
                             " timed out")
    except redis_exceptions.RedisError:
        exc.raise_with_cause(exc.JobFailure,
                             "Failed to communicate with redis,"
                             " internal error") 
Example #25
Source File: redis_utils.py    From taskflow with Apache License 2.0 5 votes vote down vote up
def _raise_on_closed(meth):

    @six.wraps(meth)
    def wrapper(self, *args, **kwargs):
        if self.closed:
            raise redis_exceptions.ConnectionError("Connection has been"
                                                   " closed")
        return meth(self, *args, **kwargs)

    return wrapper 
Example #26
Source File: enqueuer.py    From bii-server with MIT License 5 votes vote down vote up
def generic_enqueue(worker_path, parameters, timeout=60, priority="medium", async_process=True, connection=None):

    q = Enqueuer(worker_path, async_process=async_process,
                 timeout=timeout,  # Work timeout
                 priority=priority,
                 connection=connection)
    try:
        q.enqueue_job(*parameters)
    except ConnectionError as e:
        logger.error('REDIS FAIL: Redis seems to be down')
        logger.error(e)
    except Exception as e:
        logger.error("REDIS FAIL: ERROR ENQUEING"
                     " '%s' IN REDIS: %s" % (worker_path, str(e))) 
Example #27
Source File: enqueuer.py    From bii-server with MIT License 5 votes vote down vote up
def enqueue_job(self, *args):
        if not BII_LOG_TO_REDIS:
            logger.debug('Skipping logging due to config')
            return
        global POOL

        if not self.async_process:  # Call the method now!
            import importlib
            module_name = ".".join(self.worker.split(".")[0:-1])
            themodule = importlib.import_module(module_name)
            call_method = getattr(themodule, self.worker.split(".")[-1])
            call_method(*args)
        try:
            priority = Priority(self.priority)
            conn = self.connection or get_redis_connection()
            q = Queue(priority, connection=conn)
            return q.enqueue_call(self.worker, args=args,
                                  timeout=self.timeout, result_ttl=self.result_ttl)
        # NOTE: this rare way to call enqueue its needed, look at the code in queue module
        except ConnectionError as e:
            logger.warn("Error connecting redis, reconnecting...")
            raise e
        except Exception as e:
            logger.warn("Error enqueuing: %s" % str(e))
            tb = traceback.format_exc()
            logger.warn(tb)
            raise e 
Example #28
Source File: redis_db_driver.py    From dragonflow with Apache License 2.0 5 votes vote down vote up
def _key_command(self, command, key, *args):
        node = self._cluster.get_node(key)
        ask = False
        retry = 0
        command_pcs = [command, key]
        command_pcs.extend(args)
        while retry < self.RETRY_COUNT:
            LOG.debug('Executing command "%s" (retry %s)', command_pcs, retry)
            if node is None:
                LOG.error('Error finding node for key %s in cluster', key)
                self._cluster.populate_cluster()
            try:
                if ask:
                    node.client.execute_command('ASKING')
                    ask = False
                return node.client.execute_command(*command_pcs)
            except exceptions.ResponseError as e:
                (reason, slot, ip_port) = str(e).split(' ')
                (ip, port) = ip_port.split(':')
                if reason == 'MOVED':
                    self._cluster.populate_cluster()
                    node = self._cluster.get_node(key)
                if reason == 'ASK':
                    node = self._cluster.get_node_by_host(ip, port)
                    ask = True
            except exceptions.ConnectionError as e:
                LOG.exception('Connection to node %s:%s failed, refreshing',
                              node.ip, node.port)
                self._cluster.populate_cluster()
                node = self._cluster.get_node(key)
            retry += 1

        raise df_exceptions.DBKeyNotFound(key=key) 
Example #29
Source File: redis_db_driver.py    From dragonflow with Apache License 2.0 5 votes vote down vote up
def populate_cluster(self):
        for node in self._configured_nodes:
            client = node.client
            try:
                slots = client.execute_command('CLUSTER', 'SLOTS')
            except exceptions.ConnectionError:
                LOG.exception('Error connecting to cluster node %s:%s',
                              node.ip, node.port)
                continue
            except exceptions.ResponseError as e:
                if str(e).find('cluster support disabled') != -1:
                    LOG.info('Using a single non-cluster node %s:%s',
                             node.ip, node.port)
                    self._nodes_by_host = node
                    self._is_cluster = False
                    return
                LOG.exception('Response error from node %s:%s')
                continue
            self._is_cluster = True
            for slot_info in slots:
                (range_begin, range_end, master_info) = slot_info[0:3]
                master = Node(*master_info)
                self._nodes_by_host[master.key] = master
                for slot in range(int(range_begin), int(range_end) + 1):
                    self._nodes_by_slot[slot] = master
            if self.is_cluster_covered():
                self._covered = True
                break
        if not self._covered:
            LOG.error('Redis cluster not covering slot space')
        for node in self._nodes_by_host.values():
            LOG.info('Cluster node: %s:%s', node.ip, node.port) 
Example #30
Source File: client.py    From pydisque with MIT License 5 votes vote down vote up
def connect(self):
        """
        Connect to one of the Disque nodes.

        You can get current connection with connected_node property

        :returns: nothing
        """
        self.connected_node = None

        for i, node in self.nodes.items():
            host, port = i.split(':')
            port = int(port)
            redis_client = redis.Redis(host, port, **self.client_kw_args)

            try:
                ret = redis_client.execute_command('HELLO')
                format_version, node_id = ret[0], ret[1]
                others = ret[2:]
                self.nodes[i] = Node(node_id, host, port, redis_client)
                self.connected_node = self.nodes[i]
            except redis.exceptions.ConnectionError:
                pass

        if not self.connected_node:
            raise ConnectionError('couldnt connect to any nodes')
        logger.info("connected to node %s" % self.connected_node)