Beispiel #1
0
def establish_connection(hostname=None,
                         userid=None,
                         password=None,
                         virtual_host=None,
                         port=None,
                         ssl=None,
                         insist=None,
                         connect_timeout=None,
                         backend_cls=None,
                         defaults=conf):
    """Establish a connection to the message broker."""
    if insist is None:
        insist = defaults.BROKER_INSIST
    if ssl is None:
        ssl = defaults.BROKER_USE_SSL
    if connect_timeout is None:
        connect_timeout = defaults.BROKER_CONNECTION_TIMEOUT

    return BrokerConnection(hostname or defaults.BROKER_HOST,
                            userid or defaults.BROKER_USER,
                            password or defaults.BROKER_PASSWORD,
                            virtual_host or defaults.BROKER_VHOST,
                            port or defaults.BROKER_PORT,
                            backend_cls=backend_cls or defaults.BROKER_BACKEND,
                            insist=insist,
                            ssl=ssl,
                            connect_timeout=connect_timeout)
Beispiel #2
0
    def __init__(self,
                 hostname='localhost',
                 port=5672,
                 userid="collectoruser",
                 password="******",
                 **options):

        conn = BrokerConnection(hostname=hostname,
                                port=port,
                                userid=userid,
                                password=password,
                                virtual_host='collectorvhost')

        try:
            self.publisher = Publisher(connection=conn,
                                       exchange="collector.response",
                                       exchange_type='direct',
                                       routing_key="response",
                                       serializer="json")
        except Exception as ex:
            raise Worker.ConnectionException(ex)

        self.consumer = Consumer(connection=conn,
                                 queue="feed",
                                 exchange_type='topic',
                                 exchange="collector",
                                 routing_key="collector.*.*")
Beispiel #3
0
 def __init__(self, hostname, port, vhost, userid, password):
     """connects to broker and provides convenience methods"""
     self.broker = BrokerConnection(hostname=hostname,
                                    port=port,
                                    userid=userid,
                                    password=password,
                                    virtual_host=vhost)
Beispiel #4
0
 def connect(self):
     if not self.connection:
         self.connection = BrokerConnection(hostname=self.config.host,
                                            port=self.config.port,
                                            userid=self.config.user,
                                            password=self.config.password,
                                            virtual_host=self.config.vhost)
Beispiel #5
0
def main():
    logging.basicConfig(level=logging.DEBUG)
    args = parser.parse_args()
    conn = BrokerConnection(
        hostname=args.hostname,
        virtual_host=args.vhost,
        userid=args.user,
        password=args.password,
    )
    publisher = Publisher(auto_declare=False,
                          connection=conn,
                          exchange=args.exchange,
                          routing_key=args.key)
    logging.info("Declaring exchange: %s" % args.exchange)
    publisher.backend.exchange_declare(exchange=args.exchange,
                                       type="topic",
                                       durable=False,
                                       auto_delete=False)
    while True:
        line = sys.stdin.readline()
        if not line:
            break
        logging.debug("Sending message '%s'" % line.strip())
        publisher.send(line.strip())
    publisher.close()
Beispiel #6
0
def cast(msg, event_type, topic, priority):
    yagi.config.setup(config_path='/etc/yagi.conf')
    conf = yagi.config.config_with('rabbit_broker')
    host = conf('host')
    port = conf('port')
    user = conf('user')
    exchange = conf('exchange')
    password = conf('password')
    vhost = conf('vhost')

    message_dict = {
        'message_id': str(uuid.uuid4()),
        'event_type': event_type,
        'publisher_id': 'some_publisher',
        'priority': priority,
        'timestamp': str(datetime.datetime.utcnow()),
        'payload': msg
        }
    conn = BrokerConnection(hostname=host, port=port, userid=user,
             password=password, virtual_host=vhost)
    publisher = Publisher(connection=conn, exchange=exchange,
            routing_key="%s.%s" % (topic, priority), durable=False,
            exchange_type='topic')
    publisher.send(message_dict)
    publisher.close()
Beispiel #7
0
 def __init__(self, host_name, port, userid, password, virtual_host,
              encoder_class):
     self.q_connection = BrokerConnection(hostname=host_name,
                                          port=port,
                                          userid=userid,
                                          password=password,
                                          virtual_host=virtual_host)
     self.encoder = encoder_class()
