public async Task StartAsync(CancellationToken cancellationToken) { _logger.LogInformation("Background cache updater is starting"); await _apiClient.LoginAsync(); RunTaskLoop(_cancellationTokenSource.Token); _logger.LogInformation("Background cache updater started"); }
public async Task RunSeedingAsync(CancellationToken cancellationToken) { _logger.LogInformation("Start seeding data of 14 days"); if (_apiSvc.CurrentIdentity == null) { var id = await _apiSvc.LoginAsync(); if (id == null) { _logger.LogError("MW failed to log in"); return; } } var end = DateTimeOffset.UtcNow; var b = end.Subtract(TimeSpan.FromDays(14)); int retryCount = 0; do { try { var tsSamples = new List <ContribSampleEntity>(); var s = b.AddHours(1); var changes = await _apiSvc.GetRecentChangesSinceAsync(b, 0, s); _logger.LogInformation($"Get changes from {b} to {s}: {changes.Count} items"); var userGrouping = changes.Where(g => g.User != null).GroupBy(g => g.User); foreach (var g in userGrouping) { var count = g.Count(); tsSamples.Add(new ContribSampleEntity(g.Key, b, count)); } await _tsdb.IngestSamplesAsync(tsSamples, cancellationToken); b = s; retryCount = 0; } catch (Exception exc) { _logger.LogError(exc, "Caught an error and enter retry."); if (retryCount > 3) { break; } else { retryCount++; } } }while (b < end && !cancellationToken.IsCancellationRequested); await _tsdb.FlushCacheAsync(cancellationToken); _logger.LogInformation("Seeding completed"); }
private async Task <IPrincipal> RefreshSessionAsync() { var principal = await _apiSvc.LoginAsync(); if (principal != null) { _logger.LogInformation($"MW logged in as {principal.Identity.Name}"); _lastLogin = DateTimeOffset.UtcNow; } return(principal); }