public async Task <ActionResult <PoeLeagueModel> > GetDefaultLeague(string leagueName) { if (leagueName == DEFAULT_LEAGUE_NAME) { var defaultLeague = await leagueService.GetDefaultLeague(); if (defaultLeague == null) { return(NotFound()); } else { return(defaultLeague); } } else { var league = await PoeContext.Leagues.Where(x => x.LeagueId == leagueName).FirstOrDefaultAsync(); if (league == null) { return(NotFound()); } else { return(league); } } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation($"Is starting."); // web server start first Thread.Yield(); var taskCancelSources = new Dictionary <Task, CancellationTokenSource> { }; stoppingToken.Register(() => { _logger.LogInformation($"Background task is stopping."); _logger.LogInformation("Stopping children background tasks."); foreach (var canlSource in taskCancelSources.Values) { canlSource.Cancel(); } }); try { async Task fetchAndUpsertLeaguesLoop(CancellationToken stoppingToken, PoeLeagueModel waitLeague = null) { stoppingToken.Register(() => { _logger.LogInformation("Stopping fetchAndUpsertLeaguesLoop task"); }); while (!stoppingToken.IsCancellationRequested) { int defaultDelayTime = 60000 * 60 * 2; // 2hr if (waitLeague == null) { await Task.Delay(defaultDelayTime, stoppingToken); } else { if (waitLeague.EndAt == null) { await Task.Delay(defaultDelayTime, stoppingToken); } else { var now = System.DateTime.UtcNow; if (waitLeague.EndAt.Value > now) { System.TimeSpan delayTimeSpan = waitLeague.EndAt.Value.Subtract(System.DateTime.UtcNow); await Task.Delay(delayTimeSpan, stoppingToken); } else { await Task.Delay(defaultDelayTime, stoppingToken); } } } await FetchAndUpsertLeagues(); } } await FetchAndUpsertLeagues(); async Task fetchAndUpsertCharactersLoop(CancellationToken stoppingToken, string targetLeagueName) { int defaultDelayTime = 60000 * 10; // 10 min stoppingToken.Register(() => { _logger.LogInformation($"Stopping {targetLeagueName} fetchAndUpsertCharactersLoop task"); }); while (!stoppingToken.IsCancellationRequested) { await FetchAndUpsertCharacters(stoppingToken, targetLeagueName); await Task.Delay(defaultDelayTime, stoppingToken); } } var tasks = new List <Task> { }; PoeLeagueModel defaultLeague = await leagueService.GetDefaultLeague(); if (defaultLeague != null) { var leagueCancelSource = new CancellationTokenSource(); Task leaguesLoop = fetchAndUpsertLeaguesLoop(leagueCancelSource.Token, defaultLeague); tasks.Add(leaguesLoop); taskCancelSources.Add(leaguesLoop, leagueCancelSource); var characterCancelSource = new CancellationTokenSource(); Task charactersLoop = fetchAndUpsertCharactersLoop(characterCancelSource.Token, defaultLeague.LeagueId); tasks.Add(charactersLoop); taskCancelSources.Add(charactersLoop, characterCancelSource); } else { _logger.LogError("Not found default league!!!"); } // you can add other leagues await Task.WhenAll(tasks.ToArray()); } catch (System.Exception e) { _logger.LogError(e.ToString()); } finally { foreach (var canlSource in taskCancelSources.Values) { canlSource.Dispose(); } } }