async def open_async(self):
        """
        Open the Receiver using the supplied conneciton.
        If the handler has previously been redirected, the redirect
        context will be used to create a new handler before opening it.

        :param connection: The underlying client shared connection.
        :type: connection: ~uamqp.async_ops.connection_async.ConnectionAsync
        """
        # pylint: disable=protected-access
        if self.redirected:
            self.source = self.redirected.address
            source = Source(self.source)
            if self.offset is not None:
                source.set_filter(self.offset.selector())
            alt_creds = {
                "username": self.client._auth_config.get("iot_username"),
                "password": self.client._auth_config.get("iot_password")
            }
            self._handler = ReceiveClientAsync(
                source,
                auth=self.client.get_auth(**alt_creds),
                debug=self.client.debug,
                prefetch=self.prefetch,
                link_properties=self.properties,
                timeout=self.timeout,
                error_policy=self.retry_policy,
                keep_alive_interval=self.keep_alive,
                client_name=self.name,
                properties=self.client.create_properties(),
                loop=self.loop)
        await self._handler.open_async()
        while not await self.has_started():
            await self._handler._connection.work_async()
Exemple #2
0
    def _create_handler(self, auth: "JWTTokenAsync") -> None:
        source = Source(self._source)
        if self._offset is not None:
            source.set_filter(
                event_position_selector(self._offset, self._offset_inclusive))
        desired_capabilities = None
        if self._track_last_enqueued_event_properties:
            symbol_array = [types.AMQPSymbol(RECEIVER_RUNTIME_METRIC_SYMBOL)]
            desired_capabilities = utils.data_factory(
                types.AMQPArray(symbol_array))

        properties = create_properties(self._client._config.user_agent  # pylint:disable=protected-access
                                       )
        self._handler = ReceiveClientAsync(
            source,
            auth=auth,
            debug=self._client._config.network_tracing,  # pylint:disable=protected-access
            prefetch=self._prefetch,
            link_properties=self._link_properties,
            timeout=self._timeout,
            idle_timeout=self._idle_timeout,
            error_policy=self._retry_policy,
            keep_alive_interval=self._keep_alive,
            client_name=self._name,
            receive_settle_mode=uamqp.constants.ReceiverSettleMode.
            ReceiveAndDelete,
            auto_complete=False,
            properties=properties,
            desired_capabilities=desired_capabilities,
            loop=self._loop,
        )

        self._handler._streaming_receive = True  # pylint:disable=protected-access
        self._handler._message_received_callback = (  # pylint:disable=protected-access
            self._message_received)
Exemple #3
0
    def __init__(  # pylint: disable=super-init-not-called
            self,
            client,
            source,
            offset=None,
            prefetch=300,
            epoch=None,
            keep_alive=None,
            auto_reconnect=True,
            loop=None):
        """
        Instantiate an async receiver.

        :param client: The parent EventHubClientAsync.
        :type client: ~azure.eventhub.async_ops.EventHubClientAsync
        :param source: The source EventHub from which to receive events.
        :type source: ~uamqp.address.Source
        :param prefetch: The number of events to prefetch from the service
         for processing. Default is 300.
        :type prefetch: int
        :param epoch: An optional epoch value.
        :type epoch: int
        :param loop: An event loop.
        """
        self.loop = loop or asyncio.get_event_loop()
        self.running = False
        self.client = client
        self.source = source
        self.offset = offset
        self.prefetch = prefetch
        self.epoch = epoch
        self.keep_alive = keep_alive
        self.auto_reconnect = auto_reconnect
        self.retry_policy = errors.ErrorPolicy(max_retries=3,
                                               on_error=_error_handler)
        self.reconnect_backoff = 1
        self.redirected = None
        self.error = None
        self.properties = None
        partition = self.source.split('/')[-1]
        self.name = "EHReceiver-{}-partition{}".format(uuid.uuid4(), partition)
        source = Source(self.source)
        if self.offset is not None:
            source.set_filter(self.offset.selector())
        if epoch:
            self.properties = {
                types.AMQPSymbol(self._epoch): types.AMQPLong(int(epoch))
            }
        self._handler = ReceiveClientAsync(
            source,
            auth=self.client.get_auth(),
            debug=self.client.debug,
            prefetch=self.prefetch,
            link_properties=self.properties,
            timeout=self.timeout,
            error_policy=self.retry_policy,
            keep_alive_interval=self.keep_alive,
            client_name=self.name,
            properties=self.client.create_properties(),
            loop=self.loop)
