-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
66 lines (59 loc) · 2.54 KB
/
Program.cs
File metadata and controls
66 lines (59 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Azure.Messaging.EventHubs;
using McMaster.Extensions.CommandLineUtils;
using System;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace watch_event_hubs
{
[Command(Description = "Dump messages from event hubs")]
internal class Program
{
public static int Main(string[] args) => CommandLineApplication.Execute<Program>(args);
[Argument(0, Description = "The connection string for the event hub")]
[Required]
public string ConnectionString { get; } = default!;
[Option(CommandOptionType.NoValue, Description = "Get events from the start")]
public bool FromStart { get; }
public string ConsumerGroup { get; } = "$default";
public async Task<int> OnExecute()
{
using var cts = new CancellationTokenSource();
Console.CancelKeyPress += (o, e) =>
{
Console.WriteLine("Shutting down...");
cts.Cancel();
};
var ct = cts.Token;
var tcs = new TaskCompletionSource<bool>();
await using var client = new EventHubClient(ConnectionString);
var partitions = await client.GetPartitionIdsAsync(ct);
Console.WriteLine($"Got {partitions.Length} partitions");
var tasks = new Task[partitions.Length];
for (var i = 0; i < partitions.Length; i++)
{
tasks[i] = RunForPartition(partitions[i], client, ct);
}
await Task.WhenAll(tasks);
return 0;
}
private async Task RunForPartition(string partitionId, EventHubClient client, CancellationToken ct)
{
Console.WriteLine($"Running for partition {partitionId} {(FromStart ? "from start" : "")}");
try
{
await using var consumer = client.CreateConsumer(ConsumerGroup, partitionId, FromStart ? EventPosition.Earliest : EventPosition.Latest);
await foreach (var message in consumer.SubscribeToEvents(ct))
{
var json = Encoding.UTF8.GetString(message.Body.Span);
Console.WriteLine($"{message.EnqueuedTime}: {message.Offset} ({message.SequenceNumber})\n{json}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error processing partition {partitionId}: {ex.Message}");
}
}
}
}