/// <summary>
        /// Attempts to acquire a lease based on the provided lease policy.
        /// </summary>
        /// <param name="leasePolicy">The configuration details for the lease.</param>
        /// <param name="leaseId">The id of the currently acquired lease.</param>
        /// <returns>A task for the async operation.</returns>
        public async Task<string> AcquireAsync(ILeasePolicy leasePolicy, string leaseId)
        {
            this.LeasePolicy = leasePolicy;

            await this.InitialiseAsync();

            if (!this.blob.Exists())
            {
                await Retriable.RetryAsync(async () =>
                {
                    using (var ms = new MemoryStream())
                    {
                        await this.blob.UploadFromStreamAsync(ms);
                    }
                });
            }

            try
            {
                return await Retriable.RetryAsync(
                        () => this.blob.AcquireLeaseAsync(leasePolicy.Duration, leaseId),
                        new CancellationToken(),
                        new Count(10),
                        new DoNotRetryOnConflictPolicy());
            }
            catch (StorageException exception)
            {
                if (exception.RequestInformation.HttpStatusCode == 409)
                {
                    throw new LeaseAcquisitionUnsuccessfulException(this.LeasePolicy, exception);
                }
                
                throw;
            }
        }
示例#2
0
        /// <summary>
        /// Attempts to acquire a lease based on the provided lease policy.
        /// </summary>
        /// <param name="leasePolicy">The configuration details for the lease.</param>
        /// <param name="leaseId">The id of the currently acquired lease.</param>
        /// <returns>A task for the async operation.</returns>
        public async Task <string> AcquireAsync(ILeasePolicy leasePolicy, string leaseId)
        {
            this.LeasePolicy = leasePolicy;

            await this.InitialiseAsync();

            if (!this.blob.Exists())
            {
                await Retriable.RetryAsync(async() =>
                {
                    using (var ms = new MemoryStream())
                    {
                        await this.blob.UploadFromStreamAsync(ms);
                    }
                });
            }

            try
            {
                return(await Retriable.RetryAsync(
                           () => this.blob.AcquireLeaseAsync(leasePolicy.Duration, leaseId),
                           new CancellationToken(),
                           new Count(10),
                           new DoNotRetryOnConflictPolicy()));
            }
            catch (StorageException exception)
            {
                if (exception.RequestInformation.HttpStatusCode == 409)
                {
                    throw new LeaseAcquisitionUnsuccessfulException(this.LeasePolicy, exception);
                }

                throw;
            }
        }
示例#3
0
        /// <summary>
        /// Attempts to acquire a lease based on the provided lease policy.
        /// </summary>
        /// <param name="leasePolicy">The configuration details for the lease.</param>
        /// <returns>A task for the async operation.</returns>
        public async Task AcquireAsync(ILeasePolicy leasePolicy)
        {
            this.LeasePolicyValidator.Validate(leasePolicy);

            this.LeasePolicy = leasePolicy;

            this.Id = await this.leaseProvider.AcquireAsync(this.LeasePolicy, this.Id);

            if (!string.IsNullOrEmpty(this.Id))
            {
                this.LastAcquired = DateTimeOffset.UtcNow;
            }
        }
示例#4
0
        /// <summary>
        /// Attempts to acquire a lease based on the provided lease policy.
        /// </summary>
        /// <param name="leasePolicy">The configuration details for the lease.</param>
        /// <returns>A task for the async operation.</returns>
        public async Task AcquireAsync(ILeasePolicy leasePolicy)
        {
            this.LeasePolicyValidator.Validate(leasePolicy);

            this.LeasePolicy = leasePolicy;

            this.Id = await this.leaseProvider.AcquireAsync(this.LeasePolicy, this.Id);

            if (!string.IsNullOrEmpty(this.Id))
            {
                this.LastAcquired = DateTimeOffset.UtcNow;
            }
        }
        /// <summary>
        /// Validates whether the proposed ILeasePolicy is valid for the given lease implementation.
        /// </summary>
        /// <remarks>
        /// If lease policy is invalid thrown a InvalidArgumentException
        /// </remarks>
        /// <param name="leasePolicy">ILeasePolicy to validate</param>
        public void Validate(ILeasePolicy leasePolicy)
        {
            if (leasePolicy.Duration.HasValue && (leasePolicy.Duration < TimeSpan.FromSeconds(15) || leasePolicy.Duration > TimeSpan.FromSeconds(59)))
            {
                throw new ArgumentOutOfRangeException("leasePolicy", "Duration proprerty is out of range. Azure finite blob lease must be betweem 15 and 59 seconds");
            }

            if (leasePolicy.Name == null)
            {
                throw new ArgumentNullException("leasePolicy", "Name property must not be null");
            }

            if (leasePolicy.Name.Length < 1 || leasePolicy.Name.Length > 1024)
            {
                throw new ArgumentOutOfRangeException("leasePolicy", "Name property must be between 1 and 1,024 characters.");
            }

            if (leasePolicy.Name.EndsWith(".") || leasePolicy.Name.EndsWith("/"))
            {
                throw new ArgumentException("Name property must not end with a dot ('.') or a forward slash ('/')", "leasePolicy");
            }
        }
 /// <summary>
 /// Initializes a new instance of the LeaseAcquisitionUnsuccessfulException class containing the lease policy that was used during the attempt.
 /// </summary>
 /// <param name="leasePolicy">The lease policy used during the attempt to acquire the lease.</param>
 /// <param name="innerException">Exception that cause the lease to be unsuccessfully acquired.</param>
 public LeaseAcquisitionUnsuccessfulException(ILeasePolicy leasePolicy, Exception innerException) : base(string.Empty, innerException)
 {
     this.LeasePolicy = leasePolicy;
 }
 /// <summary>
 /// Initializes a new instance of the LeaseAcquisitionUnsuccessfulException class containing the lease policy that was used during the attempt.
 /// </summary>
 /// <param name="leasePolicy">The lease policy used during the attempt to acquire the lease.</param>
 /// <param name="innerException">Exception that cause the lease to be unsuccessfully acquired.</param>
 public LeaseAcquisitionUnsuccessfulException(ILeasePolicy leasePolicy, Exception innerException) : base(string.Empty, innerException)
 {
     this.LeasePolicy = leasePolicy;
 }