Exemple #4
0
 def __init__(self, client, source, prefetch=300, epoch=None, loop=None):  # pylint: disable=super-init-not-called
     """
     Instantiate an async receiver.
     :param client: The parent EventHubClient.
     :type client: ~azure.eventhub.EventHubClient
     :param source: The source EventHub from which to receive events.
     :type source: ~uamqp.Source
     :param prefetch: The number of events to prefetch from the service
      for processing. Default is 300.
     :type prefetch: int
     :param epoch: An optional epoch value.
     :type epoch: int
     :param loop: An event loop.
     """
     self.loop = loop or asyncio.get_event_loop()
     self.offset = None
     self._callback = None
     self.prefetch = prefetch
     self.epoch = epoch
     properties = None
     if epoch:
         properties = {
             types.AMQPSymbol(self._epoch): types.AMQPLong(int(epoch))
         }
     self._handler = ReceiveClientAsync(source,
                                        auth=client.auth,
                                        debug=client.debug,
                                        prefetch=self.prefetch,
                                        link_properties=properties,
                                        timeout=self.timeout,
                                        loop=self.loop)
Exemple #5
0
 async def reconnect_async(self):
     """If the Receiver was disconnected from the service with
     a retryable error - attempt to reconnect."""
     # pylint: disable=protected-access
     alt_creds = {
         "username": self.client._auth_config.get("iot_username"),
         "password": self.client._auth_config.get("iot_password")
     }
     await self._handler.close_async()
     source = Source(self.source)
     if self.offset is not None:
         source.set_filter(self.offset.selector())
     self._handler = ReceiveClientAsync(
         source,
         auth=self.client.get_auth(**alt_creds),
         debug=self.client.debug,
         prefetch=self.prefetch,
         link_properties=self.properties,
         timeout=self.timeout,
         error_policy=self.retry_policy,
         keep_alive_interval=self.keep_alive,
         client_name=self.name,
         properties=self.client.create_properties(),
         loop=self.loop)
     await self._handler.open_async()
     while not await self.has_started():
         await self._handler._connection.work_async()
 async def _reconnect_async(self):  # pylint: disable=too-many-statements
     # pylint: disable=protected-access
     alt_creds = {
         "username": self.client._auth_config.get("iot_username"),
         "password":self.client._auth_config.get("iot_password")}
     await self._handler.close_async()
     source = Source(self.source)
     if self.offset is not None:
         source.set_filter(self.offset.selector())
     self._handler = ReceiveClientAsync(
         source,
         auth=self.client.get_auth(**alt_creds),
         debug=self.client.debug,
         prefetch=self.prefetch,
         link_properties=self.properties,
         timeout=self.timeout,
         error_policy=self.retry_policy,
         keep_alive_interval=self.keep_alive,
         client_name=self.name,
         properties=self.client.create_properties(),
         loop=self.loop)
     try:
         await self._handler.open_async()
         while not await self._handler.client_ready_async():
             await asyncio.sleep(0.05)
         return True
     except errors.TokenExpired as shutdown:
         log.info("AsyncReceiver disconnected due to token expiry. Shutting down.")
         error = EventHubError(str(shutdown), shutdown)
         await self.close_async(exception=error)
         raise error
     except (errors.LinkDetach, errors.ConnectionClose) as shutdown:
         if shutdown.action.retry and self.auto_reconnect:
             log.info("AsyncReceiver detached. Attempting reconnect.")
             return False
         log.info("AsyncReceiver detached. Shutting down.")
         error = EventHubError(str(shutdown), shutdown)
         await self.close_async(exception=error)
         raise error
     except errors.MessageHandlerError as shutdown:
         if self.auto_reconnect:
             log.info("AsyncReceiver detached. Attempting reconnect.")
             return False
         log.info("AsyncReceiver detached. Shutting down.")
         error = EventHubError(str(shutdown), shutdown)
         await self.close_async(exception=error)
         raise error
     except errors.AMQPConnectionError as shutdown:
         if str(shutdown).startswith("Unable to open authentication session") and self.auto_reconnect:
             log.info("AsyncReceiver couldn't authenticate. Attempting reconnect.")
             return False
         log.info("AsyncReceiver connection error (%r). Shutting down.", shutdown)
         error = EventHubError(str(shutdown))
         await self.close_async(exception=error)
         raise error
     except Exception as e:
         log.info("Unexpected error occurred (%r). Shutting down.", e)
         error = EventHubError("Receiver reconnect failed: {}".format(e))
         await self.close_async(exception=error)
         raise error
    def _create_handler(self):
        source = Source(self._source)
        if self._offset is not None:
            source.set_filter(self._offset._selector())  # pylint:disable=protected-access

        if StrictVersion(uamqp.__version__) < StrictVersion("1.2.3"):  # backward compatible until uamqp 1.2.3 released
            desired_capabilities = {}
        elif self._track_last_enqueued_event_properties:
            symbol_array = [types.AMQPSymbol(self._receiver_runtime_metric_symbol)]
            desired_capabilities = {"desired_capabilities": utils.data_factory(types.AMQPArray(symbol_array))}
        else:
            desired_capabilities = {"desired_capabilities": None}

        self._handler = ReceiveClientAsync(
            source,
            auth=self._client._create_auth(),  # pylint:disable=protected-access
            debug=self._client._config.network_tracing,  # pylint:disable=protected-access
            prefetch=self._prefetch,
            link_properties=self._link_properties,
            timeout=self._timeout,
            error_policy=self._retry_policy,
            keep_alive_interval=self._keep_alive,
            client_name=self._name,
            receive_settle_mode=uamqp.constants.ReceiverSettleMode.ReceiveAndDelete,
            auto_complete=False,
            properties=self._client._create_properties(  # pylint:disable=protected-access
                self._client._config.user_agent),  # pylint:disable=protected-access
            **desired_capabilities,  # pylint:disable=protected-access
            loop=self._loop)
        self._messages_iter = None
