Exemple #1
0
async def main():
    conn = await aio_pika.connect_robust('amqp://*****:*****@localhost')
    channel = await conn.channel()

    # We need to declare the topic exchange
    exchange = await channel.declare_exchange('sample_topic',
                                              aio_pika.ExchangeType.TOPIC)

    # We set the consumer
    consumers = [
        asyncio.create_task(consume(channel, exchange, 'q1', 'healthcare.#')),
        asyncio.create_task(consume(channel, exchange, 'q2',
                                    'healthcare.*.en')),
        asyncio.create_task(consume(channel, exchange, 'q3', '#.es'))
    ]

    await asyncio.sleep(1)

    # Now we publish the messages
    print('publishing to key [blue]healthcare.some.en[/blue]')
    await exchange.publish(aio_pika.Message(body='hello world'.encode()),
                           routing_key='healthcare.some.en')

    print('publishing to key [blue]healthcare.any.es[/blue]')
    await exchange.publish(aio_pika.Message(body='hola mundo'.encode()),
                           routing_key='healthcare.any.es')

    await asyncio.wait(consumers)
Exemple #2
0
 async def _put(self, tasks):
     """Put a message into a list
     """
     for params, task in tasks:
         queue_name = params.get("queue")
         if not queue_name:
             continue
         if self._persistent:
             message = aio_pika.Message(
                 task.get_raw_data(),
                 delivery_mode=aio_pika.DeliveryMode.PERSISTENT)
         else:
             message = aio_pika.Message(task.get_raw_data())
         while True:
             try:
                 if queue_name not in self._queue_names:
                     await self._channel.declare_queue(queue_name,
                                                       durable=True)
                     self._queue_names.add(queue_name)
                 ret = await self._channel.default_exchange.publish(
                     message, routing_key=queue_name)
             except Exception as exc:
                 await asyncio.sleep(1)
             else:
                 break
Exemple #3
0
async def run_client(pubkey_a, pubkey_b, n_clients, n_fields, batch_id):
    connection = await aio_pika.connect_robust("amqp://*****:*****@rabbitmq:5672/")
    channel = await connection.channel()
    await channel.declare_queue("prio.0")
    await channel.declare_queue("prio.1")

    # delay for server setup
    await asyncio.sleep(3)

    pkA = prio.PublicKey().import_hex(pubkey_a)
    pkB = prio.PublicKey().import_hex(pubkey_b)

    config = prio.Config(n_fields, pkA, pkB, batch_id)
    client = prio.Client(config)

    data_items = bytes([(i % 3 == 1) or (i % 5 == 1) for i in range(n_fields)])

    for i in range(n_clients):

        logger.info("Client {}: Generated shares".format(i))
        for_server_a, for_server_b = client.encode(data_items)

        await channel.default_exchange.publish(
            aio_pika.Message(body=for_server_a, message_id=str(i), type="data"),
            routing_key="prio.0",
        )
        await channel.default_exchange.publish(
            aio_pika.Message(body=for_server_b, message_id=str(i), type="data"),
            routing_key="prio.1",
        )
    await connection.close()
    logger.info("Client done!")
Exemple #4
0
async def main():
    conn = await aio_pika.connect_robust('amqp://*****:*****@localhost')
    channel = await conn.channel()

    # We need to declare the direct exchange
    exchange = await channel.declare_exchange('sample_direct',
                                              aio_pika.ExchangeType.DIRECT)

    # We set the consumer
    consumers = [
        asyncio.create_task(consume(channel, exchange, 'q1', ['rk1'])),
        asyncio.create_task(consume(channel, exchange, 'q2', ['rk1', 'rk2'])),
        asyncio.create_task(consume(channel, exchange, 'q3', ['rk2']))
    ]

    await asyncio.sleep(1)

    # Now we publish the messages
    print('publishing to key [blue]rk1[/blue]')
    await exchange.publish(aio_pika.Message(body='hello rk1'.encode()),
                           routing_key='rk1')

    print('publishing to key [blue]rk2[/blue]')
    await exchange.publish(aio_pika.Message(body='This is for rk2'.encode()),
                           routing_key='rk2')

    await asyncio.wait(consumers)
