Beispiel #1
0
 def _create_connection(self):
     "Tries to create a connection, returns True on success"
     self._connection = None
     self._last_confirmed_message = None
     host = self._get_next_host()
     self._logger.debug('Trying to connect to {}'.format(host))
     try:
         self._connection = RabbitConnection(
             host=host,
             port=self._connection_port,
             user=self._connection_user,
             password=self._connection_password,
             vhost=self._connection_path,
             close_cb=self._close_cb,
             heartbeat=self._connection_heartbeat,
             client_properties={
                 'connection_name': self._connection_name,
             },
         )
     except socket.error as exc:
         self._logger.error('Error connecting to rabbitmq {}'.format(exc))
         return False
     self._channel = self._connection.channel()
     if self._confirm:
         self._channel.confirm.select(nowait=False)
         self._channel.basic.set_ack_listener(self._ack)
     self._logger.debug('Connected to {}'.format(host))
     return True
Beispiel #2
0
 def __init__(self, host, port, vhost, user, password):
     """Create a new Server
     """
     self._conn = RabbitConnection(host = host, port = port, vhost = vhost, user = user, password = password)
     self._channel = self._conn.channel()
     self._channel.queue.declare('test_dead_channel', auto_delete = True)
     self._channel.queue.bind('test_dead_channel', exchange = 'amq.topic', routing_key = 'test.dead_channel')
     self._channel.basic.consume('test_dead_channel', self.callback, no_ack = False)
Beispiel #3
0
 def __init__(self, host, port, vhost, user, password):
     """Create a new Server
     """
     self._conn = RabbitConnection(transport = 'gevent', host = host, port = port, vhost = vhost, user = user, password = password, open_cb = self.onConnected)
     gevent.spawn(self.loop)
     self._channel = self._conn.channel()
     self._channel.basic.qos(prefetch_count = 10)
     self._channel.queue.declare('test_confirm', auto_delete = True)
     self._channel.basic.consume('test_confirm', self.callback, no_ack = False)
Beispiel #4
0
 def __init__(self, host, port, vhost, user, password):
     """Create a new Server
     """
     self._conn = RabbitConnection(host = host, port = port, vhost = vhost, user = user, password = password)
     self._channel = self._conn.channel()
     result = self._channel.queue.declare(arguments = { 'x-dead-letter-exchange': 'amq.topic', 'x-dead-letter-routing-key': 'test.dead_channel' })
     self._deadQueue = result[0]
     # Send a message
     self._channel.basic.publish(Message('OMG! I\'m dead!'), '', self._deadQueue)
Beispiel #5
0
 def __init__(self, host, port, vhost, user, password):
     """Create a new Server
     """
     self._conn = RabbitConnection(host=host,
                                   port=port,
                                   vhost=vhost,
                                   user=user,
                                   password=password)
     self._channel = self._conn.channel()
Beispiel #6
0
def plain_rabbit_connection_to_hosts(hosts, **kwargs):
    for host in hosts:
        logger.info('Trying to connect to host: {0}'.format(host))
        try:
            conn = RabbitConnection(host=host, **kwargs)
            logger.info("...success")
            return conn
        except socket.error:
            logger.info('Error connecting to {0}'.format(host))
    logger.error('Could not connect to any hosts')
Beispiel #7
0
 def __init__(self, host, port, vhost, user, password):
     """Create a new Server
     """
     self._conn = RabbitConnection(transport = 'gevent', host = host, port = port, vhost = vhost, user = user, password = password)
     gevent.spawn(self.loop)
     self._channel = self._conn.channel()
     self._channel.confirm.select()
     self._channel.basic.set_return_listener(self.onBasicReturn)
     self._channel.basic.set_ack_listener(self.onDeliverAck)
     self._channel.basic.set_nack_listener(self.onDeliverNAck)