Exemple #8
0
    def _create_handler(self):
        alt_creds = {
            "username":
            self._client._auth_config.get("iot_username")
            if self._redirected else None,  # pylint:disable=protected-access
            "password":
            self._client._auth_config.get("iot_password")
            if self._redirected else None  # pylint:disable=protected-access
        }

        source = Source(self._source)
        if self._offset is not None:
            source.set_filter(self._offset._selector())  # pylint:disable=protected-access
        self._handler = ReceiveClientAsync(
            source,
            auth=self._client._get_auth(**alt_creds),  # pylint:disable=protected-access
            debug=self._client._config.network_tracing,  # pylint:disable=protected-access
            prefetch=self._prefetch,
            link_properties=self._link_properties,
            timeout=self._timeout,
            error_policy=self._retry_policy,
            keep_alive_interval=self._keep_alive,
            client_name=self._name,
            properties=self._client._create_properties(  # pylint:disable=protected-access
                self._client._config.user_agent),  # pylint:disable=protected-access
            loop=self._loop)
        self._messages_iter = None
 def _build_handler(self):
     auth = None if self.connection else authentication.SASTokenAsync.from_shared_access_key(
         **self.auth_config)
     self._handler = ReceiveClientAsync(self.endpoint,
                                        auth=auth,
                                        debug=self.debug,
                                        properties=self.properties,
                                        error_policy=self.error_policy,
                                        client_name=self.name,
                                        auto_complete=False,
                                        encoding=self.encoding,
                                        loop=self.loop,
                                        **self.handler_kwargs)
 async def reconnect_async(self):
     """If the Receiver was disconnected from the service with
     a retryable error - attempt to reconnect."""
     # pylint: disable=protected-access
     alt_creds = {
         "username": self.client._auth_config.get("iot_username"),
         "password": self.client._auth_config.get("iot_password")
     }
     await self._handler.close_async()
     source = Source(self.source)
     if self.offset is not None:
         source.set_filter(self.offset.selector())
     self._handler = ReceiveClientAsync(
         source,
         auth=self.client.get_auth(**alt_creds),
         debug=self.client.debug,
         prefetch=self.prefetch,
         link_properties=self.properties,
         timeout=self.timeout,
         error_policy=self.retry_policy,
         keep_alive_interval=self.keep_alive,
         client_name=self.name,
         properties=self.client.create_properties(),
         loop=self.loop)
     try:
         await self._handler.open_async()
         while not await self.has_started():
             await self._handler._connection.work_async()
     except (errors.LinkDetach, errors.ConnectionClose) as shutdown:
         if shutdown.action.retry and self.auto_reconnect:
             log.info("AsyncReceiver detached. Attempting reconnect.")
             await self.reconnect_async()
         else:
             log.info("AsyncReceiver detached. Shutting down.")
             error = EventHubError(str(shutdown), shutdown)
             await self.close_async(exception=error)
             raise error
     except errors.MessageHandlerError as shutdown:
         if self.auto_reconnect:
             log.info("AsyncReceiver detached. Attempting reconnect.")
             await self.reconnect_async()
         else:
             log.info("AsyncReceiver detached. Shutting down.")
             error = EventHubError(str(shutdown), shutdown)
             await self.close_async(exception=error)
             raise error
     except Exception as e:
         log.info("Unexpected error occurred (%r). Shutting down.", e)
         error = EventHubError("Receiver reconnect failed: {}".format(e))
         await self.close_async(exception=error)
         raise error