Exemple #5
0
    async def write(self, data: dict):
        await self.connect()

        if self.exchange_mode:
            await self.conn.publish(
                aio_pika.Message(body=json.dumps(data).encode()),
                routing_key=self.routing_key)
        else:
            await self.conn.default_exchange.publish(
                aio_pika.Message(body=json.dumps(data).encode()),
                routing_key=self.routing_key)
Exemple #6
0
 async def _put(self, tasks):
     """Put a message into a list
     """
     for queue_name, task in tasks:
         queue_name = queue_name or self._queue_name_ls[0]
         if self._persistent:
             message = aio_pika.Message(task.get_raw_data(), delivery_mode=aio_pika.DeliveryMode.PERSISTENT)
         else:
             message = aio_pika.Message(task.get_raw_data())
         ret = await self._channel.default_exchange.publish(message, routing_key=queue_name)
         if ret:
             task.confirm()
Exemple #7
0
    async def write(self, feed: str, symbol: str, timestamp: float,
                    receipt_timestamp: float, data: dict):
        await self.connect()
        data['feed'] = feed
        data['symbol'] = symbol

        if self.exchange_mode:
            await self.conn.publish(
                aio_pika.Message(body=json.dumps(data).encode()),
                routing_key=self.routing_key)
        else:
            await self.conn.default_exchange.publish(
                aio_pika.Message(body=json.dumps(data).encode()),
                routing_key=self.routing_key)
async def main(loop):
    connection = await aio_pika.connect_robust(
        "amqp://*****:*****@127.0.0.1/", loop=loop
    )

    async with connection:
        routing_key = "test_queue"

        # Transactions conflicts with `publisher_confirms`
        channel = await connection.channel(publisher_confirms=False)

        # Use transactions with async context manager
        async with channel.transaction():
            # Publishing messages but delivery will not be done
            # before committing this transaction
            for i in range(10):
                message = aio_pika.Message(body="Hello #{}".format(i).encode())

                await channel.default_exchange.publish(
                    message, routing_key=routing_key
                )

        # Using transactions manually
        tx = channel.transaction()

        # start transaction manually
        await tx.select()

        await channel.default_exchange.publish(
            aio_pika.Message(body="Hello {}".format(routing_key).encode()),
            routing_key=routing_key,
        )

        await tx.commit()
        tx.close()

        # Using transactions manually
        tx = channel.transaction()

        # start transaction manually
        await tx.select()

        await channel.default_exchange.publish(
            aio_pika.Message(body="Should be rejected".encode()),
            routing_key=routing_key,
        )

        await tx.rollback()
        tx.close()
Exemple #9
0
    async def publish(
        self,
        exchange_name: str,
        routing: str,
        payload: Dict,
        delivery_mode: DeliveryMode = DeliveryMode.PERSISTENT,
    ) -> None:
        if not self._is_ready:
            self._logger.warning(
                "Not connected to RabbitMQ, unable to publish message."
            )
            return

        exchange = self._exchanges.get(exchange_name)
        if exchange is None:
            raise KeyError(f"Unknown exchange {exchange_name}.")

        message = aio_pika.Message(
            json.dumps(payload).encode(), delivery_mode=delivery_mode
        )

        async with self._channel.transaction():
            await exchange.publish(message, routing_key=routing)
            self._logger.log(
                TRACE, "Published message %s to %s/%s", payload, exchange_name, routing
            )
Exemple #10
0
async def t_bot_skip_video(msg: MessageModel):
    """
    Handle `/skip` bot command.

    Skip video to next one.
    """
    logger.info(f'User[{msg.from_.id}] send `skip` command')

    connection = config.MQ_CONNECTIONS.get('TBot')

    if connection is None:
        logger.warning(f'Connection for user[{msg.from_.id}] is not open.')
        return await bot.send_message('You are not logged in at the site.',
                                      msg.from_.id)

    channel: aio_pika.Channel = await connection.channel()

    websocket_exchange: aio_pika.Exchange = await channel.declare_exchange(
        'WebSocket',
        aio_pika.ExchangeType.DIRECT,
    )

    message_body = {
        'from': msg.from_.id,
        'command': config.WebSocketWorkerCommands.SKIP.value,
    }
    message_body_json_bytes = json.dumps(message_body).encode('utf-8')
    message = aio_pika.Message(message_body_json_bytes,
                               delivery_mode=aio_pika.DeliveryMode.PERSISTENT)

    await websocket_exchange.publish(message, routing_key='bot-worker')

    logger.info('Send skip command to the worker.')
