Exemple #1
0
        public void Dispose()
        {
            if (_cancellationToken != null && !_cancellationToken.IsCancellationRequested)
            {
                try
                {
                    _cancellationToken.Cancel();
                    _resetEvent.Set();
                }
                catch (Exception ex)
                {
                    _logger.Error("An error occured cancelling the web deploy lease thread.", ex);
                }
            }
            if (_leaseId == null)
            {
                return;
            }

            try
            {
                var blob = AzureRoleEnvironment.WebDeployLeaseBlob();
                blob.TryReleaseLease(_leaseId);
                blob.Metadata.Remove("InstanceId");
                blob.SetMetadata();
                _leaseId = null;
            }
            catch (Exception ex)
            {
                _logger.Error("An exception occured when attempting to clear the InstanceId from the web deploy lease metadata.", ex);
            }
        }
Exemple #2
0
        public void Start()
        {
            _cancellationToken = new CancellationTokenSource();
            _resetEvent        = new ManualResetEvent(false);

            Task.Factory.StartNew(() =>
            {
                _logger.Debug("Starting web deploy leasing thread...");

                while (true)
                {
                    try
                    {
                        var blob = AzureRoleEnvironment.WebDeployLeaseBlob();
                        using (var lease = new AutoRenewLease(_loggerFactory, _logLevel, blob))
                        {
                            _logger.DebugFormat("Leasing thread checking...HasLease: {0}", lease.HasLease);

                            while (lease.HasLease)
                            {
                                if (_leaseId != lease.LeaseId)
                                {
                                    _logger.DebugFormat("This instance ({0}) has the lease, updating blob with the instance ID.", AzureRoleEnvironment.CurrentRoleInstanceId());
                                    blob.Metadata["InstanceId"] = AzureRoleEnvironment.CurrentRoleInstanceId();
                                    blob.SetMetadata(lease.LeaseId);
                                    _leaseId = lease.LeaseId;
                                }

                                _resetEvent.WaitOne(TimeSpan.FromSeconds(10));
                                if (_cancellationToken.IsCancellationRequested)
                                {
                                    return;
                                }
                            }
                            if (!_cancellationToken.IsCancellationRequested)
                            {
                                _leaseId = null;
                            }
                        }

                        _resetEvent.WaitOne(TimeSpan.FromSeconds(30));
                        if (_cancellationToken.IsCancellationRequested)
                        {
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.ErrorFormat(ex, "Failed to manage lease on {0}", AzureRoleEnvironment.CurrentRoleInstanceId());
                        _resetEvent.WaitOne(TimeSpan.FromSeconds(30));
                        if (_cancellationToken.IsCancellationRequested)
                        {
                            return;
                        }
                    }
                }
            }, _cancellationToken.Token);
        }
Exemple #3
0
        public void Release_lease_when_requested()
        {
            _service.Start();
            Thread.Sleep(TimeSpan.FromSeconds(4));
            _service.Dispose();
            Thread.Sleep(TimeSpan.FromSeconds(2));

            var hasWebDeployLease = AzureRoleEnvironment.HasWebDeployLease();

            Assert.That(hasWebDeployLease, Is.False);
            Assert.That(AzureRoleEnvironment.WebDeployLeaseBlob().Metadata["InstanceId"], Is.Null);
        }