private async Task RunAsync() { _logger.Verbose("Running {ClassName}", nameof(PlayLogger)); bool error = false; try { _logger.Information($"Beginning update loop...{Environment.NewLine}"); while (!(_cancellationToken.IsCancellationRequested || error)) { List <BoardGame> boardGames = await _mongoSvc.GetBoardListAsync(_cancellationToken); BoardGame lastGame = boardGames.Last(); _logger.Information("Will log plays for {GameCount} games:", boardGames.Count); boardGames.ForEach( bg => _logger.Information( $"\t- {{GameName}} - {{GameId}}{(bg == lastGame? Environment.NewLine : String.Empty)}", bg.Name, bg.ObjectId)); foreach (BoardGame bg in boardGames) { try { if (!_cancellationToken.IsCancellationRequested) { _logger.Information("Starting play logging for {GameName}", bg.Name); BoardGameStatus status = await _mongoSvc.GetBoardGameStatusAsync(bg.ObjectId, _cancellationToken); if (status?.ImportSuccessful == true) { await GetMostRecentPlaysAsync(bg, status); } else { await GetAllPlaysAsync(bg); } _logger.Information($"Completed updating plays for {{GameName}}{Environment.NewLine}", bg.Name); } else { _logger.Information("Skipping {GameName}, service is shutting down", bg.Name); } } catch (Exception ex) { _logger.Error(ex, "Failed to get plays for {GameName}", bg.Name); error = true; } } if (!_cancellationToken.IsCancellationRequested) { _logger.Information("Finished processing list, will update in {Minutes} minutes", _config.UpdateDelayInMinutes); await Task .Delay(TimeSpan.FromMinutes(_config.UpdateDelayInMinutes), _cancellationToken) .ContinueWith(t => { }, CancellationToken.None); } } } catch (Exception ex) { _logger.Error(ex, "Something went terribly wrong: {Message}", ex.Message); } }