Exemple #11
0
    async def send_event_payload_stream(
        self,
        event_payload_stream: MemoryObjectReceiveStream[EventPayload],
        confirmation_send_stream: MemoryObjectSendStream[EventPayload],
    ) -> None:
        connection: aio_pika.RobustConnection = await aio_pika.connect_robust(
            self.amqp_dsn)

        async with connection:
            channel: aio_pika.Channel = await connection.channel(
                publisher_confirms=True)
            exchange = await channel.declare_exchange(
                self.amqp_exchange, aio_pika.ExchangeType.FANOUT)
            async with confirmation_send_stream:
                async with event_payload_stream:
                    async for event_payload in event_payload_stream:
                        subject = event_payload.subject
                        await exchange.publish(
                            aio_pika.Message(
                                body=orjson.dumps(event_payload.body),
                                delivery_mode=aio_pika.DeliveryMode.PERSISTENT,
                            ),
                            routing_key=self.routing_key_from_subject(subject),
                        )
                        await confirmation_send_stream.send(event_payload)
Exemple #12
0
 async def write(self, feed: str, pair: str, timestamp: float, data: dict):
     await self.connect()
     data['feed'] = feed
     data['pair'] = pair
     await self.conn.default_exchange.publish(
         aio_pika.Message(body=json.dumps(data).encode()),
         routing_key='cryptofeed')
Exemple #13
0
async def provider(flag: int):
    loop = asyncio.get_event_loop()
    """
    _host = os.getenv("RABBITMQ_SERVER")
    _user = os.getenv("RABBITMQ_USER")
    _password = os.getenv("RABBITMQ_PASSWORD")
    """
    _host, _user, _password = "******", "guest", "guest"
    conn_str = "amqp://{user}:{password}@{host}".format(user=_user, host=_host, password=_password)
    conn = await aio_pika.connect_robust(conn_str, loop=loop)

    async with conn:
        routing_key = "test_queue"
        channel = await conn.channel()
        await channel.declare_queue(routing_key, durable=True)
        i = 0
        while True:
            i += 1
            try:
                await channel.default_exchange.publish(
                    aio_pika.Message(
                        body='{}: Hello {}'.format(flag, i).encode(),
                        delivery_mode=aio_pika.DeliveryMode.PERSISTENT
                    ),
                    routing_key=routing_key
                )
                await asyncio.sleep(2)
            except Exception as e:
                print(e)
            print(i)
Exemple #14
0
async def send_pt_rabbitmq_message(payload_type, command, message_body,
                                   username):
    try:
        connection = await aio_pika.connect(host="127.0.0.1",
                                            login="******",
                                            password="******",
                                            virtualhost="apfell_vhost")
        channel = await connection.channel()
        # declare our exchange
        exchange = await channel.declare_exchange('apfell_traffic',
                                                  aio_pika.ExchangeType.TOPIC)
        message = aio_pika.Message(
            message_body.encode(),
            delivery_mode=aio_pika.DeliveryMode.PERSISTENT)
        # Sending the message
        await exchange.publish(message,
                               routing_key="pt.task.{}.{}.{}".format(
                                   payload_type, command,
                                   base64.b64encode(
                                       username.encode()).decode('utf-8')))
        await connection.close()
        return {"status": "success"}
    except Exception as e:
        logger.exception(str(sys.exc_info()[-1].tb_lineno) + " " + str(e))
        # print(str(sys.exc_info()[-1].tb_lineno) + " " + str(e))
        return {
            "status": 'error',
            'error': "Failed to connect to rabbitmq, refresh"
        }
Exemple #15
0
async def alert_error_message(conn: aio_pika.Connection, message_id: int,
                              user: str, message: str):
    routing_key = "event.message.ko"
    channel = await conn.channel()
    msg = serialize_message(message_id, user, message)
    await channel.default_exchange.publish(aio_pika.Message(body=msg),
                                           routing_key=routing_key)
