예제 #1
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 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 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
예제 #4
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
예제 #5
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 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