async def message_processing(servicebus_client, queue_name):
    while True:
        try:
            async with servicebus_client.get_queue_receiver(
                    queue_name, max_wait_time=1,
                    session_id=NEXT_AVAILABLE_SESSION) as receiver:
                renewer = AutoLockRenewer()
                renewer.register(receiver, receiver.session)
                await receiver.session.set_state("OPEN")
                async for message in receiver:
                    print("Message: {}".format(message))
                    print("Time to live: {}".format(message.time_to_live))
                    print("Sequence number: {}".format(
                        message.sequence_number))
                    print("Enqueue Sequence number: {}".format(
                        message.enqueued_sequence_number))
                    print("Partition Key: {}".format(message.partition_key))
                    print("Locked until: {}".format(message.locked_until_utc))
                    print("Lock Token: {}".format(message.lock_token))
                    print("Enqueued time: {}".format(
                        message.enqueued_time_utc))
                    await receiver.complete_message(message)
                    if str(message) == 'shutdown':
                        await receiver.session.set_state("CLOSED")
                        break
                await renewer.close()
        except OperationTimeoutError:
            print(
                "If timeout occurs during connecting to a session,"
                "It indicates that there might be no non-empty sessions remaining; exiting."
                "This may present as a UserError in the azure portal metric.")
            return
Esempio n. 2
0
async def message_processing(servicebus_client, queue_name):
    while True:
        try:
            async with servicebus_client.get_queue_session_receiver(
                    queue_name, max_wait_time=1) as receiver:
                renewer = AutoLockRenewer()
                renewer.register(receiver.session)
                await receiver.session.set_state("OPEN")
                async for message in receiver:
                    print("Message: {}".format(message))
                    print("Time to live: {}".format(message.time_to_live))
                    print("Sequence number: {}".format(
                        message.sequence_number))
                    print("Enqueue Sequence number: {}".format(
                        message.enqueued_sequence_number))
                    print("Partition Key: {}".format(message.partition_key))
                    print("Locked until: {}".format(message.locked_until_utc))
                    print("Lock Token: {}".format(message.lock_token))
                    print("Enqueued time: {}".format(
                        message.enqueued_time_utc))
                    await message.complete()
                    if str(message) == 'shutdown':
                        await receiver.session.set_state("CLOSED")
                        break
                await renewer.close()
        except NoActiveSession:
            print(
                "There are no non-empty sessions remaining; exiting.  This may present as a UserError in the azure portal."
            )
            return
async def renew_lock_on_session_of_the_sessionful_entity():
    servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR)

    async with servicebus_client:

        async with servicebus_client.get_queue_sender(queue_name=SESSION_QUEUE_NAME) as sender:
            msgs_to_send = [ServiceBusMessage("session message: {}".format(i), session_id='SESSION') for i in range(10)]
            await sender.send_messages(msgs_to_send)
            print('Send messages to sessionful queue.')

        renewer = AutoLockRenewer()

        async with servicebus_client.get_queue_receiver(
            queue_name=SESSION_QUEUE_NAME,
            session_id='SESSION',
            prefetch_count=10
        ) as receiver:
            # automatically renew the lock on the session for 100 seconds
            renewer.register(receiver.session, timeout=100)
            print('Register session into AutoLockRenewer.')

            received_msgs = await receiver.receive_messages(max_message_count=10, max_wait_time=5)
            await asyncio.sleep(100)  # message handling for long period (E.g. application logic)

            for msg in received_msgs:
                await msg.complete()
            print('Complete messages.')
