public async Task RunAsync(CancellationToken cancellationToken) { try { if (_isRunning) { return; } _isRunning = true; _stopRequestedTokenSource = new CancellationTokenSource(); _clansClient.OnLog(this, new LogEventArgs(nameof(RunAsync), LogLevel.Information)); int id = int.MinValue; while (_stopRequestedTokenSource.IsCancellationRequested == false && cancellationToken.IsCancellationRequested == false) { if (!_clansClient.DownloadMembers) { await Task.Delay(Configuration.DelayBetweenTasks); continue; } using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); List <CachedClan> cachedClans = await dbContext.Clans .OrderBy(c => c.Id) .Where(c => c.Id > id && c.DownloadMembers) .Take(Configuration.ConcurrentClanDownloads) .AsNoTracking() .ToListAsync(_stopRequestedTokenSource.Token) .ConfigureAwait(false); id = cachedClans.Count < Configuration.ConcurrentClanDownloads ? int.MinValue : cachedClans.Max(c => c.Id); foreach (CachedClan cachedClan in cachedClans) { await MonitorMembersAsync(cachedClan); } await Task.Delay(Configuration.DelayBetweenTasks, _stopRequestedTokenSource.Token).ConfigureAwait(false); } _isRunning = false; } catch (Exception e) { _isRunning = false; if (_stopRequestedTokenSource.IsCancellationRequested) { return; } _clansClient.OnLog(this, new ExceptionEventArgs(nameof(RunAsync), e)); if (cancellationToken.IsCancellationRequested == false) { _ = RunAsync(cancellationToken); } } }
public async Task RunAsync(CancellationToken cancellationToken) { try { if (_isRunning) { return; } _isRunning = true; _stopRequestedTokenSource = new CancellationTokenSource(); _clansClient.OnLog(this, new LogEventArgs(nameof(RunAsync), LogLevel.Information)); while (_stopRequestedTokenSource.IsCancellationRequested == false && cancellationToken.IsCancellationRequested == false) { if (_clansClient.DownloadWarLog == false) { await Task.Delay(Configuration.DelayBetweenTasks, _stopRequestedTokenSource.Token).ConfigureAwait(false); continue; } using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); //var clanWarLogsWithLogStatus = await dbContext.ClanWarLogWithLogStatus // .Where(w => // w.IsWarLogPublic && // w.DownloadCurrentWar && // w.ServerExpiration < DateTime.UtcNow.AddSeconds(-3) && // w.LocalExpiration < DateTime.UtcNow) // .OrderBy(w => w.ServerExpiration) // .Take(1000) // .Select(l => l.Tag) // .ToListAsync() // .ConfigureAwait(false); List <CachedClanWarLog> cachedLogs = await( from l in dbContext.WarLogs join c in dbContext.Clans on l.Tag equals c.Tag where c.IsWarLogPublic == true && c.DownloadCurrentWar && l.ServerExpiration < DateTime.UtcNow.AddSeconds(-3) && l.LocalExpiration < DateTime.UtcNow orderby l.ServerExpiration select l) .Take(Configuration.ConcurrentWarLogDownloads) .ToListAsync() .ConfigureAwait(false); List <Task> tasks = new(); foreach (CachedClanWarLog cachedLog in cachedLogs) { tasks.Add(MonitorLogAsync(cachedLog)); } await Task.WhenAll(tasks).ConfigureAwait(false); await dbContext.SaveChangesAsync(_stopRequestedTokenSource.Token).ConfigureAwait(false); //for (int i = 0; i < clanWarLogsWithLogStatus.Count; i++) // await MonitorLogAsync(clanWarLogsWithLogStatus[i]); await Task.Delay(Configuration.DelayBetweenTasks, _stopRequestedTokenSource.Token).ConfigureAwait(false); } _isRunning = false; } catch (Exception e) { _isRunning = false; if (_stopRequestedTokenSource.IsCancellationRequested) { return; } _clansClient.OnLog(this, new ExceptionEventArgs(nameof(RunAsync), e)); if (cancellationToken.IsCancellationRequested == false) { _ = RunAsync(cancellationToken); } } }
public async Task RunAsync(CancellationToken cancellationToken) { try { if (_isRunning) { return; } _isRunning = true; _stopRequestedTokenSource = new CancellationTokenSource(); _clansClient.OnLog(this, new LogEventArgs(nameof(RunAsync), LogLevel.Information)); while (_stopRequestedTokenSource.IsCancellationRequested == false && cancellationToken.IsCancellationRequested == false) { if (_clansClient.DownloadCurrentWars == false) { await Task.Delay(Configuration.DelayBetweenTasks, _stopRequestedTokenSource.Token).ConfigureAwait(false); continue; } using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); //List<string> clanWarWithLogStatus = await dbContext.ClanWarWithLogStatus // .Where(w => // w.IsWarLogPublic == true && // w.DownloadCurrentWar && // w.ServerExpiration < DateTime.UtcNow.AddSeconds(-3) && // w.LocalExpiration < DateTime.UtcNow) // .OrderBy(w => w.ServerExpiration) // .Take(100) // .Select(l => l.Tag ) // .ToListAsync() // .ConfigureAwait(false); List <CachedClanWar> cachedClanWars = await( from cw in dbContext.ClanWars join c in dbContext.Clans on cw.Tag equals c.Tag into cw_c_join from c2 in cw_c_join.DefaultIfEmpty() where c2.IsWarLogPublic == true && c2.DownloadCurrentWar == true && cw.ServerExpiration < DateTime.UtcNow.AddSeconds(-3) && cw.LocalExpiration < DateTime.UtcNow orderby cw.ServerExpiration select cw) .Take(Configuration.ConcurrentClanWarDownloads) .ToListAsync(); List <Task> tasks = new(); foreach (CachedClanWar cachedClanWar in cachedClanWars) { tasks.Add(MonitorClanWarAsync(cachedClanWar)); } await Task.WhenAll(tasks).ConfigureAwait(false); await dbContext.SaveChangesAsync(_stopRequestedTokenSource.Token).ConfigureAwait(false); await Task.Delay(Configuration.DelayBetweenTasks, _stopRequestedTokenSource.Token).ConfigureAwait(false); } _isRunning = false; } catch (Exception e) { _isRunning = false; if (_stopRequestedTokenSource.IsCancellationRequested) { return; } _clansClient.OnLog(this, new ExceptionEventArgs(nameof(RunAsync), e)); if (cancellationToken.IsCancellationRequested == false) { _ = RunAsync(cancellationToken); } } }
public async Task RunAsync(CancellationToken cancellationToken) { try { if (_isRunning) { return; } _isRunning = true; _stopRequestedTokenSource = new CancellationTokenSource(); _clansClient.OnLog(this, new LogEventArgs(nameof(RunAsync), LogLevel.Information)); while (_stopRequestedTokenSource.IsCancellationRequested == false && cancellationToken.IsCancellationRequested == false) { using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); List <CachedClan> cachedClans = await dbContext.Clans .Where(w => w.ServerExpiration < DateTime.UtcNow.AddSeconds(-3) && w.LocalExpiration < DateTime.UtcNow) .OrderBy(w => w.ServerExpiration) .Take(Configuration.ConcurrentClanDownloads) .ToListAsync(_stopRequestedTokenSource.Token) .ConfigureAwait(false); List <Task> tasks = new(); foreach (CachedClan cachedClan in cachedClans) { tasks.Add(MonitorClanAsync(cachedClan)); } await Task.WhenAll(tasks).ConfigureAwait(false); await dbContext.SaveChangesAsync(_stopRequestedTokenSource.Token).ConfigureAwait(false); if (_deletedUnmonitoredPlayers < DateTime.UtcNow.AddMinutes(-10)) { _deletedUnmonitoredPlayers = DateTime.UtcNow; tasks = new List <Task> { DeleteUnmonitoredPlayersNotInAClan(), DeletePlayersInClansNotMonitored() }; await Task.WhenAll(tasks); } await Task.Delay(Configuration.DelayBetweenTasks, _stopRequestedTokenSource.Token).ConfigureAwait(false); } _isRunning = false; } catch (Exception e) { _isRunning = false; if (_stopRequestedTokenSource.IsCancellationRequested) { return; } _clansClient.OnLog(this, new ExceptionEventArgs(nameof(RunAsync), e)); if (cancellationToken.IsCancellationRequested == false) { _ = RunAsync(cancellationToken); } } }
public async Task RunAsync(CancellationToken cancellationToken) { try { if (_isRunning) { return; } _isRunning = true; _stopRequestedTokenSource = new CancellationTokenSource(); _clansClient.OnLog(this, new LogEventArgs(nameof(RunAsync), LogLevel.Information)); while (_stopRequestedTokenSource.IsCancellationRequested == false && cancellationToken.IsCancellationRequested == false) { if (_clansClient.DownloadCurrentWars == false && _clansClient.DownloadCwl == false) { await Task.Delay(Configuration.DelayBetweenTasks, _stopRequestedTokenSource.Token).ConfigureAwait(false); continue; } using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); var wars = await dbContext.Wars .Where(w => w.IsFinal == false && w.ServerExpiration < DateTime.UtcNow.AddSeconds(-3) && w.LocalExpiration < DateTime.UtcNow //&& //w.State < WarState.WarEnded && //todo is there an index on state? //w.EndTime.AddDays(8) > DateTime.UtcNow //todo does this require a new column and a new index? //todo w.StartTime < DateTime.UtcNow.AddHours(-1)) //todo preparation start time < now ) .OrderBy(w => w.ServerExpiration) .ToListAsync() .ConfigureAwait(false); for (int i = 0; i < wars.Count; i++) { await MonitorWarAsync(wars[i]).ConfigureAwait(false); } await dbContext.SaveChangesAsync(_stopRequestedTokenSource.Token).ConfigureAwait(false); await Task.Delay(Configuration.DelayBetweenTasks, _stopRequestedTokenSource.Token).ConfigureAwait(false); } _isRunning = false; } catch (Exception e) { _isRunning = false; if (_stopRequestedTokenSource.IsCancellationRequested) { return; } _clansClient.OnLog(this, new ExceptionEventArgs(nameof(RunAsync), e)); if (cancellationToken.IsCancellationRequested == false) { _ = RunAsync(cancellationToken); } } }
public async Task RunAsync(CancellationToken cancellationToken) { try { if (_isRunning) { return; } _isRunning = true; _stopRequestedTokenSource = new CancellationTokenSource(); _clansClient.OnLog(this, new LogEventArgs(nameof(RunAsync), LogLevel.Information)); int id = int.MinValue; while (_stopRequestedTokenSource.IsCancellationRequested == false && cancellationToken.IsCancellationRequested == false) { if (_clansClient.DownloadCwl == false) { await Task.Delay(Configuration.DelayBetweenTasks, _stopRequestedTokenSource.Token).ConfigureAwait(false); continue; } using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); //var cachedWarLogs = await dbContext.ClanWarLeagueGroupWithStatus // .Where(w => // w.DownloadCwl == true && // w.ServerExpiration < DateTime.UtcNow.AddSeconds(-3) && // w.LocalExpiration < DateTime.UtcNow) // .OrderBy(w => w.ServerExpiration) // .Take(10) // .ToListAsync() // .ConfigureAwait(false); List <CachedClanWarLeagueGroup> cachedGroup = await( from g in dbContext.Groups join c in dbContext.Clans on g.Tag equals c.Tag where c.DownloadCwl && g.ServerExpiration < DateTime.UtcNow.AddSeconds(-3) && g.LocalExpiration <DateTime.UtcNow && g.Id> id orderby g.Id select g) .Take(Configuration.ConcurrentCwlDownloads) .ToListAsync() .ConfigureAwait(false); id = cachedGroup.Count == Configuration.ConcurrentClanDownloads ? cachedGroup.Max(g => g.Id) : int.MinValue; List <Task> tasks = new(); foreach (CachedClanWarLeagueGroup group in cachedGroup) { tasks.Add(MonitorCwlAsync(group, dbContext)); } await Task.WhenAll(tasks).ConfigureAwait(false); await dbContext.SaveChangesAsync(_stopRequestedTokenSource.Token).ConfigureAwait(false); await Task.Delay(Configuration.DelayBetweenTasks, _stopRequestedTokenSource.Token).ConfigureAwait(false); } _isRunning = false; } catch (Exception e) { _isRunning = false; if (_stopRequestedTokenSource.IsCancellationRequested) { return; } _clansClient.OnLog(this, new ExceptionEventArgs(nameof(RunAsync), e)); if (cancellationToken.IsCancellationRequested == false) { _ = RunAsync(cancellationToken); } } }