Exemplo n.º 1
0
        public async Task Add(ActiveDeployment activeDeployment)
        {
            var queue = _queueClient.GetQueueReference(QueueName);
            await queue.CreateIfNotExistsAsync();

            await queue.AddMessageAsync(
                new CloudQueueMessage(
                    JsonConvert.SerializeObject(activeDeployment)));
        }
        private async Task MonitorDeployment(ActiveDeployment activeDeployment)
        {
            _logger.LogDebug($"Waiting for storage {activeDeployment.StorageName} deployment to complete");

            using (var cts = new CancellationTokenSource())
            {
                // runs in background
                var renewer = _leaseMaintainer.MaintainLease(activeDeployment, cts.Token);

                try
                {
                    var deploymentState = ProvisioningState.Running;

                    while (deploymentState == ProvisioningState.Running)
                    {
                        var fileServer = (NfsFileServer)await _assetRepoCoordinator.GetRepository(activeDeployment.StorageName);

                        if (fileServer == null)
                        {
                            break;
                        }

                        await _assetRepoCoordinator.UpdateRepositoryFromDeploymentAsync(fileServer);

                        deploymentState = fileServer.Deployment.ProvisioningState;

                        _logger.LogDebug($"[MonitorDeployment={activeDeployment.StorageName}] " +
                                         $"Deployment returned state {deploymentState}");

                        if (deploymentState == ProvisioningState.Running)
                        {
                            await Task.Delay(TimeSpan.FromSeconds(15), cts.Token);
                        }
                    }
                }
                finally
                {
                    _logger.LogDebug($"[MonitorDeployment={activeDeployment.StorageName}] " +
                                     $"Deleting queue message {activeDeployment.MessageId} " +
                                     $"with receipt {activeDeployment.PopReceipt}");

                    await _deploymentQueue.Delete(activeDeployment.MessageId, activeDeployment.PopReceipt);

                    cts.Cancel();
                    try
                    {
                        await renewer;
                    }
                    catch (OperationCanceledException)
                    {
                        // expected
                    }
                }
            }
        }
 private async Task ExecuteMessage(ActiveDeployment activeDeployment)
 {
     if (activeDeployment.Action == "DeleteVM")
     {
         await DeleteDeployment(activeDeployment);
     }
     else
     {
         await MonitorDeployment(activeDeployment);
     }
 }
 private async Task ExecuteMessage(ActiveDeployment activeDeployment)
 {
     if (activeDeployment.DequeueCount > 10)
     {
         _logger.LogWarning(
             $"Deleting background task for {activeDeployment.StorageName} as it has exceeded the " +
             $"maximum dequeue count.");
         await _deploymentQueue.Delete(activeDeployment.MessageId, activeDeployment.PopReceipt);
     }
     else if (activeDeployment.Action == "DeleteVM")
     {
         await DeleteDeployment(activeDeployment);
     }
     else
     {
         await MonitorDeployment(activeDeployment);
     }
 }
        private async Task DeleteDeployment(ActiveDeployment storageDeployment)
        {
            _logger.LogDebug($"[DeleteStorage={storageDeployment.StorageName}] Deleting storage");

            using (var cts = new CancellationTokenSource())
            {
                // runs in background
                var renewer = _leaseMaintainer.MaintainLease(storageDeployment, cts.Token);

                try
                {
                    var repository = await _assetRepoCoordinator.GetRepository(storageDeployment.StorageName);

                    if (repository != null)
                    {
                        await _assetRepoCoordinator.DeleteRepositoryResourcesAsync(repository, storageDeployment.DeleteResourceGroup);
                    }

                    _logger.LogDebug($"[DeleteStorage={storageDeployment.StorageName}] Deleting queue message {storageDeployment.MessageId} with receipt {storageDeployment.PopReceipt}");

                    await _deploymentQueue.Delete(storageDeployment.MessageId, storageDeployment.PopReceipt);
                }
                catch (CloudException e)
                {
                    _logger.LogError(e, $"[DeleteStorage={storageDeployment.StorageName}]");
                }
                finally
                {
                    cts.Cancel();

                    try
                    {
                        await renewer;
                    }
                    catch (OperationCanceledException)
                    {
                        // expected
                    }
                }
            }
        }
        private async Task DeleteDeployment(ActiveDeployment activeDeployment)
        {
            _logger.LogDebug($"Deleting file server {activeDeployment.FileServerName}");

            using (var cts = new CancellationTokenSource())
            {
                // runs in background
                var renewer = _leaseMaintainer.MaintainLease(activeDeployment, cts.Token);

                try
                {
                    var repository = await _assetRepoCoordinator.GetRepository(activeDeployment.FileServerName);

                    if (repository != null)
                    {
                        await _assetRepoCoordinator.DeleteRepositoryResourcesAsync(repository, _managementClientProvider);
                    }

                    await _deploymentQueue.Delete(activeDeployment.MessageId, activeDeployment.PopReceipt);
                }
                catch (CloudException e)
                {
                    _logger.LogError(e, $"Error deleting file server {activeDeployment.FileServerName}");
                }
                finally
                {
                    cts.Cancel();

                    try
                    {
                        await renewer;
                    }
                    catch (OperationCanceledException)
                    {
                        // expected
                    }
                }
            }
        }