Exemple #16
0
async def main_loop():
    config_file = open("rabbitmq_config.json", 'rb')
    main_config = json.loads(config_file.read().decode('utf-8'))
    config_file.close()
    if main_config['name'] == "hostname":
        hostname = socket.gethostname()
    else:
        hostname = main_config['name']
    while True:
        try:
            connection = await aio_pika.connect_robust(host=main_config['host'],
                                                       login=main_config['username'],
                                                       password=main_config['password'],
                                                       virtualhost=main_config['virtual_host'])
            channel = await connection.channel()
            # declare our heartbeat exchange that everybody will publish to, but only the apfell server will are about
            exchange = await channel.declare_exchange('apfell_traffic', aio_pika.ExchangeType.TOPIC)
        except Exception as e:
            print(str(e))
            await asyncio.sleep( 2 )
            continue
        while True:
            try:
                # routing key is ignored for fanout, it'll go to anybody that's listening, which will only be the server
                await exchange.publish( aio_pika.Message("heartbeat".encode()), routing_key="pt.heartbeat.{}".format(hostname))
                await asyncio.sleep( 5 )
            except Exception as e:
                print(str(e))
                # if we get an exception here, break out to the bigger loop and try to connect again
                break
Exemple #17
0
async def heartbeat():
    global exchange
    global queue
    global channel
    global hostname
    try:
        if exchange is not None:
            while True:
                try:
                    queue = await channel.get_queue(hostname + "_tasking")
                    await exchange.publish(
                        aio_pika.Message("".encode()),
                        routing_key="pt.heartbeat.{}.{}.{}".format(
                            hostname, container_version,
                            str(queue.declaration_result.consumer_count)),
                    )
                    await asyncio.sleep(10)
                except Exception as e:
                    print_flush(
                        "[*] heartbeat - exception in heartbeat message loop:\n "
                        + str(traceback.format_exc()),
                        override=True)
                    sys.exit(1)
        else:
            print_flush(
                "[-] Failed to process heartbeat functionality, exiting container:\n "
                + str(traceback.format_exc()),
                override=True)
            sys.exit(1)
    except Exception as h:
        print_flush(
            "[-] Failed to process heartbeat functionality, exiting container:\n "
            + str(traceback.format_exc()),
            override=True)
        sys.exit(1)
Exemple #18
0
async def async_msg_local(loop):
    """Sends a message locally

    :param loop:
    :return:
    """
    # Perform connection
    connection_string = f'amqp://{mq_user}:{mq_password}@{mq_host}:{mq_port}//asm'
    async with await aio_pika.connect(connection_string,
                                      loop=loop) as connection:
        channel = await connection.channel()
        exchange = await channel.declare_exchange('amq.topic',
                                                  ExchangeType.TOPIC,
                                                  durable=True)

        # asm guild: 327585223112130570
        # officer lounge: 441342971842134016
        # leadership: 460905527145398292
        # test: 402570847363006475

        # test guild: 234755524145446912
        # channel: 350506185092366336
        msg = "I agree, \\@208289854038081567> truly is the best."
        payload_dict = {
            "guild": Guilds.TEST.value,
            "channel": Channels.TESTING_SERVER_CHANNEL.value,
            "body": msg
        }
        payload = json.dumps(payload_dict).encode('utf-8')
        await exchange.publish(
            aio_pika.Message(payload),
            routing_key='century',
        )

        print('[x] Sent: ' + str(payload_dict))
    async def __run(self, loop):
        generator = self.get_fibbonacci_generator()
        connector = None

        while True:
            try:
                connector = RabbitMQConnector(
                    username=self.__config.rabbitmq_username,
                    password=self.__config.rabbitmq_password,
                    host=self.__config.rabbitmq_host,
                    port=self.__config.rabbitmq_port,
                    vhost=self.__config.rabbitmq_vhost)
                await connector.connect(loop)
                message = str(next(generator))
                await connector.channel.default_exchange.publish(
                    message=aio_pika.Message(body=message.encode()),
                    routing_key=self.__config.rabbitmq_queue)
                self.logger.info(f'Message published: "{message}"')

                await connector.close()
                self.logger.info(
                    f'Sleeping for {self.__config.delay} seconds...')
                await asyncio.sleep(self.__config.delay)
            except Exception:
                raise
            finally:
                if connector and connector.connection:
                    await connector.close()
