private async Task ExecuteAsync(CancellationToken cancellationToken) { var storageConnectionString = _configuration["azureStorageConnectionString"]; if (string.IsNullOrWhiteSpace(storageConnectionString)) { return; } var repo = new Core.Repositories.TableStorage.EventRepository(storageConnectionString); _eventWriteService = new RepositoryEventWriteService(repo); _queueClient = new QueueClient(storageConnectionString, "event"); while (!cancellationToken.IsCancellationRequested) { try { var messages = await _queueClient.ReceiveMessagesAsync(32); if (messages.Value?.Any() ?? false) { foreach (var message in messages.Value) { await ProcessQueueMessageAsync(message.DecodeMessageText(), cancellationToken); await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt); } } else { await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); } } catch (Exception e) { _logger.LogError(e, "Exception occurred: " + e.Message); await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); } } _logger.LogWarning("Done processing."); }
private async Task ExecuteAsync(CancellationToken cancellationToken) { var storageConnectionString = _configuration["azureStorageConnectionString"]; if (string.IsNullOrWhiteSpace(storageConnectionString)) { return; } var repo = new Core.Repositories.TableStorage.EventRepository(storageConnectionString); _eventWriteService = new RepositoryEventWriteService(repo); var storageAccount = CloudStorageAccount.Parse(storageConnectionString); var queueClient = storageAccount.CreateCloudQueueClient(); _queue = queueClient.GetQueueReference("event"); while (!cancellationToken.IsCancellationRequested) { var messages = await _queue.GetMessagesAsync(32, TimeSpan.FromMinutes(1), null, null, cancellationToken); if (messages.Any()) { foreach (var message in messages) { await ProcessQueueMessageAsync(message.AsString, cancellationToken); await _queue.DeleteMessageAsync(message); } } else { await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); } } }