in Hands-on lab/lab-files/TransactionGenerator/Program.cs [352:455]
public static async Task Main(string[] args)
{
// Setup configuration to either read from the appsettings.json file (if present) or environment variables.
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
_configuration = builder.Build();
var arguments = ParseArguments();
// Set an optional timeout for the generator.
var cancellationSource = arguments.MillisecondsToRun == 0 ? new CancellationTokenSource() : new CancellationTokenSource(arguments.MillisecondsToRun);
var cancellationToken = cancellationSource.Token;
var statistics = new Statistic[0];
// Set the Cosmos DB connection policy.
// TODO 3: Complete the code below to create a Cosmos DB connection policy that has a Direct Connection Mode.
// COMPLETE THIS CODE ... var clientOptions = new CosmosClientOptions...
var numberOfMillisecondsToLead = arguments.MillisecondsToLead;
var taskWaitTime = 0;
if (numberOfMillisecondsToLead > 0)
{
taskWaitTime = numberOfMillisecondsToLead;
}
var progress = new Progress<Progress>();
progress.ProgressChanged += (sender, progressArgs) =>
{
foreach (var message in progressArgs.Messages)
{
WriteLineInColor(message.Message, message.Color.ToConsoleColor());
}
statistics = progressArgs.Statistics;
};
WriteLineInColor("Payment Generator", ConsoleColor.White);
Console.WriteLine("======");
WriteLineInColor("Press Ctrl+C or Ctrl+Break to cancel.", ConsoleColor.Cyan);
Console.WriteLine("Statistics for generated payment data will updated for every 1000 sent");
Console.WriteLine(string.Empty);
ThreadPool.SetMinThreads(100, 100);
// Handle Control+C or Control+Break.
Console.CancelKeyPress += (o, e) =>
{
WriteLineInColor("Stopped generator. No more events are being sent.", ConsoleColor.Yellow);
cancellationSource.Cancel();
// Allow the main thread to continue and exit...
WaitHandle.Set();
OutputStatistics(statistics);
};
// Get records to insert:
var paymentRecords = GetTransactionData(Transaction.FromString);
// Instantiate Event Hub client(s):
var eventHubClients = new List<EventHubProducerClient>
{
// TODO 6: Create an Event Hub Client from a connection string, using the EventHubConnectionString value.
// TODO 8: Create additional Event Hub clients from remaining two connection strings.
};
// Instantiate Cosmos DB client and start sending messages to Event Hubs and Cosmos DB:
using (_cosmosDbClient = new CosmosClient(arguments.CosmosDbEndpointUrl,
arguments.CosmosDbAuthorizationKey, clientOptions))
{
await InitializeCosmosDb();
// Find and output the collection details, including # of RU/s.
var container = GetContainerIfExists(DatabaseName, CollectionName);
var throughputResponse = await container.ReadThroughputAsync(requestOptions: null, cancellationToken: cancellationToken);
if (throughputResponse != null)
{
WriteLineInColor($"Found container `{CollectionName}` with a current throughput of {throughputResponse.Resource.Throughput} RU/s with a max throughput of {throughputResponse.Resource.AutoscaleMaxThroughput} reads/second; {throughputResponse.Resource.AutoscaleMaxThroughput / 5} writes/second @ 1KB doc size", ConsoleColor.Green);
var estimatedCostPerMonth = 0.06 * throughputResponse.Resource.AutoscaleMaxThroughput;
var estimatedCostPerHour = estimatedCostPerMonth / (24 * 30);
WriteLineInColor($"The container will cost a maximum of ${estimatedCostPerHour:0.00} per hour (${estimatedCostPerMonth:0.00} per month (per write region))", ConsoleColor.Green);
}
// Start sending data to both Event Hubs and Cosmos DB.
await SendData(paymentRecords, eventHubClients, 100, taskWaitTime, cancellationToken, progress, arguments.OnlyWriteToCosmosDb);
}
cancellationSource.Cancel();
Console.WriteLine();
WriteLineInColor("Done sending generated transaction data", ConsoleColor.Cyan);
Console.WriteLine();
Console.WriteLine();
OutputStatistics(statistics);
// Keep the console open.
Console.ReadLine();
WaitHandle.WaitOne();
}