Ejemplo n.º 1
0
 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
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
    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)
 def _get_source_for_session_entity(self):
     source = Source(self._entity_uri)
     session_filter = None if self._session_id == NEXT_AVAILABLE else self._session_id
     source.set_filter(session_filter, name=SESSION_FILTER, descriptor=None)
     return source
Ejemplo n.º 5
0
 def _reconnect(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")
     }
     self._handler.close()
     source = Source(self.source)
     if self.offset is not None:
         source.set_filter(self.offset.selector())
     self._handler = ReceiveClient(
         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())
     try:
         self._handler.open()
         while not self._handler.client_ready():
             time.sleep(0.05)
         return True
     except errors.TokenExpired as shutdown:
         log.info(
             "Receiver disconnected due to token expiry. Shutting down.")
         error = EventHubError(str(shutdown), shutdown)
         self.close(exception=error)
         raise error
     except (errors.LinkDetach, errors.ConnectionClose) as shutdown:
         if shutdown.action.retry and self.auto_reconnect:
             log.info("Receiver detached. Attempting reconnect.")
             return False
         log.info("Receiver detached. Shutting down.")
         error = EventHubError(str(shutdown), shutdown)
         self.close(exception=error)
         raise error
     except errors.MessageHandlerError as shutdown:
         if self.auto_reconnect:
             log.info("Receiver detached. Attempting reconnect.")
             return False
         log.info("Receiver detached. Shutting down.")
         error = EventHubError(str(shutdown), shutdown)
         self.close(exception=error)
         raise error
     except errors.AMQPConnectionError as shutdown:
         if str(shutdown).startswith("Unable to open authentication session"
                                     ) and self.auto_reconnect:
             log.info(
                 "Receiver couldn't authenticate. Attempting reconnect.")
             return False
         log.info("Receiver connection error (%r). Shutting down.",
                  shutdown)
         error = EventHubError(str(shutdown))
         self.close(exception=error)
         raise error
     except Exception as e:
         log.info("Unexpected error occurred (%r). Shutting down.", e)
         error = EventHubError("Receiver reconnect failed: {}".format(e))
         self.close(exception=error)
         raise error
 def _get_source(self):
     source = Source(self.endpoint)
     session_filter = None if self.session_filter == NEXT_AVAILABLE else self.session_filter
     source.set_filter(session_filter, name=SESSION_FILTER, descriptor=None)
     return source