Skip to content

Commit

Permalink
Retry to fetch usernames when they failed to fetch before
Browse files Browse the repository at this point in the history
  • Loading branch information
Simyon264 committed May 23, 2024
1 parent d2723eb commit 421edbe
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions ReplayBrowser/Services/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,47 @@ public AccountService(IMemoryCache cache, IServiceScopeFactory scopeFactory, Ss1
public Task StartAsync(CancellationToken cancellationToken)
{
GenerateAccountSettings();
_timer = new Timer(CheckDuplicateAccounts, null, TimeSpan.Zero, TimeSpan.FromMinutes(1));
_timer = new Timer(CheckAccounts, null, TimeSpan.Zero, TimeSpan.FromMinutes(1));
return Task.CompletedTask;
}

private async void CheckAccounts(object? state)
{
using var scope = _scopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ReplayDbContext>();

CheckDuplicate(context);
await CheckApiErrorName(context);

await context.SaveChangesAsync();
}

/// <summary>
/// This method checks for accounts with the name "Unable to fetch username (API error)" tries to fetch the username again.
/// </summary>
private async Task CheckApiErrorName(ReplayDbContext context)
{
var accounts = context.Accounts
.Include(a => a.Settings)
.Include(a => a.History)
.ToList();

foreach (var account in accounts)
{
if (account.Username == "Unable to fetch username (API error)")
{
Log.Warning($"Account {account.Guid} has an error name. Trying to fetch username again.");
var playerData = await _apiHelper.FetchPlayerDataFromGuid(account.Guid);
account.Username = playerData?.Username ?? "Unable to fetch username (API error)";
}
}
}

/// <summary>
/// This method checks for duplicate accounts in the database and removes them. Why are there duplicates? I'm not sure. I fucked up *somewhere*
/// </summary>
private void CheckDuplicateAccounts(object? state)
private void CheckDuplicate(ReplayDbContext context)
{
using var scope = _scopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ReplayDbContext>();
var accounts = context.Accounts
.Include(a => a.Settings)
.Include(a => a.History)
Expand All @@ -57,8 +87,6 @@ private void CheckDuplicateAccounts(object? state)
Log.Warning($"Duplicate account found: {accountToRemove.Username} ({accountToRemove.Guid})");
context.Accounts.Remove(accountToRemove);
}

context.SaveChanges();
}

public Task StopAsync(CancellationToken cancellationToken)
Expand Down

0 comments on commit 421edbe

Please sign in to comment.