async def create_lease_if_not_exists_async(self, partition_id):
        """
        Create in the store the lease info for the given partition, if it does not exist.
        Do nothing if it does exist in the store already.

        :param partition_id: The ID of a given parition.
        :type partition_id: str
        :return: the existing or newly-created lease info for the partition.
        :rtype: ~azure.eventprocessorhost.lease.Lease
        """
        return_lease = None
        try:
            return_lease = AzureBlobLease()
            return_lease.partition_id = partition_id
            serializable_lease = return_lease.serializable()
            json_lease = json.dumps(serializable_lease)
            _logger.info("Creating Lease %r %r %r",
                         self.lease_container_name,
                         partition_id,
                         json.dumps({k:v for k, v in serializable_lease.items() if k != 'event_processor_context'}))
            await self.host.loop.run_in_executor(
                self.executor,
                functools.partial(
                    self.storage_client.create_blob_from_text,
                    self.lease_container_name,
                    partition_id,
                    json_lease))
        except Exception:  # pylint: disable=broad-except
            try:
                return_lease = await self.get_lease_async(partition_id)
            except Exception as err:  # pylint: disable=broad-except
                _logger.error("Failed to create lease %r", err)
                raise err
        return return_lease
Пример #2
0
    async def create_lease_if_not_exists_async(self, partition_id):
        """
        Create in the store the lease info for the given partition, if it does not exist.
        Do nothing if it does exist in the store already.

        :param partition_id: The ID of a given parition.
        :type partition_id: str
        :return: the existing or newly-created lease info for the partition.
        :rtype: ~azure.eventprocessorhost.lease.Lease
        """
        return_lease = None
        try:
            return_lease = AzureBlobLease()
            return_lease.partition_id = partition_id
            json_lease = json.dumps(return_lease.serializable())
            _logger.info("Creating Lease {} {} {}".format(
                self.lease_container_name,
                partition_id,
                json_lease))
            await self.host.loop.run_in_executor(
                self.executor,
                functools.partial(
                    self.storage_client.create_blob_from_text,
                    self.lease_container_name,
                    partition_id,
                    json_lease))
        except Exception:  # pylint: disable=broad-except
            try:
                return_lease = await self.get_lease_async(partition_id)
            except Exception as err:  # pylint: disable=broad-except
                _logger.error("Failed to create lease {!r}".format(err))
                raise err
        return return_lease
    async def get_lease_async(self, partition_id):
        """
        Return the lease info for the specified partition.
        Can return null if no lease has been created in the store for the specified partition.
        :returns: lease info for the partition, or `None`.
        """
        try:
            blob = await self.host.loop.run_in_executor(
                self.executor,
                functools.partial(self.storage_client.get_blob_to_text,
                                  self.lease_container_name, partition_id))
            lease = AzureBlobLease()
            lease.with_blob(blob)

            async def state():
                """
                Allow lease to curry storage_client to get state
                """
                try:
                    loop = asyncio.get_event_loop()
                    res = await loop.run_in_executor(
                        self.executor,
                        functools.partial(
                            self.storage_client.get_blob_properties,
                            self.lease_container_name, partition_id))
                    return res.properties.lease.state
                except Exception as err:  # pylint: disable=broad-except
                    _logger.error("Failed to get lease state {} {}".format(
                        err, partition_id))

            lease.state = state
            return lease
        except Exception as err:  # pylint: disable=broad-except
            _logger.error("Failed to get lease {} {}".format(
                err, partition_id))
 async def release_lease_async(self, lease):
     """
     Give up a lease currently held by this host. If the lease has been stolen, or expired,
     releasing it is unnecessary, and will fail if attempted.
     :returns: `True` if the lease was released successfully, `False` if not.
     """
     lease_id = None
     try:
         _logger.info("Releasing lease {} {}".format(
             self.host.guid, lease.partition_id))
         lease_id = lease.token
         released_copy = AzureBlobLease()
         released_copy.with_lease(lease)
         released_copy.token = None
         released_copy.owner = None
         released_copy.state = None
         await self.host.loop.run_in_executor(
             self.executor,
             functools.partial(self.storage_client.create_blob_from_text,
                               self.lease_container_name,
                               lease.partition_id,
                               json.dumps(released_copy.serializable()),
                               lease_id=lease_id))
         await self.host.loop.run_in_executor(
             self.executor,
             functools.partial(self.storage_client.release_blob_lease,
                               self.lease_container_name,
                               lease.partition_id, lease_id))
     except Exception as err:  # pylint: disable=broad-except
         _logger.error("Failed to release lease {} {} {}".format(
             err, lease.partition_id, lease_id))
         return False
     return True
    async def update_checkpoint_async(self, lease, checkpoint):
        """
        Update the checkpoint in the store with the offset/sequenceNumber in the provided checkpoint
        checkpoint:offset/sequeceNumber to update the store with.

        :param lease: The stored lease to be updated.
        :type lease: ~azure.eventprocessorhost.lease.Lease
        :param checkpoint: The checkpoint to update the lease with.
        :type checkpoint: ~azure.eventprocessorhost.checkpoint.Checkpoint
        """
        new_lease = AzureBlobLease()
        new_lease.with_source(lease)
        new_lease.offset = checkpoint.offset
        new_lease.sequence_number = checkpoint.sequence_number
        return await self.update_lease_async(new_lease)
