mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-24 09:24:12 +00:00
Use background service semaphore
Must have missed it the first time...
This commit is contained in:
parent
a9853021f4
commit
e62a89d0a0
1 changed files with 16 additions and 7 deletions
|
@ -1,7 +1,7 @@
|
|||
using BirthdayBot.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace BirthdayBot.BackgroundServices;
|
||||
|
||||
/// <summary>
|
||||
/// Proactively fills the user cache for guilds in which any birthday data already exists.
|
||||
/// </summary>
|
||||
|
@ -9,14 +9,23 @@ class AutoUserDownload : BackgroundService {
|
|||
public AutoUserDownload(ShardInstance instance) : base(instance) { }
|
||||
|
||||
public override async Task OnTick(int tickCount, CancellationToken token) {
|
||||
using var db = new BotDatabaseContext();
|
||||
|
||||
// Take action if a guild's cache is incomplete...
|
||||
var incompleteCaches = ShardInstance.DiscordClient.Guilds.Where(g => !g.HasAllMembers).Select(g => (long)g.Id).ToHashSet();
|
||||
// ...and if the guild contains any user data
|
||||
var mustFetch = db.UserEntries.Where(e => incompleteCaches.Contains(e.GuildId)).Select(e => e.GuildId).Distinct();
|
||||
IEnumerable<long> mustFetch;
|
||||
try {
|
||||
await DbConcurrentOperationsLock.WaitAsync(token);
|
||||
using var db = new BotDatabaseContext();
|
||||
mustFetch = db.UserEntries.AsNoTracking()
|
||||
.Where(e => incompleteCaches.Contains(e.GuildId)).Select(e => e.GuildId).Distinct()
|
||||
.ToList();
|
||||
} finally {
|
||||
try {
|
||||
DbConcurrentOperationsLock.Release();
|
||||
} catch (ObjectDisposedException) { }
|
||||
}
|
||||
|
||||
int processed = 0;
|
||||
var processed = 0;
|
||||
foreach (var item in mustFetch) {
|
||||
// May cause a disconnect in certain situations. Cancel all further attempts until the next pass if it happens.
|
||||
if (ShardInstance.DiscordClient.ConnectionState != ConnectionState.Connected) break;
|
||||
|
@ -28,6 +37,6 @@ class AutoUserDownload : BackgroundService {
|
|||
processed++;
|
||||
}
|
||||
|
||||
if (processed > 100) Log($"Explicit user list request processed for {processed} guild(s).");
|
||||
if (processed > 25) Log($"Explicit user list request processed for {processed} guild(s).");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue