protected override Task ExecuteAsync(CancellationToken cancelToken) { _logger.Verbose("Begin."); Health.Set(HealthStatus.Healthy); try { PelotonService.ValidateConfig(_settings.Peloton); GarminUploader.ValidateConfig(_settings); Metrics.ValidateConfig(_config.Observability); Tracing.ValidateConfig(_config.Observability); } catch (Exception ex) { _logger.Fatal(ex, "Exception during config validation."); Health.Set(HealthStatus.Dead); Environment.Exit(-1); } return(RunAsync(cancelToken)); }
private async Task RunAsync(CancellationToken cancelToken) { int exitCode = 0; Statics.MetricPrefix = Constants.ConsoleAppName; Statics.TracingService = Constants.ConsoleAppName; using var metrics = Metrics.EnableMetricsServer(_config.Observability.Prometheus); using var metricsCollector = Metrics.EnableCollector(_config.Observability.Prometheus); using var tracing = Tracing.EnableTracing(_config.Observability.Jaeger); using var tracingSource = new ActivitySource("ROOT"); try { if (_settings.Peloton.NumWorkoutsToDownload <= 0) { Console.Write("How many workouts to grab? "); int num = Convert.ToInt32(Console.ReadLine()); _settings.Peloton.NumWorkoutsToDownload = num; } if (_settings.App.EnablePolling) { while (_settings.App.EnablePolling && !cancelToken.IsCancellationRequested) { var syncResult = await _syncService.SyncAsync(_settings.Peloton.NumWorkoutsToDownload); Health.Set(syncResult.SyncSuccess ? HealthStatus.Healthy : HealthStatus.UnHealthy); Log.Information("Sleeping for {@Seconds} seconds...", _settings.App.PollingIntervalSeconds); var now = DateTime.UtcNow; var nextRunTime = now.AddSeconds(_settings.App.PollingIntervalSeconds); NextSyncTime.Set(new DateTimeOffset(nextRunTime).ToUnixTimeSeconds()); Thread.Sleep(_settings.App.PollingIntervalSeconds * 1000); } } else { await _syncService.SyncAsync(_settings.Peloton.NumWorkoutsToDownload); } _logger.Information("Done."); } catch (Exception ex) { _logger.Fatal(ex, "Uncaught Exception"); Health.Set(HealthStatus.Dead); exitCode = -2; } finally { _logger.Verbose("Exit."); if (!_settings.App.CloseWindowOnFinish) { Console.ReadLine(); } Environment.Exit(exitCode); } }