Exemple #20
0
 async def send_request(self, name, content):
     """
     Overriden from the base class
     """
     await self._lock.acquire()
     try:
         correlation_id = uuid.uuid4(
         ).hex  # probably not unique but with almost zero probability
         future = asyncio.Future()
         self._awaiting_requests[correlation_id] = future
         logger.info('Publishing for %s[%s]', name, correlation_id)
         await self._request_exchange.publish(
             aio_pika.Message(
                 correlation_id=correlation_id,
                 body=content,
                 reply_to=self._response_queue.name,
             ),
             routing_key='%s.%s' % (self.prefix, name),
         )
         logger.debug('Published for %s[%s]', name, correlation_id)
     finally:
         self._lock.release()
     ret = await future
     logger.debug('Got a result for %s[%s]', name, correlation_id)
     del self._awaiting_requests[correlation_id]
     return ret
Exemple #21
0
 async def _put(self, tasks):
     """Put a message into a list
     """
     for queue_name, task in tasks:
         queue_name = queue_name or self._queue_name_ls[0]
         if self._persistent:
             message = aio_pika.Message(task.get_raw_data(), delivery_mode=aio_pika.DeliveryMode.PERSISTENT)
         else:
             message = aio_pika.Message(task.get_raw_data())
         while True:
             try:
                 ret = await self._channel.default_exchange.publish(message, routing_key=queue_name)
             except Exception as exc:
                 await asyncio.sleep(1)
             else:
                 break
Exemple #22
0
async def test_client_receive(api_ws_cli, create_token, publisher):
    sess_id = random.choice(range(1, 6))
    routing_key = f"food.image.{sess_id}"
    resp = await api_ws_cli.ws_connect('/ws',
                                       headers={
                                           'upgrade':
                                           'websocket',
                                           'Sec-WebSocket-Extensions':
                                           'permessage-deflate',
                                           'Sec-WebSocket-Key':
                                           'yE4TmCX7xmZ815muKPbHbA==',
                                           'Connection':
                                           'keep-alive, Upgrade',
                                           'token':
                                           create_token(session_id=sess_id),
                                           'origin':
                                           origin.pop()
                                       },
                                       heartbeat=2)
    pb: aio_pika.Exchange = await publisher()
    body = "message"
    ms = aio_pika.Message(body.encode())
    await pb.publish(ms, routing_key=routing_key)
    res = await resp.receive()
    assert res.data == body
    await resp.close()
Exemple #23
0
async def sub(queue):
    connection = await aio_pika.connect_robust(
        "amqp://*****:*****@rabbitmq-container/")
    channel = await connection.channel()
    while True:
        line = await queue.get()
        queue.task_done()
        if line:
            message = {"body": line}
            await channel.default_exchange.publish(
                aio_pika.Message(
                    body=bytes(json.dumps(message), encoding="utf-8")),
                routing_key="stream_messages",
            )

        if "ALL TESTS DONE" in line:
            loop = asyncio.get_running_loop()
            pub_id = os.environ.get("CRAX_PUB_TEST_CORO")
            sub_id = os.environ.get("CRAX_SUB_TEST_CORO")
            run_id = os.environ.get("CRAX_RUN_TEST_CORO")
            tasks = [
                x for x in asyncio.Task.all_tasks(loop=loop)
                if id(x) in [int(pub_id),
                             int(sub_id),
                             int(run_id)]
            ]
            [x.cancel() for x in tasks]
Exemple #24
0
async def test_client_init(loop, rabbit_client, publisher):
    rk = "route_me"
    rc: RabbitClient = await rabbit_client("hello_queue",
                                           auto_delete=True,
                                           exclusive=True,
                                           routing_key=rk)
    pb: aio_pika.Exchange = await publisher()

    async def _get_res(rk):
        res = None
        while not res:
            res = await rc.get_result(rk)
            await asyncio.sleep(1)
        return res

    def put_mess(res: Queue, fut: asyncio.Future):
        res.put_nowait(fut.result())

    t = loop.create_task(_get_res(rk))
    res = Queue()
    t.add_done_callback(partial(put_mess, res))
    body = "message"
    ms = aio_pika.Message(body.encode())
    await pb.publish(ms, routing_key=rk)
    assert await res.get() == body
