♻️ Refactor the last active flush handler
This commit is contained in:
parent
ea599fb15b
commit
eb1c283971
@ -1,4 +1,3 @@
|
|||||||
using EFCore.BulkExtensions;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using NodaTime;
|
using NodaTime;
|
||||||
using Quartz;
|
using Quartz;
|
||||||
@ -34,29 +33,21 @@ public class LastActiveFlushHandler(IServiceProvider serviceProvider) : IFlushHa
|
|||||||
.GroupBy(x => x.Account.Id)
|
.GroupBy(x => x.Account.Id)
|
||||||
.ToDictionary(g => g.Key, g => g.Last().SeenAt);
|
.ToDictionary(g => g.Key, g => g.Last().SeenAt);
|
||||||
|
|
||||||
// Load all sessions that need to be updated in one batch
|
// Update sessions using native EF Core ExecuteUpdateAsync
|
||||||
var sessionsToUpdate = await db.AuthSessions
|
foreach (var kvp in sessionIdMap)
|
||||||
.Where(s => sessionIdMap.Keys.Contains(s.Id))
|
{
|
||||||
.ToListAsync();
|
await db.AuthSessions
|
||||||
|
.Where(s => s.Id == kvp.Key)
|
||||||
|
.ExecuteUpdateAsync(s => s.SetProperty(x => x.LastGrantedAt, kvp.Value));
|
||||||
|
}
|
||||||
|
|
||||||
// Update their LastGrantedAt
|
// Update account profiles using native EF Core ExecuteUpdateAsync
|
||||||
foreach (var session in sessionsToUpdate)
|
foreach (var kvp in accountIdMap)
|
||||||
session.LastGrantedAt = sessionIdMap[session.Id];
|
{
|
||||||
|
await db.AccountProfiles
|
||||||
// Bulk update sessions
|
.Where(a => a.AccountId == kvp.Key)
|
||||||
await db.BulkUpdateAsync(sessionsToUpdate);
|
.ExecuteUpdateAsync(a => a.SetProperty(x => x.LastSeenAt, kvp.Value));
|
||||||
|
}
|
||||||
// Similarly, load account profiles in one batch
|
|
||||||
var accountProfilesToUpdate = await db.AccountProfiles
|
|
||||||
.Where(a => accountIdMap.Keys.Contains(a.AccountId))
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
// Update their LastSeenAt
|
|
||||||
foreach (var profile in accountProfilesToUpdate)
|
|
||||||
profile.LastSeenAt = accountIdMap[profile.AccountId];
|
|
||||||
|
|
||||||
// Bulk update profiles
|
|
||||||
await db.BulkUpdateAsync(accountProfilesToUpdate);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user