-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathProgram.cs
160 lines (139 loc) · 7.04 KB
/
Program.cs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using CommandLine;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Fluent;
using Microsoft.Extensions.Configuration;
namespace episode2
{
class Program
{
static async Task Main(string[] args)
{
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
CosmosClient client = new CosmosClientBuilder(configuration.GetConnectionString("Cosmos"))
.WithApplicationName("OnDotNetRocks")
.Build();
await Program.InitializeContainersAsync(client);
// Parse command line arguments
ParserResult<CommandLineOptions> result = Parser.Default.ParseArguments<CommandLineOptions>(args);
try
{
await result.MapResult(async options => await Program.ParseOptionsAndRunAsync(options, client), _ => Task.CompletedTask);
Console.WriteLine("Job's done.");
}
catch(ArgumentException exception)
{
Console.WriteLine(exception.Message);
Console.WriteLine("Options: --processor \"<name>\": Starts a processor instance with a particular name.");
Console.WriteLine(" --estimator: Starts an estimator to watch for changes.");
Console.WriteLine(" --writer <number>: Starts a writer for a fixed number of documents.");
}
}
private static Task ParseOptionsAndRunAsync(CommandLineOptions options, CosmosClient client)
{
Program.ValidateArguments(options);
if (!string.IsNullOrEmpty(options.Processor))
{
return Program.StartProcessorAsync(client, options.Processor);
}
if (options.DocumentWriter > 0)
{
return Program.StartWriterAsync(client, options.DocumentWriter);
}
return Program.StartEstimatorAsync(client);
}
private static async Task StartProcessorAsync(CosmosClient client, string instanceName)
{
Container container = client.GetContainer("OnDotNet", "changefeed-items");
Container leasesContainer = client.GetContainer("OnDotNet", "changefeed-leases");
ChangeFeedProcessor processor = container.GetChangeFeedProcessorBuilder<Model>("OnDotNet", Program.ProcessChangesAsync)
.WithInstanceName(instanceName)
.WithLeaseContainer(leasesContainer)
.Build();
await processor.StartAsync();
Console.WriteLine($"Instance {instanceName} started.");
Console.WriteLine("Press any key to stop.");
Console.ReadKey();
await processor.StopAsync();
Console.WriteLine($"Instance {instanceName} stopped.");
}
private static Task ProcessChangesAsync(IReadOnlyCollection<Model> changes, CancellationToken cancellationToken)
{
ConsoleColor previousColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
foreach(Model item in changes)
{
Console.WriteLine(item.ToString());
}
Console.ForegroundColor = previousColor;
return Task.CompletedTask;
}
private static async Task StartEstimatorAsync(CosmosClient client)
{
Container container = client.GetContainer("OnDotNet", "changefeed-items");
Container leasesContainer = client.GetContainer("OnDotNet", "changefeed-leases");
ChangeFeedProcessor processor = container.GetChangeFeedEstimatorBuilder("OnDotNet", Program.ShowEstimation, TimeSpan.FromSeconds(1))
.WithLeaseContainer(leasesContainer)
.Build();
await processor.StartAsync();
Console.WriteLine($"Estimator started.");
Console.WriteLine("Press any key to stop.");
Console.ReadKey();
await processor.StopAsync();
Console.WriteLine($"Estimator stopped.");
}
private static Task ShowEstimation(long pendingChanges, CancellationToken cancellationToken)
{
ConsoleColor previousColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"{pendingChanges} changes to be read.");
Console.ForegroundColor = previousColor;
return Task.CompletedTask;
}
private static Task StartWriterAsync(CosmosClient client, int documentsToWrite)
{
List<Task> tasks = new List<Task>();
Container container = client.GetContainer("OnDotNet", "changefeed-items");
Console.WriteLine($"Generating {documentsToWrite} items to insert...");
foreach(Model item in new Bogus.Faker<Model>()
.StrictMode(true)
.RuleFor(o => o.id, f => Guid.NewGuid().ToString())
.RuleFor(o => o.partitionKey, f => Guid.NewGuid().ToString())
.RuleFor(o => o.userName, f => f.Internet.UserName())
.RuleFor(o => o.email, f => f.Internet.Email())
.RuleFor(o => o.age, f => f.Random.Number(20, 80))
.RuleFor(o => o.favLanguage, f => (Language)f.Random.Number(0, 9))
.Generate(documentsToWrite))
{
tasks.Add(container.CreateItemAsync(item));
}
return Task.WhenAll(tasks);
}
private static async Task InitializeContainersAsync(CosmosClient cosmosClient)
{
Database database = await cosmosClient.CreateDatabaseIfNotExistsAsync("OnDotNet");
await database.CreateContainerIfNotExistsAsync(id: "changefeed-items", partitionKeyPath: "/partitionKey", throughput: 10000);
await database.CreateContainerIfNotExistsAsync(id: "changefeed-leases", partitionKeyPath: "/id");
}
private static void ValidateArguments(CommandLineOptions options)
{
bool validArguments = true;
validArguments |= (options.Estimator && !string.IsNullOrEmpty(options.Processor));
validArguments |= (options.Estimator && options.DocumentWriter > 0);
validArguments |= (options.DocumentWriter > 0 && !string.IsNullOrEmpty(options.Processor));
if (!validArguments)
{
throw new ArgumentException("Only one argument mode is allowed.");
}
if (!options.Estimator && string.IsNullOrEmpty(options.Processor) && options.DocumentWriter == 0)
{
throw new ArgumentException("At least one argument mode should be set.");
}
}
}
}