Beispiel #1
0
        static async Task Main()
        {
            Microsoft.Extensions.Logging.ILogger logger = InitLogger().CreateLogger("loadgen");
            Log.Information($"Starting load run with the following settings:\r\n{Settings.Current.ToString()}");

            try
            {
                var retryPolicy = new RetryPolicy(TimeoutErrorDetectionStrategy, TransientRetryStrategy);
                retryPolicy.Retrying += (_, args) =>
                {
                    Log.Error($"Creating ModuleClient failed with exception {args.LastException}");
                    if (args.CurrentRetryCount < RetryCount)
                    {
                        Log.Information("Retrying...");
                    }
                };
                ModuleClient client = await retryPolicy.ExecuteAsync(() => InitModuleClient(Settings.Current.TransportType));

                using (var timers = new Timers())
                {
                    var    random     = new Random();
                    SHA256 sha        = SHA256Managed.Create();
                    var    bufferPool = new BufferPool();

                    // setup the message timer
                    timers.Add(
                        Settings.Current.MessageFrequency,
                        Settings.Current.JitterFactor,
                        () => GenMessage(client, random, sha, bufferPool));

                    // setup the twin update timer
                    timers.Add(
                        Settings.Current.TwinUpdateFrequency,
                        Settings.Current.JitterFactor,
                        () => GenTwinUpdate(client));
                    timers.Start();

                    (
                        CancellationTokenSource cts,
                        ManualResetEventSlim completed,
                        Option <object> handler
                    ) = ShutdownHandler.Init(TimeSpan.FromSeconds(5), logger);

                    Log.Information("Load gen running.");

                    await cts.Token.WhenCanceled();

                    Log.Information("Stopping timers.");
                    timers.Stop();
                    Log.Information("Closing connection to Edge Hub.");
                    await client.CloseAsync();

                    completed.Set();
                    handler.ForEach(h => GC.KeepAlive(h));

                    Log.Information("Load run complete. Exiting.");
                }
            }
            catch (Exception ex)
            {
                Log.Error($"Error occurred during load run. \r\n{ex.ToString()}");
            }
        }