Beispiel #8
0
 def __init__(self, host, port, vhost, user, password):
     """Create a new Server
     """
     self._conn = RabbitConnection(host=host,
                                   port=port,
                                   vhost=vhost,
                                   user=user,
                                   password=password)
     self._channel = self._conn.channel()
     self._channel.queue.declare('test_meta', auto_delete=True)
     self._channel.basic.consume('test_meta', self.callback, no_ack=False)
Beispiel #9
0
 def __init__(self, host, port, vhost, user, password):
     """Create a new Server
     """
     self._conn = RabbitConnection(transport='gevent',
                                   host=host,
                                   port=port,
                                   vhost=vhost,
                                   user=user,
                                   password=password)
     gevent.spawn(self.loop)
     self._channel = self._conn.channel()
Beispiel #10
0
 def __init__(self, host, port, vhost, user, password):
     """Create a new Server
     """
     self._conn = RabbitConnection(host=host,
                                   port=port,
                                   vhost=vhost,
                                   user=user,
                                   password=password)
     self._channel = self._conn.channel()
     result = self._channel.queue.declare(exclusive=True)
     self._callbackQueue = result[0]
     self._channel.basic.consume(self._callbackQueue,
                                 self.onResponse,
                                 no_ack=True)
     self._response = None
Beispiel #11
0
    def __init__(self, connectionParams=None, channelConfigCb=None):
        """
    NOTE: Connection establishment may be performed in the scope of the
    constructor or on demand, depending on the underlying implementation

    :param nta.utils.amqp.connection.ConnectionParams connectionParams:
      parameters for connecting to AMQP broker;
      [default=default params for RabbitMQ broker on localhost]
    :param channelConfigCb: An optional callback function that will be
      called whenever a new AMQP Channel is being brought up
    :type channelConfigCb: None or callable with the signature
      channelConfigCb(SynchronousAmqpClient)
    """
        self._channelConfigCb = channelConfigCb

        # Holds _ChannelContext when channel is created; we create the channel on
        # demand. The implementation accesses this member via the
        # `_liveChannelContext` property getter when it's desirable to bring up the
        # channel on-on demand. When it's undesirable to bring up the channel, the
        # implementation interacts directly with this member, which will be None
        # when we don't have a channel.
        self._channelContextInstance = None

        # Set to True when user calls close, so we know to not raise an exception
        # from our _on*Closed callback methods
        self._userInitiatedClosing = False

        # Instantiate underlying connection object
        params = (connectionParams if connectionParams is not None else
                  amqp_connection.ConnectionParams())

        # NOTE: we could get a `close_cb` call from RabbitConnection constructor, so
        # prepare for it by initializing `self._connection`
        self._connection = None
        self._connection = RabbitConnection(
            transport="socket",
            sock_opts={(socket.IPPROTO_TCP, socket.TCP_NODELAY): 1},
            synchronous=True,
            close_cb=self._onConnectionClosed,
            user=params.credentials.username,
            password=params.credentials.password,
            vhost=params.vhost,
            host=params.host,
            port=params.port,
            heartbeat=self._DEFAULT_HEARTBEAT_TIMEOUT_SEC,
            logger=g_log)
Beispiel #12
0
def get_connection(amqp_url):
    global connection
    if connection and not connection.closed:
        return connection

    parse = urlparse.urlparse(amqp_url)
    sock_opts = {
        (socket.IPPROTO_TCP, socket.TCP_NODELAY): 1
        }
    connection = RabbitConnection(logger=logger, debug=logging.WARN, user=parse.username, password=parse.password,
                                   vhost=parse.path, host=parse.hostname, hearbeat=None, port=parse.port,
                                   close_cb=connection_closed, sock_opts=sock_opts, transport="gevent")

    global connection_task
    connection_task = gevent.spawn(frame_loop)

    global connection_consumers
    if connection_consumers:
        for c in connection_consumers.itervalues():
            c._reconnect()
            c.start_consuming()
    return connection
