🐛 Session auto renew
This commit is contained in:
@@ -18,7 +18,7 @@ public class LastActiveFlushHandler(IServiceProvider srp, ILogger<LastActiveFlus
|
|||||||
public async Task FlushAsync(IReadOnlyList<LastActiveInfo> items)
|
public async Task FlushAsync(IReadOnlyList<LastActiveInfo> items)
|
||||||
{
|
{
|
||||||
logger.LogInformation("Flushing {Count} LastActiveInfo items...", items.Count);
|
logger.LogInformation("Flushing {Count} LastActiveInfo items...", items.Count);
|
||||||
|
|
||||||
using var scope = srp.CreateScope();
|
using var scope = srp.CreateScope();
|
||||||
var db = scope.ServiceProvider.GetRequiredService<AppDatabase>();
|
var db = scope.ServiceProvider.GetRequiredService<AppDatabase>();
|
||||||
|
|
||||||
@@ -38,13 +38,22 @@ public class LastActiveFlushHandler(IServiceProvider srp, ILogger<LastActiveFlus
|
|||||||
.ToDictionary(g => g.Key, g => g.Last().SeenAt);
|
.ToDictionary(g => g.Key, g => g.Last().SeenAt);
|
||||||
|
|
||||||
var now = SystemClock.Instance.GetCurrentInstant();
|
var now = SystemClock.Instance.GetCurrentInstant();
|
||||||
|
|
||||||
var updatingSessions = sessionMap.Select(x => x.Key).ToList();
|
var updatingSessions = sessionMap.Select(x => x.Key).ToList();
|
||||||
var sessionUpdates = await db.AuthSessions
|
var sessionUpdates = await db.AuthSessions
|
||||||
.Where(s => updatingSessions.Contains(s.Id))
|
.Where(s => updatingSessions.Contains(s.Id))
|
||||||
.ExecuteUpdateAsync(s => s.SetProperty(x => x.LastGrantedAt, now));
|
.ExecuteUpdateAsync(s =>
|
||||||
|
s.SetProperty(x => x.LastGrantedAt, now)
|
||||||
|
);
|
||||||
logger.LogInformation("Updated {Count} auth sessions according to LastActiveInfo", sessionUpdates);
|
logger.LogInformation("Updated {Count} auth sessions according to LastActiveInfo", sessionUpdates);
|
||||||
|
var newExpiration = now.Plus(Duration.FromDays(7));
|
||||||
|
var keepAliveSessionUpdates = await db.AuthSessions
|
||||||
|
.Where(s => updatingSessions.Contains(s.Id) && s.ExpiredAt != null)
|
||||||
|
.ExecuteUpdateAsync(s =>
|
||||||
|
s.SetProperty(x => x.ExpiredAt, newExpiration)
|
||||||
|
);
|
||||||
|
logger.LogInformation("Updated {Count} auth sessions' duration according to LastActiveInfo", sessionUpdates);
|
||||||
|
|
||||||
var updatingAccounts = accountMap.Select(x => x.Key).ToList();
|
var updatingAccounts = accountMap.Select(x => x.Key).ToList();
|
||||||
var profileUpdates = await db.AccountProfiles
|
var profileUpdates = await db.AccountProfiles
|
||||||
.Where(a => updatingAccounts.Contains(a.AccountId))
|
.Where(a => updatingAccounts.Contains(a.AccountId))
|
||||||
@@ -53,7 +62,8 @@ public class LastActiveFlushHandler(IServiceProvider srp, ILogger<LastActiveFlus
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class LastActiveFlushJob(FlushBufferService fbs, LastActiveFlushHandler hdl, ILogger<LastActiveFlushJob> logger) : IJob
|
public class LastActiveFlushJob(FlushBufferService fbs, LastActiveFlushHandler hdl, ILogger<LastActiveFlushJob> logger)
|
||||||
|
: IJob
|
||||||
{
|
{
|
||||||
public async Task Execute(IJobExecutionContext context)
|
public async Task Execute(IJobExecutionContext context)
|
||||||
{
|
{
|
||||||
@@ -62,7 +72,8 @@ public class LastActiveFlushJob(FlushBufferService fbs, LastActiveFlushHandler h
|
|||||||
logger.LogInformation("Running LastActiveInfo flush job...");
|
logger.LogInformation("Running LastActiveInfo flush job...");
|
||||||
await fbs.FlushAsync(hdl);
|
await fbs.FlushAsync(hdl);
|
||||||
logger.LogInformation("Completed LastActiveInfo flush job...");
|
logger.LogInformation("Completed LastActiveInfo flush job...");
|
||||||
} catch (Exception ex)
|
}
|
||||||
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
logger.LogError(ex, "Error running LastActiveInfo job...");
|
logger.LogError(ex, "Error running LastActiveInfo job...");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user