Пример #6
0
    async def get_lease_async(self, partition_id):
        """
        Return the lease info for the specified partition.
        Can return null if no lease has been created in the store for the specified partition.

        :param partition_id: The partition ID.
        :type partition_id: str
        :return: lease info for the partition, or `None`.
        :rtype: ~azure.eventprocessorhost.lease.Lease
        """
        try:
            blob = await self.host.loop.run_in_executor(
                self.executor,
                functools.partial(
                    self.storage_client.get_blob_to_text,
                    #self.lease_container_name, partition_id))
                    self.lease_container_name + '/' +
                    self.consumer_group_directory,
                    partition_id))
            lease = AzureBlobLease()
            lease.with_blob(blob)

            async def state():
                """
                Allow lease to curry storage_client to get state
                """
                try:
                    loop = asyncio.get_event_loop()
                    res = await loop.run_in_executor(
                        self.executor,
                        functools.partial(
                            self.storage_client.get_blob_properties,
                            #self.lease_container_name,
                            self.lease_container_name + '/' +
                            self.consumer_group_directory,
                            partition_id))
                    return res.properties.lease.state
                except Exception as err:  # pylint: disable=broad-except
                    _logger.error("Failed to get lease state %r %r", err,
                                  partition_id)

            lease.state = state
            return lease
        except Exception as err:  # pylint: disable=broad-except
            _logger.error("Failed to get lease %r %r", err, partition_id)
Пример #7
0
    async def create_lease_if_not_exists_async(self, partition_id):
        """
        Create in the store the lease info for the given partition, if it does not exist.
        Do nothing if it does exist in the store already.

        :param partition_id: The ID of a given parition.
        :type partition_id: str
        :return: the existing or newly-created lease info for the partition.
        :rtype: ~azure.eventprocessorhost.lease.Lease
        """
        return_lease = None
        try:
            return_lease = AzureBlobLease()
            return_lease.partition_id = partition_id
            serializable_lease = return_lease.serializable()
            json_lease = json.dumps(serializable_lease)
            _logger.info(
                "Creating Lease %r %r %r", self.lease_container_name,
                partition_id,
                json.dumps({
                    k: v
                    for k, v in serializable_lease.items()
                    if k != 'event_processor_context'
                }))
            await self.host.loop.run_in_executor(
                self.executor,
                functools.partial(
                    self.storage_client.create_blob_from_text,
                    self.lease_container_name + '/' +
                    self.consumer_group_directory,
                    #self.lease_container_name,
                    partition_id,
                    json_lease))
        except Exception:  # pylint: disable=broad-except
            try:
                return_lease = await self.get_lease_async(partition_id)
            except Exception as err:  # pylint: disable=broad-except
                _logger.error("Failed to create lease %r", err)
                raise err
        return return_lease
    async def get_lease_async(self, partition_id):
        """
        Return the lease info for the specified partition.
        Can return null if no lease has been created in the store for the specified partition.

        :param partition_id: The partition ID.
        :type partition_id: str
        :return: lease info for the partition, or `None`.
        :rtype: ~azure.eventprocessorhost.lease.Lease
        """
        try:
            blob = await self.host.loop.run_in_executor(
                self.executor,
                functools.partial(
                    self.storage_client.get_blob_to_text,
                    self.lease_container_name, partition_id))
            lease = AzureBlobLease()
            lease.with_blob(blob)
            async def state():
                """
                Allow lease to curry storage_client to get state
                """
                try:
                    loop = asyncio.get_event_loop()
                    res = await loop.run_in_executor(
                        self.executor,
                        functools.partial(
                            self.storage_client.get_blob_properties,
                            self.lease_container_name,
                            partition_id))
                    return res.properties.lease.state
                except Exception as err:  # pylint: disable=broad-except
                    _logger.error("Failed to get lease state %r %r", err, partition_id)

            lease.state = state
            return lease
        except Exception as err:  # pylint: disable=broad-except
            _logger.error("Failed to get lease %r %r", err, partition_id)
 async def update_checkpoint_async(self, lease, checkpoint):
     """
     Update the checkpoint in the store with the offset/sequenceNumber in the provided checkpoint
     checkpoint:offset/sequeceNumber to update the store with.
     """
     new_lease = AzureBlobLease()
     new_lease.with_source(lease)
     new_lease.offset = checkpoint.offset
     new_lease.sequence_number = checkpoint.sequence_number
     await self.update_lease_async(new_lease)