Beispiel #8
0
 def add(self):
     broker = BrokerConnection(hostname=self.hostname,
                               port=self.port,
                               userid=self.username,
                               password=self.password,
                               virtual_host=self.vhost)
     with self.lock:
         self._brokers.append(broker)
Beispiel #9
0
 def add_many(self, limit):
     with self.lock:
         for _ in range(0, limit):
             broker = BrokerConnection(hostname=self.hostname,
                                       port=self.port,
                                       userid=self.username,
                                       password=self.password,
                                       virtual_host=self.vhost)
             self._brokers.append(broker)
Beispiel #10
0
 def __init__(self, host_name, port, userid, password, virtual_host,
              encoder_class):
     self.q_connection = BrokerConnection(hostname=host_name,
                                          port=port,
                                          userid=userid,
                                          password=password,
                                          virtual_host=virtual_host)
     self.encoder = encoder_class()
     dispatcher.connect(self.spider_opened, signals.spider_opened)
     dispatcher.connect(self.spider_closed, signals.spider_closed)
    def test_with_statement(self):

        with BrokerConnection(**test_connection_args()) as conn:
            self.assertFalse(conn._closed)
            with Publisher(connection=conn, exchange="F", routing_key="G") \
                    as publisher:
                self.assertFalse(publisher._closed)
        self.assertTrue(conn._closed)
        self.assertTrue(publisher._closed)

        with BrokerConnection(**test_connection_args()) as conn:
            self.assertFalse(conn._closed)
            with Consumer(connection=conn,
                          queue="E",
                          exchange="F",
                          routing_key="G") as consumer:
                self.assertFalse(consumer._closed)
        self.assertTrue(conn._closed)
        self.assertTrue(consumer._closed)
Beispiel #12
0
    def test_messaging(self):
        m = TMessaging(connection=BrokerConnection(backend_cls=PyQueueBackend))
        self.assertTrue(m)

        self.assertEquals(m.fetch(), None)
        mdata = {"name": "Cosmo Kramer"}
        m.send(mdata)
        next_msg = m.fetch()
        next_msg_data = next_msg.decode()
        self.assertEquals(next_msg_data, mdata)
        self.assertEquals(m.fetch(), None)
    def __init__(self, hostname, port, user_id, password, virtual_host, encoder_class):
        self.queue_connection = BrokerConnection(
            hostname=hostname,
            port=port,
            userid=user_id,
            password=password,
            virtual_host=virtual_host
        )

        self.encoder = encoder_class()

        # Setup / Teardown Rabbit plumbing when spider opens / closes
        dispatcher.connect(self.spider_opened, signals.spider_opened)
        dispatcher.connect(self.spider_closed, signals.spider_closed)
Beispiel #14
0
    def establish_consumer_connection(self, consumer):
        # for now all consumers use the same queue connection
        # That may not be the case forever
        config = conf.config_with("rabbit_broker")
        reconnect_delay = int(config("reconnect_delay"))
        max_wait = int(config("max_wait"))

        # This just sets the connection string. It doesn't actually
        # connect to the AMQP server yet.
        connection = BrokerConnection(hostname=config("host"),
                                      port=config("port"),
                                      userid=config("user"),
                                      password=config("password"),
                                      virtual_host=config("vhost"),
                                      ssl=confbool("ssl"))

        auto_delete = consumer.config("auto_delete") == "True" or False
        durable = consumer.config("durable") == "True" or False

        exdurable = confbool(consumer.config("exchange_durable"))
        exauto_delete = confbool(consumer.config("exchange_auto_delete"))

        # try a few times to connect, we might have lost the connection
        retries = 0
        while True:
            try:
                carrot_consumer = NotQuiteSoStupidConsumer(
                    connection=connection,
                    warn_if_exists=True,
                    exchange=consumer.config("exchange"),
                    exchange_type=consumer.config("exchange_type"),
                    routing_key=consumer.config("routing_key"),
                    queue=consumer.queue_name,
                    auto_delete=auto_delete,
                    durable=durable,
                    exchange_durable=exdurable,
                    exchange_auto_delete=exauto_delete,
                )
                consumer.connect(connection, carrot_consumer)
                LOG.info("Connection established for %s" % consumer.queue_name)
                break
            except amqplib.client_0_8.exceptions.AMQPConnectionException, e:
                LOG.error("AMQP protocol error connecting to for queue %s" %
                          consumer.queue_name)
                LOG.exception(e)
            except socket.error, e:
                # lost connection?
                pass
