Exemplo n.º 1
0
    def connect(self):
        """
        'Connects' to the bus.

        :returns: The same instance.
        :rtype: commissaire_http.bus.Bus
        """
        if self.connection is not None:
            self.logger.warn('Bus already connected.')
            return self

        self.connection = Connection(self.connection_url)
        self._channel = self.connection.channel()
        self._exchange = Exchange(
            self.exchange_name, type='topic').bind(self._channel)
        self._exchange.declare()

        # Create queues
        self._queues = []
        for kwargs in self.qkwargs:
            queue = Queue(**kwargs)
            queue.exchange = self._exchange
            queue = queue.bind(self._channel)
            self._queues.append(queue)
            self.logger.debug('Created queue {}'.format(queue.as_dict()))

        # Create producer for publishing on topics
        self.producer = Producer(self._channel, self._exchange)
        self.logger.debug('Bus connection finished')
        return self
Exemplo n.º 2
0
    def __init__(self, exchange_name, connection_url, qkwargs):
        """
        Initializes a new Service instance.

        :param exchange_name: Name of the topic exchange.
        :type exchange_name: str
        :param connection_url: Kombu connection url.
        :type connection_url: str
        :param qkwargs: One or more dicts keyword arguments for queue creation
        :type qkwargs: list
        """
        name = self.__class__.__name__
        self.logger = logging.getLogger(name)
        self.logger.debug('Initializing {0}'.format(name))
        self.connection = Connection(connection_url)
        self._channel = self.connection.channel()
        self._exchange = Exchange(exchange_name,
                                  type='topic').bind(self._channel)
        self._exchange.declare()

        # Set up queues
        self._queues = []
        for kwargs in qkwargs:
            queue = Queue(**kwargs)
            queue.exchange = self._exchange
            queue = queue.bind(self._channel)
            self._queues.append(queue)
            self.logger.debug(queue.as_dict())

        # Create producer for publishing on topics
        self.producer = Producer(self._channel, self._exchange)
        self.logger.debug('Initializing of {} finished'.format(name))
Exemplo n.º 3
0
    def __init__(self,
                 exchange_name,
                 connection_url,
                 qkwargs,
                 config_file=None):
        """
        Initializes a new Service instance.

        :param exchange_name: Name of the topic exchange.
        :type exchange_name: str
        :param connection_url: Kombu connection url.
        :type connection_url: str
        :param qkwargs: One or more dicts keyword arguments for queue creation
        :type qkwargs: list
        :param config_file: Path to the configuration file location.
        :type config_file: str or None
        """
        name = self.__class__.__name__
        self.logger = logging.getLogger(name)
        self.logger.debug('Initializing {}'.format(name))

        # If we are given no default, use the global one
        # Read the configuration file
        self._config_data = read_config_file(config_file,
                                             self._default_config_file)

        if connection_url is None and 'bus_uri' in self._config_data:
            connection_url = self._config_data.get('bus_uri')
            self.logger.debug('Using connection_url=%s from config file',
                              connection_url)
        if exchange_name is None and 'exchange_name' in self._config_data:
            self.logger.debug('Using exchange_name=%s from config file',
                              exchange_name)
            exchange_name = self._config_data.get('bus_exchange')

        self.connection = Connection(connection_url)
        self._channel = self.connection.default_channel
        self._exchange = Exchange(exchange_name,
                                  type='topic').bind(self._channel)
        self._exchange.declare()

        # Set up queues
        self._queues = []
        for kwargs in qkwargs:
            queue = Queue(**kwargs)
            queue.exchange = self._exchange
            queue = queue.bind(self._channel)
            self._queues.append(queue)
            self.logger.debug(queue.as_dict())

        # Create producer for publishing on topics
        self.producer = Producer(self._channel, self._exchange)
        self.logger.debug('Initializing of {} finished'.format(name))
Exemplo n.º 4
0
 def test_queue_dump(self):
     b = binding(self.exchange, 'rk')
     q = Queue('foo', self.exchange, 'rk', bindings=[b])
     d = q.as_dict(recurse=True)
     assert d['bindings'][0]['routing_key'] == 'rk'
     registry.dumps(d)
Exemplo n.º 5
0
 def test_as_dict(self):
     q = Queue('foo', self.exchange, 'rk')
     d = q.as_dict(recurse=True)
     assert d['exchange']['name'] == self.exchange.name
Exemplo n.º 6
0
 def test_queue_dump(self):
     b = binding(self.exchange, 'rk')
     q = Queue('foo', self.exchange, 'rk', bindings=[b])
     d = q.as_dict(recurse=True)
     self.assertEqual(d['bindings'][0]['routing_key'], 'rk')
     registry.dumps(d)
Exemplo n.º 7
0
 def test_as_dict(self):
     q = Queue('foo', self.exchange, 'rk')
     d = q.as_dict(recurse=True)
     self.assertEqual(d['exchange']['name'], self.exchange.name)