private async Task <IWatcherCheckResult> EnsureAsync(NamespaceManager namespaceManager) { bool isValid = true; if (_configuration.EnsureThatAsync != null) { isValid = await _configuration.EnsureThatAsync?.Invoke(namespaceManager); } isValid = isValid && (_configuration.EnsureThat?.Invoke(namespaceManager) ?? true); return(AzureServiceBusWatcherCheckResult.Create(this, isValid, namespaceManager, $"Service Bus {namespaceManager.Address} is checked with result {isValid}")); }
public async Task <IWatcherCheckResult> ExecuteAsync() { try { NamespaceManager manager = NamespaceManager.CreateFromConnectionString(_configuration.ConnectionString); List <string> errors = new List <string>(); if (_configuration.QueueProcessingConfiguration.MonitorProcessing || _configuration.MonitorQueues.Any()) { var queues = (await manager.GetQueuesAsync()).ToList(); if (_configuration.QueueProcessingConfiguration.MonitorProcessing) { List <QueueDescription> queuesToMonitor = GetMonitorProcessingQueues(_configuration.QueueProcessingConfiguration, queues); foreach (QueueDescription queueDescription in queuesToMonitor) { QueueClient client = QueueClient.CreateFromConnectionString(_configuration.ConnectionString, queueDescription.Path); BrokeredMessage nextMessage = null; bool peeked = false; while (!peeked || (nextMessage != null && nextMessage.State != MessageState.Active)) { nextMessage = await client.PeekAsync(); peeked = true; } if (nextMessage == null) { continue; } string lastMessageId; _queueProcessingMonitoring.TryGetValue(queueDescription.Path, out lastMessageId); if (lastMessageId == nextMessage.MessageId) { errors.Add( $"The queue `{queueDescription.Path}` seems to be stalling it's processing."); } else { _queueProcessingMonitoring.TryAdd(queueDescription.Path, nextMessage.MessageId); } } } if (_configuration.MonitorQueues.Any()) { var queuesToMonitor = GetSpecificQueues(queues, _configuration.MonitorQueues); foreach (QueueDescription queueDescription in queuesToMonitor) { QueueClient client = QueueClient.CreateFromConnectionString(_configuration.ConnectionString, queueDescription.Path); MonitoringState monitoringState; if (!_queueMonitoringStates.TryGetValue(queueDescription.Path, out monitoringState)) { monitoringState = new MonitoringState(); _queueMonitoringStates.Add(queueDescription.Path, monitoringState); } bool hasNewMessages = true; while (hasNewMessages) { var message = await client.PeekAsync(); if (message != null) { if (!monitoringState.MessageIds.Contains(message.MessageId)) { monitoringState.MessageIds.Add(message.MessageId); errors.Add($"The queue `{queueDescription.Path}` has one new message. Message Id `{message.MessageId}`. Content: ```{new StreamReader(message.GetBody<Stream>()).ReadToEnd()}```"); } } else { hasNewMessages = false; } } } } } if (_configuration.TopicProcessingConfiguration.MonitorProcessing || _configuration.MonitorTopics.Any()) { var topics = (await manager.GetTopicsAsync()).ToList(); if (_configuration.TopicProcessingConfiguration.MonitorProcessing) { var topicsToMonitor = GetMonitorProcessingTopics(_configuration.TopicProcessingConfiguration, topics); foreach (TopicDescription topicDescription in topicsToMonitor) { TopicClient client = TopicClient.CreateFromConnectionString(_configuration.ConnectionString, topicDescription.Path); var nextMessage = await client.PeekAsync(); if (nextMessage == null) { continue; } string lastMessageId; _topicProcessingMonitoring.TryGetValue(topicDescription.Path, out lastMessageId); if (lastMessageId == nextMessage.MessageId) { errors.Add( $"The topic `{topicDescription.Path}` seems to be stalling it's processing. Message Id {nextMessage.MessageId}."); } else { _topicProcessingMonitoring.TryAdd(topicDescription.Path, nextMessage.MessageId); } } } if (_configuration.MonitorTopics.Any()) { var topicsToMonitor = GetSpecificTopics(topics, _configuration.MonitorTopics); foreach (TopicDescription topicDescription in topicsToMonitor) { TopicClient client = TopicClient.CreateFromConnectionString(_configuration.ConnectionString, topicDescription.Path); MonitoringState monitoringState; if (!_topicMonitoringStates.TryGetValue(topicDescription.Path, out monitoringState)) { monitoringState = new MonitoringState(); _topicMonitoringStates.Add(topicDescription.Path, monitoringState); } bool hasNewMessages = true; while (hasNewMessages) { var message = await client.PeekAsync(); if (message != null) { if (!monitoringState.MessageIds.Contains(message.MessageId)) { monitoringState.MessageIds.Add(message.MessageId); errors.Add($"The queue `{topicDescription.Path}` has one new message. Message Id `{message.MessageId}`. Content: ```{new StreamReader(message.GetBody<Stream>()).ReadToEnd()}```"); } } else { hasNewMessages = false; } } } } } if (errors.Any()) { return(AzureServiceBusWatcherCheckResult.Create(this, false, manager, string.Join("\r\n", errors))); } return(await EnsureAsync(manager)); } catch (Exception exception) { throw new WatcherException($"There was an error while trying to access the Service Bus", exception); } }