async def renew_lock_on_message_received_from_non_sessionful_entity():
    servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR)

    async with servicebus_client:
        async with servicebus_client.get_queue_sender(queue_name=QUEUE_NAME) as sender:
            msgs_to_send = [ServiceBusMessage("session message: {}".format(i)) for i in range(10)]
            await sender.send_messages(msgs_to_send)
            print('Send messages to non-sessionful queue.')

        # Can also be called via "with AutoLockRenewer() as renewer" to automate shutdown.
        renewer = AutoLockRenewer()

        async with servicebus_client.get_queue_receiver(queue_name=QUEUE_NAME, prefetch_count=10) as receiver:
            received_msgs = await receiver.receive_messages(max_message_count=10, max_wait_time=5)

            for msg in received_msgs:
                # automatically renew the lock on each message for 100 seconds
                renewer.register(msg, timeout=100)
            print('Register messages into AutoLockRenewer done.')

            await asyncio.sleep(100)  # message handling for long period (E.g. application logic)

            for msg in received_msgs:
                await msg.complete()
            print('Complete messages.')

        await renewer.close()
Esempio n. 5
0
async def example_session_ops_async():
    servicebus_connection_str = os.environ['SERVICE_BUS_CONNECTION_STR']
    queue_name = os.environ['SERVICE_BUS_SESSION_QUEUE_NAME']
    session_id = "<your session id>"

    async with ServiceBusClient.from_connection_string(
            conn_str=servicebus_connection_str) as servicebus_client:

        async with servicebus_client.get_queue_sender(
                queue_name=queue_name) as sender:
            await sender.send_messages(
                ServiceBusMessage('msg', session_id=session_id))

        # [START get_session_async]
        async with servicebus_client.get_queue_receiver(
                queue_name=queue_name, session_id=session_id) as receiver:
            session = receiver.session
        # [END get_session_async]

        # [START get_session_state_async]
        async with servicebus_client.get_queue_receiver(
                queue_name=queue_name, session_id=session_id) as receiver:
            session = receiver.session
            session_state = await session.get_state()
        # [END get_session_state_async]

        # [START set_session_state_async]
        async with servicebus_client.get_queue_receiver(
                queue_name=queue_name, session_id=session_id) as receiver:
            session = receiver.session
            await session.set_state("START")
        # [END set_session_state_async]

        # [START session_renew_lock_async]
        async with servicebus_client.get_queue_receiver(
                queue_name=queue_name, session_id=session_id) as receiver:
            session = receiver.session
            await session.renew_lock()
        # [END session_renew_lock_async]

        # [START auto_lock_renew_session_async]
        from azure.servicebus.aio import AutoLockRenewer

        lock_renewal = AutoLockRenewer()
        async with servicebus_client.get_queue_receiver(
                queue_name=queue_name, session_id=session_id) as receiver:
            session = receiver.session
            # Auto renew session lock for 2 minutes
            lock_renewal.register(receiver,
                                  session,
                                  max_lock_renewal_duration=120)
            async for message in receiver:
                await process_message(message)
                await receiver.complete_message(message)
                # [END auto_lock_renew_session_async]
                break
async def renew_lock_with_lock_renewal_failure_callback():
    servicebus_client = ServiceBusClient.from_connection_string(
        conn_str=CONNECTION_STR)

    async with servicebus_client:
        async with servicebus_client.get_queue_sender(
                queue_name=QUEUE_NAME) as sender:
            await sender.send_messages(ServiceBusMessage("message"))

        async with AutoLockRenewer() as renewer:
            # For this sample we're going to set the renewal recurrence of the autolockrenewer to greater than the
            # service side message lock duration, to demonstrate failure.  Normally, this should not be adjusted.
            renewer._sleep_time = 40
            async with servicebus_client.get_queue_receiver(
                    queue_name=QUEUE_NAME, prefetch_count=10) as receiver:

                async def on_lock_renew_failure_callback(renewable, error):
                    # If auto-lock-renewal fails, this function will be called.
                    # If failure is due to an error, the second argument will be populated, otherwise
                    # it will default to `None`.
                    # This callback can be an ideal location to log the failure, or take action to safely
                    # handle any processing on the message or session that was in progress.
                    print("Intentionally failed to renew lock on {} due to {}".
                          format(renewable, error))

                received_msgs = await receiver.receive_messages(
                    max_message_count=1, max_wait_time=5)

                for msg in received_msgs:
                    # automatically renew the lock on each message for 120 seconds
                    renewer.register(
                        receiver,
                        msg,
                        max_lock_renewal_duration=90,
                        on_lock_renew_failure=on_lock_renew_failure_callback)
                print('Register messages into AutoLockRenewer done.')

                # Cause the messages and autorenewal to time out.
                # Other reasons for renew failure could include a network or service outage.
                await asyncio.sleep(80)

                try:
                    for msg in received_msgs:
                        await receiver.complete_message(msg)
                except ServiceBusError as e:
                    print(
                        'Messages cannot be settled if they have timed out. (This is expected)'
                    )

                print('Lock renew failure demonstration complete.')
