def test_copy(self):
     c = Connection('amqp://example.com')
     assert copy(c).info() == c.info()
Example #2
0
    def test__green_pidbox_node(self):
        pool = Mock()
        pool.is_green = True
        l = MyKombuConsumer(self.buffer.put,
                            timer=self.timer,
                            pool=pool,
                            app=self.app)
        l.node = Mock()
        controller = find_step(l, consumer.Control)

        class BConsumer(Mock):
            def __enter__(self):
                self.consume()
                return self

            def __exit__(self, *exc_info):
                self.cancel()

        controller.box.node.listen = BConsumer()
        connections = []

        class Connection(object):
            calls = 0

            def __init__(self, obj):
                connections.append(self)
                self.obj = obj
                self.default_channel = self.channel()
                self.closed = False

            def __enter__(self):
                return self

            def __exit__(self, *exc_info):
                self.close()

            def channel(self):
                return Mock()

            def as_uri(self):
                return 'dummy://'

            def drain_events(self, **kwargs):
                if not self.calls:
                    self.calls += 1
                    raise socket.timeout()
                self.obj.connection = None
                controller.box._node_shutdown.set()

            def close(self):
                self.closed = True

        l.connection = Mock()
        l.connect = lambda: Connection(obj=l)
        controller = find_step(l, consumer.Control)
        controller.box.loop(l)

        self.assertTrue(controller.box.node.listen.called)
        self.assertTrue(controller.box.consumer)
        controller.box.consumer.consume.assert_called_with()

        self.assertIsNone(l.connection)
        self.assertTrue(connections[0].closed)
Example #3
0
            type='fanout',
            channel=self.channel,
            durable=True
        )
        queues = [
            Queue(
                name='',
                exchange=exchange,
                routing_key='',
                channel=self.channel,
                exclusive=True
            )
        ]
        return [
            Consumer(queues=queues, accept=['json'], callbacks=[self.on_message])
        ]

    def on_message(self, body, message):
        try:
            print(body)
        except Exception as e:
            print(str(e))
        message.ack()

if __name__ == '__main__':
    from kombu.utils.debug import setup_logging
    setup_logging(loglevel='DEBUG', loggers=[''])
    with Connection(AMQP_URL) as conn:
        consumer = FanoutConsumer(conn)
        consumer.run()
Example #4
0
 def setUp(self):
     if pyamqp is None:
         raise SkipTest('py-amqp not installed')
     self.connection = Connection('pyamqp://')
     self.transport = self.connection.transport
