|
1 | | -using System; |
| 1 | +using System; |
2 | 2 | using System.Collections.Concurrent; |
3 | 3 | using System.Collections.Generic; |
4 | 4 | using System.Diagnostics; |
@@ -69,18 +69,27 @@ protected QueueBase(TOptions options) : base(options?.TimeProvider, options?.Log |
69 | 69 | { |
70 | 70 | if (options.MetricsPollingInterval > TimeSpan.Zero && _nextQueueStatsUpdate >= _timeProvider.GetUtcNow()) |
71 | 71 | { |
72 | | - return _queueStats is not null ? (_queueStats.Queued, _queueStats.Working, _queueStats.Deadletter) : (0, 0, 0); |
| 72 | + if (_queueStats is not null) |
| 73 | + { |
| 74 | + _logger.LogTrace("Using cached queue stats for {QueueName} ({QueueId})", _options.Name, QueueId); |
| 75 | + return (_queueStats.Queued, _queueStats.Working, _queueStats.Deadletter); |
| 76 | + } |
| 77 | + |
| 78 | + _logger.LogTrace("Returning default queue stats for {QueueName} ({QueueId})", _options.Name, QueueId); |
| 79 | + return (0, 0, 0); |
73 | 80 | } |
74 | 81 |
|
75 | 82 | _nextQueueStatsUpdate = _timeProvider.GetUtcNow().UtcDateTime.Add(_options.MetricsPollingInterval); |
| 83 | + _logger.LogTrace("Getting metrics queue stats for {QueueName} ({QueueId}): Next update scheduled for {NextQueueStatsUpdate:O}", _options.Name, QueueId, _nextQueueStatsUpdate); |
76 | 84 | try |
77 | 85 | { |
78 | 86 | using var _ = FoundatioDiagnostics.ActivitySource.StartActivity("Queue Stats: " + _options.Name); |
79 | 87 | _queueStats = GetMetricsQueueStats(); |
80 | 88 | return (_queueStats.Queued, _queueStats.Working, _queueStats.Deadletter); |
81 | 89 | } |
82 | | - catch |
| 90 | + catch (Exception ex) |
83 | 91 | { |
| 92 | + _logger.LogError(ex, "Error getting queue metrics for {QueueName} ({QueueId}): {Message}", _options.Name, QueueId, ex.Message); |
84 | 93 | return (0, 0, 0); |
85 | 94 | } |
86 | 95 | }, _logger); |
@@ -157,13 +166,14 @@ public async Task<IEnumerable<T>> GetDeadletterItemsAsync(CancellationToken canc |
157 | 166 |
|
158 | 167 | public async Task<QueueStats> GetQueueStatsAsync() |
159 | 168 | { |
160 | | - _queueStats = await GetQueueStatsImplAsync(); |
| 169 | + _logger.LogTrace("Getting queue stats for {QueueName} ({QueueId})", _options.Name, QueueId); |
| 170 | + _queueStats = await GetQueueStatsImplAsync().AnyContext(); |
161 | 171 | return _queueStats; |
162 | 172 | } |
163 | 173 |
|
164 | 174 | protected virtual QueueStats GetMetricsQueueStats() |
165 | 175 | { |
166 | | - return GetQueueStatsAsync().GetAwaiter().GetResult(); |
| 176 | + return GetQueueStatsAsync().AnyContext().GetAwaiter().GetResult(); |
167 | 177 | } |
168 | 178 |
|
169 | 179 | public abstract Task DeleteQueueAsync(); |
|
0 commit comments