Beispiel #13
0
    def _connect_to_broker(self):
        ''' Connect to broker and regisiter cleanup action to disconnect

        :returns: connection instance
        :rtype: `haigha.connections.rabbit_connection.Connection`
        '''
        sock_opts = {
            (socket.IPPROTO_TCP, socket.TCP_NODELAY): 1,
        }
        connection = RabbitConnection(logger=_LOG,
                                      debug=_OPTIONS.debug,
                                      user=_OPTIONS.user,
                                      password=_OPTIONS.password,
                                      vhost=_OPTIONS.vhost,
                                      host=_OPTIONS.host,
                                      heartbeat=None,
                                      sock_opts=sock_opts,
                                      transport='socket')
        self.addCleanup(lambda: connection.close(disconnect=True)
                        if not connection.closed else None)

        return connection
Beispiel #14
0
 def __init__(self, host, port, vhost, user, password):
     """Create a new EventSender
     """
     self._conn = RabbitConnection(host = host, port = port, vhost = vhost, user = user, password = password)
     self._channel = self._conn.channel()
     self._channel.queue.declare('_aprefix/%s/webservice/%s' % (socket.gethostname(), os.getpid()), exclusive = True)
Beispiel #15
0
def run(args):
    host = os.getenv('AMQP_HOST', 'localhost')
    user = os.getenv('AMQP_USER', 'guest')
    password = os.getenv('AMQP_PASS', 'guest')
    vhost = os.getenv('AMQP_VHOST', '/')

    connection = RabbitConnection(
      user=user, password=password,
      vhost=vhost, host=host,
      heartbeat=None, debug=True)

    config = get_config(args.config)

    ch = connection.channel()
    for exchange in config.get('exchanges'):
        print 'Declaring exchange:'
        print '\t', exchange
        try:
            ch.exchange.declare(
                exchange['name'],
                exchange['type'],
                durable=exchange['durable'],
                auto_delete=exchange['auto_delete'],
                arguments=exchange.get('arguments', {}),
            )
        except AttributeError as ae:
            print ae
            print 'Declare conflict! This must be fixed manually'
            sys.exit(1)

    for queue in config.get('queues'):
        print 'Declaring queue:'
        print '\t', queue
        try:
            ch.queue.declare(
                queue['name'],
                auto_delete=queue['auto_delete'],
                durable=queue['durable'],
                arguments=queue.get('arguments', {}),
            )
        except AttributeError as ae:
            print ae
            print 'Declare conflict! This must be fixed manually'
            sys.exit(1)
        for binding in queue['bindings']:
            print 'Binding queue:'
            print '\t', binding
            try:

                if binding.get('binding_key'):
                    ch.queue.bind(
                        queue['name'],
                        binding['exchange'],
                        binding['binding_key'],
                    )
                else:
                    ch.queue.bind(
                        queue['name'],
                        binding['exchange']
                    )
            except AttributeError:
                print 'Declare conflict! This must be fixed manually'
                sys.exit(1)
    connection.close()
Beispiel #16
0
import json
import os
import time

from haigha.connections.rabbit_connection import RabbitConnection
from haigha.message import Message

connection = RabbitConnection(user=os.getenv('RABBITMQ_USER'),
                              password=os.getenv('RABBITMQ_PASS'),
                              vhost='/',
                              host=os.getenv('RABBITMQ_HOSTS',
                                             'localhost').split(',')[0],
                              heartbeat=None,
                              debug=True)

ch = connection.channel()
ch.exchange.declare('cronq', 'direct')
ch.queue.declare('cronq_jobs', auto_delete=False)
ch.queue.declare('cronq_results', auto_delete=False)
ch.queue.bind('cronq_jobs', 'cronq', 'cronq_jobs')
ch.queue.bind('cronq_results', 'cronq', 'cronq_results')

while True:
    print 'publish'
    cmd = {
        "cmd": "sleep 1",
        "job_id": 1024,
        "name": "[TEST] A test job",
        "run_id": "1234"
    }
    ch.basic.publish(Message(json.dumps(cmd),