Example #5
0
    def callback(ch, method, properties, body):
        try:
            print(" [x] Received %r" % int(body))
            ch.basic_ack(delivery_tag=method.delivery_tag)
        except Exception as e:
            raise e

    channel.basic_consume(callback, queue='hello', no_ack=False)

    print(' [*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()

else:

    from kombu import Connection, Exchange, Queue

    media_exchange = Exchange('welog', 'topic', durable=True)
    video_queue = Queue('welog', exchange=media_exchange, routing_key='welog')

    def process_media(body, message):
        print body
        message.ack()

    # connections
    with Connection('amqp://*****:*****@localhost//') as conn:
        # consume
        with conn.Consumer(video_queue, callbacks=[process_media]) as consumer:
            # Process messages and handle events on all channels
            while True:
                conn.drain_events()
Example #6
0
    def test_default_port(self):
        class Transport(pyamqp.Transport):
            Connection = MockConnection

        c = Connection(port=None, transport=Transport).connect()
        assert c['host'] == f'127.0.0.1:{Transport.default_port}'
Example #7
0
def start_worker(broker_url):
    connection = Connection(broker_url)
    print(' [x] Awaiting RPC requests')
    worker = Worker(connection)
    worker.run()
 def test_as_uri_when_prefix(self):
     conn = Connection('redis+socket:///var/spool/x/y/z/redis.sock')
     assert conn.as_uri() == 'redis+socket:///var/spool/x/y/z/redis.sock'
 def test_as_uri_when_mongodb(self):
     x = Connection('mongodb://localhost')
     assert x.as_uri()
 def test_completes_cycle_no_cycle(self):
     c = Connection('amqp://')
     assert c.completes_cycle(0)
     assert c.completes_cycle(1)
 def test_completes_cycle(self):
     c = Connection('amqp://a;amqp://b;amqp://c')
     assert not c.completes_cycle(0)
     assert not c.completes_cycle(1)
     assert c.completes_cycle(2)
 def test_heartbeat_check(self):
     c = Connection(transport=Transport)
     c.transport.heartbeat_check = Mock()
     c.heartbeat_check(3)
     c.transport.heartbeat_check.assert_called_with(c.connection, rate=3)
 def test_maybe_switch_next_no_cycle(self):
     c = Connection('amqp://foo')
     c.maybe_switch_next()
     assert not c._closed
     assert c.hostname == 'foo'
     assert c.transport_cls, ('librabbitmq', 'pyamqp' in 'amqp')
 def test_copy_multiples(self):
     c = Connection('amqp://A.example.com;amqp://B.example.com')
     assert c.alt
     d = copy(c)
     assert d.alt == c.alt
 def test_without_pyrabbit(self):
     with self.assertRaises(ImportError):
         Connection('amqp://').get_manager()
 def setup(self):
     self.conn = Connection(port=5672, transport=Transport,
                            transport_options=self.transport_options)
Example #17
0
def test_amqps_connection():
    conn = Connection('amqps://')
    assert conn.transport  # evaluate transport, don't connect
    assert conn.ssl
 def test_bogus_scheme(self):
     with pytest.raises(KeyError):
         Connection('bogus://localhost:7421').transport
Example #19
0
 def setup(self):
     self.connection = Connection('pyamqp://')
     self.transport = self.connection.transport
 def test_prepare_not_callable(self):
     P = self.create_resource(None)
     conn = Connection('memory://')
     assert P.prepare(conn) is conn
Example #21
0
    def test_custom_port(self):
        class Transport(pyamqp.Transport):
            Connection = MockConnection

        c = Connection(port=1337, transport=Transport).connect()
        self.assertEqual(c['host'], '127.0.0.1:1337')
 def create_resource(self, limit):
     return Connection(port=5672, transport=Transport).ChannelPool(limit)
Example #23
0
import time
import random

from ddtrace import tracer
from kombu import Connection


@tracer.wrap('process_message')
def process_message(message):
    time.sleep(random.uniform(0.001, 0.01))


with Connection('amqp://*****:*****@127.0.0.1:5672//') as conn:
    simple_queue = conn.SimpleQueue('simple_queue')
    while True:
        with tracer.trace('consume', service='consumer'):
            message = simple_queue.get(block=True, timeout=2)
            message.ack()
            process_message(message)
            print('Received: {0}'.format(message.payload))

    simple_queue.close()
 def test_prepare_not_callable(self):
     P = self.create_resource(10)
     conn = Connection('memory://')
     chan = conn.default_channel
     assert P.prepare(chan) is chan
Example #25
0
#get config info:
parser = OptionParser()
parser.add_option("-c",
                  dest='configfile',
                  default=os.path.join(os.path.dirname(__file__),
                                       __file__).replace('.py', '.conf'),
                  help="configuration file to use")
(options, args) = parser.parse_args()
initConfig()

#connect and declare the message queue/kombu objects.
connString = 'amqp://{0}:{1}@{2}:{3}//'.format(options.mquser,
                                               options.mqpassword,
                                               options.mqserver,
                                               options.mqport)
mqConn = Connection(connString)

eventTaskExchange = Exchange(name=options.taskexchange,
                             type='direct',
                             durable=True)
eventTaskExchange(mqConn).declare()
eventTaskQueue = Queue(options.taskexchange, exchange=eventTaskExchange)
eventTaskQueue(mqConn).declare()
mqproducer = mqConn.Producer(serializer='json')

if __name__ == "__main__":
    run(host=options.listen_host, port=8080)
else:
    application = default_app()
Example #26
0
import dataset

print("import dataset heheheheh")
# https://dataset.readthedocs.io/en/latest/ setar para falar com banco
db = dataset.connect('mysql://*****:*****@192.168.0.108:49153/COUNTER_TBL')
tabela = db['EVENTOS']

console = Console()
queue = "contador-carro-exchange"
exchange = "contador-carro-exchange"
routing_key = "contador-carro-exchange"
rabbit_url = "amqp://*****:*****@192.168.0.108:5672//"

# Rabbit config
conn = Connection(rabbit_url)
channel_ = conn.channel()
exchange_ = Exchange(exchange, type="direct", delivery_mode=1)


class Worker(ConsumerMixin):
    def __init__(self, connection, queues):
        self.connection = connection
        self.queues = queues

    def on_message(self, body, message):
        data = pickle.loads(body)
        print(data['id_camera'])
        tabela.insert(
            dict(id_camera=data['id_camera'],
                 data_=data['data_'],
Example #27
0
 def run(self):
     with Connection(rpc.conn_str) as conn:
         self.consumer = RPCConsumer(conn, self.receiver)
         self.consumer.run()
Example #28
0
            denoise_list = glob.glob('./kaggle/*_*.jpg')
            total_list = glob.glob('./kaggle/*.jpg')
            raw_list = list(set(total_list) - set(denoise_list))
            run_duration = execute_user_script(raw_list)
            for i in xrange(1, 40):
                tmp = run_metrics('./kaggle/' + str(i) + '.jpg',
                                  './kaggle/denoise_' + str(i) + '.jpg')
                print i, tmp['score']
                val_ret['score'] += tmp['score']
            val_ret['duration'] = run_duration
            return

        except Exception as exc:
            logger.error('task raised exception: %r', exc)
        message.ack()


if __name__ == '__main__':
    from kombu import Connection
    from kombu.utils.debug import setup_logging
    # setup root logger
    setup_logging(loglevel='INFO', loggers=[''])

    with Connection(
            'amqp://*****:*****@37.187.117.106/echopen1') as conn:
        try:
            worker = Worker(conn)
            worker.run()
        except KeyboardInterrupt:
            print('bye bye')
Example #29
0
    def __call__(self, *args, **kwargs):
        _log.debug('invoking %s', self)

        worker_ctx = self.worker_ctx
        container = worker_ctx.container

        msg = {'args': args, 'kwargs': kwargs}

        conn = Connection(
            container.config[AMQP_URI_CONFIG_KEY],
            transport_options={'confirm_publish': True},
        )

        # We use the `mandatory` flag in `producer.publish` below to catch rpc
        # calls to non-existent services, which would otherwise wait forever
        # for a reply that will never arrive.
        #
        # However, the basic.return ("no one is listening for topic") is sent
        # asynchronously and conditionally, so we can't wait() on the channel
        # for it (will wait forever on successful delivery).
        #
        # Instead, we use (the rabbitmq extension) confirm_publish
        # (https://www.rabbitmq.com/confirms.html), which _always_ sends a
        # reply down the channel. Moreover, in the case where no queues are
        # bound to the exchange (service unknown), the basic.return is sent
        # first, so by the time kombu returns (after waiting for the confim)
        # we can reliably check for returned messages.

        routing_key = '{}.{}'.format(self.service_name, self.method_name)

        exchange = get_rpc_exchange(container)

        with producers[conn].acquire(block=True) as producer:

            headers = self.get_message_headers(worker_ctx)
            correlation_id = str(uuid.uuid4())

            reply_listener = self.reply_listener
            reply_to_routing_key = reply_listener.routing_key
            reply_event = reply_listener.get_reply_event(correlation_id)

            producer.publish(
                msg,
                exchange=exchange,
                routing_key=routing_key,
                mandatory=True,
                reply_to=reply_to_routing_key,
                headers=headers,
                correlation_id=correlation_id,
                retry=True,
                retry_policy=DEFAULT_RETRY_POLICY
            )

            if not producer.channel.returned_messages.empty():
                raise UnknownService(self.service_name)

        _log.debug('Waiting for RPC reply event %s', self)
        resp_body = reply_event.wait()
        _log.debug('RPC reply event complete %s %s', self, resp_body)

        error = resp_body.get('error')
        if error:
            raise deserialize(error)
        return resp_body['result']
 def test_manager(self):
     c = Connection(transport=Mock)
     assert c.manager is c.transport.manager