mirror of
https://github.com/logos-storage/logos-storage-nim-cs-dist-tests.git
synced 2026-01-08 16:33:07 +00:00
block and lock for loading of discord users
This commit is contained in:
parent
16b5ee4fd1
commit
abdc7a0ab1
@ -25,7 +25,7 @@ namespace BiblioTech.Rewards
|
||||
|
||||
public async Task RunRoleGiver(Func<IRoleGiver, Task> action)
|
||||
{
|
||||
var context = await OpenRoleModifyContext();
|
||||
var context = OpenRoleModifyContext();
|
||||
var mapper = new RoleMapper(context);
|
||||
await action(mapper);
|
||||
}
|
||||
@ -37,7 +37,7 @@ namespace BiblioTech.Rewards
|
||||
|
||||
public async Task IterateUsersWithRoles(Func<IRoleGiver, IUser, ulong, Task> onUserWithRole, Func<IRoleGiver, Task> whenDone, params ulong[] rolesToIterate)
|
||||
{
|
||||
var context = await OpenRoleModifyContext();
|
||||
var context = OpenRoleModifyContext();
|
||||
var mapper = new RoleMapper(context);
|
||||
foreach (var user in context.Users)
|
||||
{
|
||||
@ -52,10 +52,10 @@ namespace BiblioTech.Rewards
|
||||
await whenDone(mapper);
|
||||
}
|
||||
|
||||
private async Task<RoleModifyContext> OpenRoleModifyContext()
|
||||
private RoleModifyContext OpenRoleModifyContext()
|
||||
{
|
||||
var context = new RoleModifyContext(GetGuild(), userRepo, log, rewardsChannel);
|
||||
await context.Initialize();
|
||||
context.Initialize();
|
||||
return context;
|
||||
}
|
||||
|
||||
|
||||
@ -11,6 +11,7 @@ namespace BiblioTech.Rewards
|
||||
private Dictionary<ulong, IGuildUser> users = new();
|
||||
private Dictionary<ulong, SocketRole> roles = new();
|
||||
private DateTime lastLoad = DateTime.MinValue;
|
||||
private readonly object _lock = new object();
|
||||
|
||||
private readonly SocketGuild guild;
|
||||
private readonly UserRepo userRepo;
|
||||
@ -25,15 +26,20 @@ namespace BiblioTech.Rewards
|
||||
this.rewardsChannel = rewardsChannel;
|
||||
}
|
||||
|
||||
public async Task Initialize()
|
||||
public void Initialize()
|
||||
{
|
||||
var span = DateTime.UtcNow - lastLoad;
|
||||
if (span > TimeSpan.FromMinutes(10))
|
||||
lock (_lock)
|
||||
{
|
||||
lastLoad = DateTime.UtcNow;
|
||||
log.Log("Loading all users and roles...");
|
||||
this.users = await LoadAllUsers(guild);
|
||||
this.roles = LoadAllRoles(guild);
|
||||
var span = DateTime.UtcNow - lastLoad;
|
||||
if (span > TimeSpan.FromMinutes(10))
|
||||
{
|
||||
lastLoad = DateTime.UtcNow;
|
||||
log.Log("Loading all users and roles...");
|
||||
var task = LoadAllUsers(guild);
|
||||
task.Wait();
|
||||
this.users = task.Result;
|
||||
this.roles = LoadAllRoles(guild);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user