Пример #10
0
    async def release_lease_async(self, lease):
        """
        Give up a lease currently held by this host. If the lease has been stolen, or expired,
        releasing it is unnecessary, and will fail if attempted.

        :param lease: The stored lease to be released.
        :type lease: ~azure.eventprocessorhost.lease.Lease
        :return: `True` if the lease was released successfully, `False` if not.
        :rtype: bool
        """
        lease_id = None
        try:
            _logger.info("Releasing lease %r %r", self.host.guid,
                         lease.partition_id)
            lease_id = lease.token
            released_copy = AzureBlobLease()
            released_copy.with_lease(lease)
            released_copy.token = None
            released_copy.owner = None
            released_copy.state = None
            await self.host.loop.run_in_executor(
                self.executor,
                functools.partial(
                    self.storage_client.create_blob_from_text,
                    #self.lease_container_name,
                    self.lease_container_name + '/' +
                    self.consumer_group_directory,
                    lease.partition_id,
                    json.dumps(released_copy.serializable()),
                    lease_id=lease_id))
            await self.host.loop.run_in_executor(
                self.executor,
                functools.partial(
                    self.storage_client.release_blob_lease,
                    #self.lease_container_name,
                    self.lease_container_name + '/' +
                    self.consumer_group_directory,
                    lease.partition_id,
                    lease_id))
        except Exception as err:  # pylint: disable=broad-except
            _logger.error("Failed to release lease %r %r %r", err,
                          lease.partition_id, lease_id)
            return False
        return True
Пример #11
0
    async def update_checkpoint_async(self, lease, checkpoint):
        """
        Update the checkpoint in the store with the offset/sequenceNumber in the provided checkpoint
        checkpoint:offset/sequeceNumber to update the store with.

        :param lease: The stored lease to be updated.
        :type lease: ~azure.eventprocessorhost.lease.Lease
        :param checkpoint: The checkpoint to update the lease with.
        :type checkpoint: ~azure.eventprocessorhost.checkpoint.Checkpoint
        """
        new_lease = AzureBlobLease()
        new_lease.with_source(lease)
        new_lease.offset = checkpoint.offset
        new_lease.sequence_number = checkpoint.sequence_number
        return await self.update_lease_async(new_lease)
    async def release_lease_async(self, lease):
        """
        Give up a lease currently held by this host. If the lease has been stolen, or expired,
        releasing it is unnecessary, and will fail if attempted.

        :param lease: The stored lease to be released.
        :type lease: ~azure.eventprocessorhost.lease.Lease
        :return: `True` if the lease was released successfully, `False` if not.
        :rtype: bool
        """
        lease_id = None
        try:
            _logger.info("Releasing lease %r %r", self.host.guid, lease.partition_id)
            lease_id = lease.token
            released_copy = AzureBlobLease()
            released_copy.with_lease(lease)
            released_copy.token = None
            released_copy.owner = None
            released_copy.state = None
            await self.host.loop.run_in_executor(
                self.executor,
                functools.partial(
                    self.storage_client.create_blob_from_text,
                    self.lease_container_name,
                    lease.partition_id,
                    json.dumps(released_copy.serializable()),
                    lease_id=lease_id))
            await self.host.loop.run_in_executor(
                self.executor,
                functools.partial(
                    self.storage_client.release_blob_lease,
                    self.lease_container_name,
                    lease.partition_id,
                    lease_id))
        except Exception as err:  # pylint: disable=broad-except
            _logger.error("Failed to release lease %r %r %r",
                          err, lease.partition_id, lease_id)
            return False
        return True