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 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 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)
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
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
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)