Exemplo n.º 1
0
        public async Task PopulateDomainTopicQueue(DataCleanupParameters parameters)
        {
            _log.LogDebug("Queuing domain topics for deletion.");

            try
            {
                string      storageConnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage") ?? "UseDevelopmentStorage=true";
                QueueClient queuePendingDelete      = _queueManager.CreateQueueClient(storageConnectionString, QueueNameHoldingDomainTopicsToDelete);
                await _queueManager.CreateIfNotExistsAsync(queuePendingDelete);

                EventGridManagementClient eventGridManagementClient = await _eventGridManager.GetEventGridManagementClient(
                    parameters.SubscriptionId,
                    parameters.ServicePrincipalClientId,
                    parameters.ServicePrincipalClientKey,
                    string.Concat(@"https://login.windows.net/", parameters.ServicePrincipalTenantId),
                    @"https://management.azure.com/");

                (List <string> domainTopicNames, string nextPageLink) = await _eventGridManager.GetDomainTopics(
                    eventGridManagementClient,
                    parameters.ResourceGroupName,
                    parameters.EventGridName,
                    parameters.DomainTopicNextpage);

                var queueAddTasks = new List <Task>();
                foreach (var domainTopic in domainTopicNames)
                {
                    parameters.DomainTopicName = domainTopic;
                    string deleteMessage = JsonSerializer.Serialize(parameters);
                    queueAddTasks.Add(_queueManager.SendMessageAsync(queuePendingDelete, deleteMessage));

                    _log.LogDebug($"Found {domainTopic}");
                }

                _log.LogDebug($"{queueAddTasks.Count} domain topics being added to queue for deletion.");

                // If there is another page of domain topics, then add a task to the queue to parse it.
                if (!string.IsNullOrWhiteSpace(nextPageLink))
                {
                    QueueClient queuePendingList = _queueManager.CreateQueueClient(storageConnectionString, QueueNameHoldingDomainTopicsToList);
                    await _queueManager.CreateIfNotExistsAsync(queuePendingList);

                    parameters.DomainTopicNextpage = nextPageLink;
                    parameters.DomainTopicName     = null;
                    string nextPageParameters = JsonSerializer.Serialize(parameters);
                    queueAddTasks.Add(_queueManager.SendMessageAsync(queuePendingList, nextPageParameters));

                    _log.LogDebug("Added next domain topic page to queue.");
                }

                await Task.WhenAll(queueAddTasks);
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "Exception encountered in PopulateDomainTopicQueue method.");
                throw;
            }

            _log.LogDebug("Finished processing page of domain topics.");

            return;
        }