|
1 | 1 | using Azure.Messaging.ServiceBus; |
2 | 2 | using servicebus_cli.Services; |
| 3 | +using System.Text.Json; |
3 | 4 |
|
4 | 5 | namespace servicebus_cli.Subjects.Deadletter.Actions; |
5 | 6 |
|
6 | 7 | public interface IDeadletterActions |
7 | 8 | { |
8 | 9 | Task Resend(List<string> args); |
9 | 10 | Task Purge(List<string> args); |
| 11 | + Task Peek(List<string> args); |
10 | 12 | } |
11 | 13 |
|
12 | 14 | public class DeadletterActions( |
@@ -107,6 +109,93 @@ await _consoleService.ProcessWorkloadWithStatusUpdates<ServiceBusReceivedMessage |
107 | 109 | resendMessagesWorkload); |
108 | 110 | } |
109 | 111 |
|
| 112 | + public async Task Peek(List<string> args) |
| 113 | + { |
| 114 | + var fullyQualifiedNamespace = ""; |
| 115 | + var entityPath = ""; |
| 116 | + var settingsFileContent = _fileService.GetConfigFileContent(); |
| 117 | + var savedNamespaces = _userSettingsService.Deserialize(settingsFileContent); |
| 118 | + |
| 119 | + switch (args.Count) |
| 120 | + { |
| 121 | + case 2: |
| 122 | + fullyQualifiedNamespace = args[0]; |
| 123 | + entityPath = args[1]; |
| 124 | + break; |
| 125 | + default: |
| 126 | + if (!savedNamespaces.FullyQualifiedNamespaces.Any()) |
| 127 | + { |
| 128 | + fullyQualifiedNamespace = await _consoleService.PromptFreeText( |
| 129 | + "Enter the [green]fully qualified namespace[/]:"); |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + fullyQualifiedNamespace = await _consoleService.PromptSelection( |
| 134 | + "Select a fully qualified namespace:", |
| 135 | + savedNamespaces.FullyQualifiedNamespaces); |
| 136 | + } |
| 137 | + |
| 138 | + _consoleService.WriteMarkup($"[grey]Selected fully qualified namespace: {fullyQualifiedNamespace}[/]"); |
| 139 | + |
| 140 | + var peekQueuesWorkload = async () => |
| 141 | + { |
| 142 | + return await _serviceBusService.GetInformationAboutAllQueues(fullyQualifiedNamespace).ConfigureAwait(false); |
| 143 | + }; |
| 144 | + |
| 145 | + var queues = await _consoleService.ProcessWorkloadWithSpinner( |
| 146 | + $"Fetching queues on {fullyQualifiedNamespace}...", |
| 147 | + peekQueuesWorkload); |
| 148 | + |
| 149 | + var selectedQueue = await _consoleService.PromptSelection( |
| 150 | + "Select a [green]queue[/]:", |
| 151 | + queues.Select(q => $"{q.QueueProperties.Name} ([green]{q.QueueRuntimeProperties.ActiveMessageCount}[/], [red]{q.QueueRuntimeProperties.DeadLetterMessageCount}[/], [blue]{q.QueueRuntimeProperties.ScheduledMessageCount}[/])").ToList(), |
| 152 | + enableSearch: true); |
| 153 | + |
| 154 | + entityPath = selectedQueue.Split(' ')[0]; |
| 155 | + |
| 156 | + _consoleService.WriteMarkup($"[grey]Selected queue: {entityPath}[/]"); |
| 157 | + |
| 158 | + break; |
| 159 | + } |
| 160 | + |
| 161 | + var peekWorkload = async () => |
| 162 | + { |
| 163 | + return await _serviceBusService.PeekDeadLetterMessages(fullyQualifiedNamespace, entityPath).ConfigureAwait(false); |
| 164 | + }; |
| 165 | + |
| 166 | + var messages = await _consoleService.ProcessWorkloadWithSpinner( |
| 167 | + $"Peeking deadletter messages on {entityPath}...", |
| 168 | + peekWorkload); |
| 169 | + |
| 170 | + if (messages.Count == 0) |
| 171 | + { |
| 172 | + _consoleService.WriteError($"No deadletter messages found in queue {entityPath}"); |
| 173 | + return; |
| 174 | + } |
| 175 | + |
| 176 | + var jsonMessages = messages.Select(m => new |
| 177 | + { |
| 178 | + m.MessageId, |
| 179 | + Body = m.Body?.ToString(), |
| 180 | + m.Subject, |
| 181 | + m.ContentType, |
| 182 | + m.CorrelationId, |
| 183 | + m.DeadLetterReason, |
| 184 | + m.DeadLetterErrorDescription, |
| 185 | + m.DeadLetterSource, |
| 186 | + m.EnqueuedTime, |
| 187 | + m.ExpiresAt, |
| 188 | + m.SequenceNumber, |
| 189 | + m.DeliveryCount, |
| 190 | + ApplicationProperties = m.ApplicationProperties.ToDictionary(kvp => kvp.Key, kvp => kvp.Value) |
| 191 | + }); |
| 192 | + |
| 193 | + var jsonOptions = new JsonSerializerOptions { WriteIndented = true }; |
| 194 | + var json = JsonSerializer.Serialize(jsonMessages, jsonOptions); |
| 195 | + |
| 196 | + _consoleService.WriteJson(json); |
| 197 | + } |
| 198 | + |
110 | 199 | public async Task Purge(List<string> args) |
111 | 200 | { |
112 | 201 | var fullyQualifiedNamespace = ""; |
|
0 commit comments