Exemple #25
0
    async def send_bytes(self,
                         message: bytes,
                         routing_key: str,
                         channel_number: int = None,
                         exchange_name: str = None,
                         reply: dict = None):
        channel = await self.get_channel(channel_number=channel_number)

        exchange = channel.default_exchange if exchange_name is None else await self.get_exchange(
            exchange_name)
        sent = message if len(message) <= 100 else "<Too large to display>"

        if reply is not None:
            reply_key = reply["routing_key"]
            reply_callback = reply["callback"]
            corr_id = self.correlation_id
            await self.create_listener(reply_key,
                                       reply_callback,
                                       auto_ack=True,
                                       channel_number=channel_number)
        else:
            reply_key = None
            corr_id = None

        disp(f"Sending message: \"{sent}\"", do_print=self.verbose)
        message = aio_pika.Message(body=message,
                                   reply_to=reply_key,
                                   correlation_id=corr_id)
        await exchange.publish(message, routing_key=routing_key)
Exemple #26
0
async def send_message(data):
    """Send message to queue_name"""
    conn = await aio_pika.connect(connection_str, loop=asyncio.get_event_loop())
    async with await conn.channel() as chn:
        message = aio_pika.Message(data.encode())
        await chn.default_exchange.publish(message, routing_key=ms_queue)
    await conn.close()
Exemple #27
0
async def main_loop():
    while True:
        try:
            connection = await aio_pika.connect_robust(
                host="127.0.0.1",
                login="******",
                password="******",
                virtualhost="apfell_vhost",
            )
            channel = await connection.channel()
            # declare our heartbeat exchange that everybody will publish to, but only the apfell server will are about
            exchange = await channel.declare_exchange(
                'apfell_traffic', aio_pika.ExchangeType.TOPIC)
        except Exception as e:
            print(str(e))
            await asyncio.sleep(2)
            continue
        while True:
            try:
                # routing key is ignored for fanout, it'll go to anybody that's listening, which will only be the server
                await exchange.publish(
                    aio_pika.Message("heartbeat".encode()),
                    routing_key="pt.heartbeat.{}".format(hostname))
                await asyncio.sleep(5)
            except Exception as e:
                print(str(e))
                # if we get an exception here, break out to the bigger loop and try to connect again
                break
Exemple #28
0
async def producer(rabbit_mq_path, queue):
    connection = await aio_pika.connect_robust(
        rabbit_mq_path  # amqp://guest:[email protected]/"
    )
    async with connection:

        channel = await connection.channel()
        message = yield 'starting'

        if message == 'finished':
            yield 'finished'

        while True:

            print(f"producer waiting for messages")
            message = yield
            if message == 'finished':
                break
            print(f"producer received message: {message}")
            result = await channel.default_exchange.publish(
                aio_pika.Message(body=json.dumps(message).encode()),
                routing_key=queue,
            )
            print(f"publish result: {result}")
    print(f"producer exiting")
Exemple #29
0
 async def return_message(incoming: Incoming, outgoing: Outgoing):
     outgoing_message = aio_pika.Message(
         body=incoming.message.body,
         content_type=incoming.message.content_type,
         delivery_mode=aio_pika.DeliveryMode.PERSISTENT,
     )
     outgoing.media = outgoing_message
    def generate_message(self) -> aio_pika.Message:
        """
        Serializes message body and returns message. Default messages are persistent.

        :return:
        """
        if isinstance(self.media, aio_pika.Message):
            return self.media

        convert_params_headers(self.headers)

        body_bytes = encode_content(
            content=self.media,
            mimetype=self.mimetype,
            data_schema=self.schema,
            headers=self.headers,
            encoders=self._encoders,
        )

        message = aio_pika.Message(
            body=body_bytes,
            delivery_mode=aio_pika.DeliveryMode.PERSISTENT,
            content_type=self.headers.get("Content-Type"),  # type: ignore
            headers=self.headers,
            **self.message_kwargs,
        )

        return message