public void GivenIHaveAlreadyAcquiredTheLease()
 {
     var lease = new Lease(new AzureLeaseProvider(new ConnectionStringProvider()), new AzureLeasePolicyValidator());
     var policy = ScenarioContext.Current.Get<LeasePolicy>();
     lease.AcquireAsync(policy).Wait();
     ScenarioContext.Current.Set(lease);
 }
 public void GivenActorAHasAlreadyAcquiredALeaseForAnOperationCalled(string leaseName)
 {
     var policy = new LeasePolicy { Name = leaseName, Duration = TimeSpan.FromSeconds(15) };
     var lease = new Lease(new AzureLeaseProvider(new ConnectionStringProvider()), new AzureLeasePolicyValidator());
     lease.AcquireAsync(policy).Wait();
     ScenarioContext.Current.Set(lease, "ActorALease");
 }
 public void GivenTheLeaseHasExpired()
 {
     var lease = new Lease(new AzureLeaseProvider(new ConnectionStringProvider()), new AzureLeasePolicyValidator());
     var policy = ScenarioContext.Current.Get<LeasePolicy>();
     lease.AcquireAsync(policy).Wait();
     Thread.Sleep(policy.Duration.Value.Add(TimeSpan.FromSeconds(2)));
     ScenarioContext.Current.Set(lease);
 }
Exemple #4
0
        private async Task AcquireLeaseAndExecuteInnerAsync <T>(Func <CancellationToken, T, Task> action, CancellationTokenSource cancellationTokenSource, ILeaseProvider leaseProvider, T arg)
        {
            using (var lease = new Lease(leaseProvider, this.leasePolicyValidator))
            {
                try
                {
                    await this.AcquireLeaseAndStartRenewingAsync(cancellationTokenSource.Token, leaseProvider, lease);

                    await action(cancellationTokenSource.Token, arg);

                    Trace.TraceInformation("[{0}] Action completed using lease policy name : {1}", this.LeasePolicy.ActorName, this.LeasePolicy.Name);

                    cancellationTokenSource.Cancel();
                }
                catch (LeaseAcquisitionUnsuccessfulException)
                {
                    Trace.TraceInformation("[{0}] Lease could not be obtained with policy name : {1}.", this.LeasePolicy.ActorName, this.LeasePolicy.Name);
                    throw;
                }
            }
        }
Exemple #5
0
        public async Task <Lease> GetAutoRenewingLeaseAsync(CancellationToken cancellationToken, string leaseName, string actorName = "")
        {
            var leaseProvider = this.leaseProviderFactory.Create();

            this.LeasePolicy = new LeasePolicy {
                Duration = leaseProvider.DefaultLeaseDuration, Name = leaseName, ActorName = actorName
            };
            this.RetryStrategy = new Linear(TimeSpan.FromSeconds(Math.Round(leaseProvider.DefaultLeaseDuration.TotalSeconds / 3)), int.MaxValue);
            this.RetryPolicy   = new RetryUntilLeaseAcquiredPolicy();

            this.CheckProperties();

            var lease = new Lease(leaseProvider, this.leasePolicyValidator);

            await Retriable.RetryAsync(async() =>
            {
                await this.AcquireLeaseAndStartRenewingAsync(cancellationToken, leaseProvider, lease);
            },
                                       cancellationToken,
                                       this.RetryStrategy,
                                       this.RetryPolicy);

            return(lease);
        }
Exemple #6
0
        private void AcquireRenewingLease(CancellationToken cancellationToken, Lease lease, TimeSpan renewEvery)
        {
            var renewalTask = Task.Factory.StartNew(async() =>
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    cancellationToken.WaitHandle.WaitOne(renewEvery);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }

                    Trace.TraceInformation("[{0}] Attempting to extend the Lease using the existing lease policy name : {1} - duration: {2}", this.LeasePolicy.ActorName, this.LeasePolicy.Name, this.LeasePolicy.Duration);

                    await lease.ExtendAsync(cancellationToken);

                    Trace.TraceInformation("[{0}] Lease was successfully extended for lease policy name : {1} - duration: {2}", this.LeasePolicy.ActorName, this.LeasePolicy.Name, this.LeasePolicy.Duration);
                }
            },
                                                    cancellationToken);

            renewalTask.ContinueWith(t => { }, TaskScheduler.Current);
        }
Exemple #7
0
        private async Task AcquireLeaseAndStartRenewingAsync(CancellationToken cancellationToken, ILeaseProvider leaseProvider, Lease lease)
        {
            Trace.TraceInformation("[{0}] Attempting to acquire a Lease with a lease policy name : {1} - duration: {2}",
                                   this.LeasePolicy.ActorName, this.LeasePolicy.Name, this.LeasePolicy.Duration);

            await lease.AcquireAsync(this.LeasePolicy);

            Trace.TraceInformation(
                "[{0}] Lease was successfully acquired for lease policy name : {1} - duration: {2}",
                this.LeasePolicy.ActorName,
                this.LeasePolicy.Name,
                this.LeasePolicy.Duration);

            var leaseDuration = this.LeasePolicy.Duration.HasValue
                ? this.LeasePolicy.Duration.Value.Add(TimeSpan.FromSeconds(-5))
                : leaseProvider.DefaultLeaseDuration.Add(TimeSpan.FromSeconds(-1));

            var renewEvery = TimeSpan.FromSeconds(Math.Round(leaseDuration.TotalSeconds / 3));

            this.AcquireRenewingLease(cancellationToken, lease, renewEvery);
        }
        public void WhenIAcquireTheLease()
        {
            var policy = ScenarioContext.Current.Get<LeasePolicy>();

            Lease lease;
            if (!ScenarioContext.Current.TryGetValue(out lease))
            {
                lease = new Lease(new AzureLeaseProvider(new ConnectionStringProvider()), new AzureLeasePolicyValidator());
            }

            try
            {
                lease.AcquireAsync(policy).Wait();
            }
            catch (AggregateException ex)
            {
                ScenarioContext.Current.Add("AggregateException", ex);
            }
            catch (Exception ex)
            {
                ScenarioContext.Current.Add("Exception", ex);
            }

            ScenarioContext.Current.Set(lease);
        }