Esempio n. 7
0
async def example_send_and_receive_async():
    servicebus_sender = await example_create_servicebus_sender_async()
    servicebus_receiver = await example_create_servicebus_receiver_async()

    from azure.servicebus import ServiceBusMessage
    # [START send_async]
    async with servicebus_sender:
        message = ServiceBusMessage("Hello World")
        await servicebus_sender.send_messages(message)
    # [END send_async]

    # [START create_batch_async]
    async with servicebus_sender:
        batch_message = await servicebus_sender.create_message_batch()
        batch_message.add_message(
            ServiceBusMessage("Single message inside batch"))
    # [END create_batch_async]

    # [START peek_messages_async]
    async with servicebus_receiver:
        messages = await servicebus_receiver.peek_messages()
        for message in messages:
            print(str(message))
    # [END peek_messages_async]

    # [START receive_async]
    async with servicebus_receiver:
        messages = await servicebus_receiver.receive_messages(max_wait_time=5)
        for message in messages:
            print(str(message))
            await message.complete()
    # [END receive_async]

    # [START receive_forever_async]
    async with servicebus_receiver:
        async for message in servicebus_receiver.get_streaming_message_iter():
            print(str(message))
            await message.complete()
    # [END receive_forever_async]

    # [START auto_lock_renew_message_async]
    from azure.servicebus.aio import AutoLockRenewer

    lock_renewal = AutoLockRenewer()
    async with servicebus_receiver:
        async for message in servicebus_receiver:
            lock_renewal.register(message, timeout=60)
            await process_message(message)
            await message.complete()
Esempio n. 8
0
async def receive_message(service_bus_client,
                          logger_adapter: logging.LoggerAdapter, config: dict):
    """
    This method is run per process. Each process will connect to service bus and try to establish a session.
    If messages are there, the process will continue to receive all the messages associated with that session.
    If no messages are there, the session connection will time out, sleep, and retry.
    """
    q_name = config["resource_request_queue"]

    while True:
        try:
            logger_adapter.info("Looking for new session...")
            # max_wait_time=1 -> don't hold the session open after processing of the message has finished
            async with service_bus_client.get_queue_receiver(
                queue_name=q_name,
                max_wait_time=1,
                session_id=NEXT_AVAILABLE_SESSION) as receiver:
                logger_adapter.info("Got a session containing messages")
                async with AutoLockRenewer() as renewer:
                    # allow a session to be auto lock renewed for up to an hour - if it's processing a message
                    renewer.register(receiver,
                                     receiver.session,
                                     max_lock_renewal_duration=3600)

                    async for msg in receiver:
                        result = True
                        message = ""

                        try:
                            message = json.loads(str(msg))
                            logger_adapter.info(
                                f"Message received for resource_id={message['id']}, operation_id={message['operationId']}, step_id={message['stepId']}"
                            )
                            message_logger_adapter = get_message_id_logger(
                                message['operationId']
                            )  # correlate messages per operation
                            result = await invoke_porter_action(
                                message, service_bus_client,
                                message_logger_adapter, config)
                        except (json.JSONDecodeError) as e:
                            logging.error(
                                f"Received bad service bus resource request message: {e}"
                            )

                        if result:
                            logging.info(
                                f"Resource request for {message} is complete")
                        else:
                            logging.error('Message processing failed!')

                        logger_adapter.info(
                            f"Message for resource_id={message['id']}, operation_id={message['operationId']} processed as {result} and marked complete."
                        )
                        await receiver.complete_message(msg)

                    logger_adapter.info("Closing session")
                    await renewer.close()

        except OperationTimeoutError:
            # Timeout occurred whilst connecting to a session - this is expected and indicates no non-empty sessions are available
            logger_adapter.info(
                "No sessions for this process. Will look again...")

        except ServiceBusConnectionError:
            # Occasionally there will be a transient / network-level error in connecting to SB.
            logger_adapter.info(
                "Unknown Service Bus connection error. Will retry...")

        except Exception:
            # Catch all other exceptions, log them via .exception to get the stack trace, sleep, and reconnect
            logger_adapter.exception("Unknown exception. Will retry...")