Beispiel #15
0
def get_carrot_connection(config):
    backend = config.get('queue.library', 'pyamqplib')
    log.info("Carrot connnection using %s backend" % backend)
    try:
        port = int(config.get('queue.port', PORT))
    except ValueError:
        port = PORT
    userid = config.get('queue.user_id', USERID)
    password = config.get('queue.password', PASSWORD)
    hostname = config.get('queue.host', HOSTNAME)
    virtual_host = config.get('queue.virtual_host', VIRTUAL_HOST)
    
    backend_cls = 'carrot.backends.%s.Backend' % backend
    return BrokerConnection(hostname=hostname, port=port,
                            userid=userid, password=password,
                            virtual_host=virtual_host,
                            backend_cls=backend_cls)
Beispiel #16
0
    def test_consumer_interface(self):
        to_send = ['No', 'soup', 'for', 'you!']
        messages = []

        def cb(message_data, message):
            messages.append(message_data)

        conn = BrokerConnection(backend_cls='memory')
        consumer = Consumer(connection=conn,
                            queue="test",
                            exchange="test",
                            routing_key="test")
        consumer.register_callback(cb)
        publisher = Publisher(connection=conn,
                              exchange="test",
                              routing_key="test")
        for i in to_send:
            publisher.send(i)
        it = consumer.iterconsume()
        for i in range(len(to_send)):
            it.next()
        self.assertEqual(messages, to_send)
Beispiel #17
0
def main():
    logging.basicConfig(level=logging.INFO)
    args = parser.parse_args()
    conn = BrokerConnection(
        hostname=args.hostname,
        virtual_host=args.vhost,
        userid=args.user,
        password=args.password,
    )
    consumer = Consumer(
        auto_declare=False,
        connection=conn,
        exclusive=True,
        auto_delete=True,
    )
    try:
        logging.info("Creating exchange: %s" % args.exchange)
        consumer.backend.exchange_declare(exchange=args.exchange,
                                          type="topic",
                                          durable=False,
                                          auto_delete=True)
    except Exception, e:
        logging.warning("Failed to create exchange: %s" % e)
Beispiel #18
0
from carrot.connection import BrokerConnection

from config import BROKER_CONFIG, PRINTER_CONFIG

from pointofsale import printer

if __name__ == '__main__':
    conn = BrokerConnection(**BROKER_CONFIG)

    registry = printer.PrinterRegistry(PRINTER_CONFIG['registry'],
                                       PRINTER_CONFIG['auth_token'])
    p = printer.TestPrinter(PRINTER_CONFIG['name'], registry, conn)
    pm_handler = printer.PrinterMessageThread(p)

    pm_handler.start()
    while True:
        pm_handler.join(1)
        if not pm_handler.is_alive():
            break
from carrot.connection import BrokerConnection
from carrot.messaging import Consumer

if __name__ == '__main__':
    connection = BrokerConnection(hostname='localhost',
                                  port=5672,
                                  userid='guest',
                                  password='******',
                                  virtual_host=None)
    consumer = Consumer(connection=connection,
                        queue="transactions",
                        exchange="transactions")

    def print_message(message_data, message):
        print(message_data)
        message.ack()

    consumer.register_callback(print_message)
    consumer.wait()
Beispiel #20
0
def create_backend():
    return PyQueueBackend(connection=BrokerConnection())
Beispiel #21
0
def establish_test_connection():
    return BrokerConnection(**test_connection_args())
Beispiel #22
0
"""Message-classes."""
import os, subprocess
from os.path import join, split, splitext, exists
from carrot.connection import BrokerConnection

conn = BrokerConnection(hostname="localhost", port=5672,
                        userid="test", password="******",
                        virtual_host="test")

arrot.messaging import Consumer
consumer = Consumer(connection=conn, queue="feed",
                    exchange="feed", routing_key="importer")

def import_feed_callback(message_data, message):
    feed_url = message_data["import_feed"]
    print("Got feed import message for: %s" % feed_url)
    # something importing this feed url
    # import_feed(feed_url)
    message.ack()

consumer.register_callback(import_feed_callback)
consumer.wait() # Go into the consumer loop.
Beispiel #23
0
def create_connection():
    return BrokerConnection(backend_cls=StompBackend,
                            **test_stomp_connection_args())