Exemple #11
0
 def _create_handler(self, auth):
     self._handler = ReceiveClientAsync(
         self._get_source(),
         auth=auth,
         debug=self._config.logging_enable,
         properties=self._properties,
         error_policy=self._error_policy,
         client_name=self._name,
         on_attach=self._on_attach,
         auto_complete=False,
         encoding=self._config.encoding,
         receive_settle_mode=self._mode.value,
         send_settle_mode=SenderSettleMode.Settled
         if self._mode == ReceiveSettleMode.ReceiveAndDelete else None,
         timeout=self._idle_timeout * 1000 if self._idle_timeout else 0,
         prefetch=self._prefetch)
    async def open_async(self, connection):
        """
        Open the Receiver using the supplied conneciton.
        If the handler has previously been redirected, the redirect
        context will be used to create a new handler before opening it.

        :param connection: The underlying client shared connection.
        :type: connection: ~uamqp._async.connection_async.ConnectionAsync
        """
        if self.redirected:
            self._handler = ReceiveClientAsync(self.redirected.address,
                                               auth=None,
                                               debug=self.debug,
                                               prefetch=self.prefetch,
                                               link_properties=self.properties,
                                               timeout=self.timeout,
                                               loop=self.loop)
        await self._handler.open_async(connection=connection)
    async def open_async(self):
        """
        Open the Receiver using the supplied conneciton.
        If the handler has previously been redirected, the redirect
        context will be used to create a new handler before opening it.

        :param connection: The underlying client shared connection.
        :type: connection: ~uamqp.async_ops.connection_async.ConnectionAsync

        Example:
            .. literalinclude:: ../examples/async_examples/test_examples_eventhub_async.py
                :start-after: [START eventhub_client_async_receiver_open]
                :end-before: [END eventhub_client_async_receiver_open]
                :language: python
                :dedent: 4
                :caption: Open the Receiver using the supplied conneciton.

        """
        # pylint: disable=protected-access
        self.running = True
        if self.redirected:
            self.source = self.redirected.address
            source = Source(self.source)
            if self.offset is not None:
                source.set_filter(self.offset.selector())
            alt_creds = {
                "username": self.client._auth_config.get("iot_username"),
                "password":self.client._auth_config.get("iot_password")}
            self._handler = ReceiveClientAsync(
                source,
                auth=self.client.get_auth(**alt_creds),
                debug=self.client.debug,
                prefetch=self.prefetch,
                link_properties=self.properties,
                timeout=self.timeout,
                error_policy=self.retry_policy,
                keep_alive_interval=self.keep_alive,
                client_name=self.name,
                properties=self.client.create_properties(),
                loop=self.loop)
        await self._handler.open_async()
        while not await self._handler.client_ready_async():
            await asyncio.sleep(0.05)
Exemple #14
0
 def _create_handler(self, auth):
     self._handler = ReceiveClientAsync(
         self._get_source(),
         auth=auth,
         debug=self._config.logging_enable,
         properties=self._properties,
         error_policy=self._error_policy,
         client_name=self._name,
         on_attach=self._on_attach,
         auto_complete=False,
         encoding=self._config.encoding,
         receive_settle_mode=ServiceBusToAMQPReceiveModeMap[self._receive_mode],
         send_settle_mode=SenderSettleMode.Settled
         if self._receive_mode == ServiceBusReceiveMode.RECEIVE_AND_DELETE
         else None,
         timeout=self._max_wait_time * 1000 if self._max_wait_time else 0,
         prefetch=self._prefetch_count,
         keep_alive_interval=self._config.keep_alive,
         shutdown_after_timeout=False,
     )