Esempio n. 9
0
async def example_send_and_receive_async():
    servicebus_sender = await example_create_servicebus_sender_async()
    servicebus_receiver = await example_create_servicebus_receiver_async()

    from azure.servicebus import ServiceBusMessage
    # [START send_async]
    async with servicebus_sender:
        message = ServiceBusMessage("Hello World")
        await servicebus_sender.send_messages(message)
        # [END send_async]
        await servicebus_sender.send_messages(
            [ServiceBusMessage("Hello World")] * 5)

    # [START create_batch_async]
    async with servicebus_sender:
        batch_message = await servicebus_sender.create_message_batch()
        batch_message.add_message(
            ServiceBusMessage("Single message inside batch"))
    # [END create_batch_async]

    # [START peek_messages_async]
    async with servicebus_receiver:
        messages = await servicebus_receiver.peek_messages()
        for message in messages:
            print(str(message))
    # [END peek_messages_async]

    # [START receive_async]
    async with servicebus_receiver:
        messages = await servicebus_receiver.receive_messages(max_wait_time=5)
        for message in messages:
            print(str(message))
            await servicebus_receiver.complete_message(message)
    # [END receive_async]

    # [START receive_forever_async]
    async with servicebus_receiver:
        async for message in servicebus_receiver.get_streaming_message_iter():
            print(str(message))
            await servicebus_receiver.complete_message(message)
            # [END receive_forever_async]
            break

        # [START abandon_message_async]
        messages = await servicebus_receiver.receive_messages(max_wait_time=5)
        for message in messages:
            await servicebus_receiver.abandon_message(message)
        # [END abandon_message_async]

        # [START complete_message_async]
        messages = await servicebus_receiver.receive_messages(max_wait_time=5)
        for message in messages:
            await servicebus_receiver.complete_message(message)
        # [END complete_message_async]

        # [START defer_message_async]
        messages = await servicebus_receiver.receive_messages(max_wait_time=5)
        for message in messages:
            await servicebus_receiver.defer_message(message)
        # [END defer_message_async]

        # [START dead_letter_message_async]
        messages = await servicebus_receiver.receive_messages(max_wait_time=5)
        for message in messages:
            await servicebus_receiver.dead_letter_message(message)
        # [END dead_letter_message_async]

        # [START renew_message_lock_async]
        messages = await servicebus_receiver.receive_messages(max_wait_time=5)
        for message in messages:
            await servicebus_receiver.renew_message_lock(message)
        # [END renew_message_lock_async]

    # [START auto_lock_renew_message_async]
    from azure.servicebus.aio import AutoLockRenewer

    lock_renewal = AutoLockRenewer()
    async with servicebus_receiver:
        async for message in servicebus_receiver:
            lock_renewal.register(servicebus_receiver,
                                  message,
                                  max_lock_renewal_duration=60)
            await process_message(message)
            await servicebus_receiver.complete_message(message)
            # [END auto_lock_renew_message_async]
            break
    await lock_renewal.close()