Kavita/Kavita.Services/HostedServices/ReadingSessionInitializer.cs
Fesaa c62b20f54b
BE Tech Debt (#4497)
Co-authored-by: Joseph Milazzo <joseph.v.milazzo@gmail.com>
Co-authored-by: Joe Milazzo <josephmajora@gmail.com>
2026-03-07 10:04:08 -08:00

47 lines
1.7 KiB
C#

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Kavita.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Kavita.Services.HostedServices;
public class ReadingSessionInitializer : IHostedService
{
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly ILogger<ReadingSessionInitializer> _logger;
public ReadingSessionInitializer(IServiceScopeFactory serviceScopeFactory,
ILogger<ReadingSessionInitializer> logger)
{
_serviceScopeFactory = serviceScopeFactory;
_logger = logger;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Closing any orphaned reading sessions from previous run");
using var scope = _serviceScopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<DataContext>();
var closedCount = await context.AppUserReadingSession
.Where(s => s.IsActive)
.ExecuteUpdateAsync(s => s
.SetProperty(x => x.IsActive, false)
.SetProperty(x => x.EndTime, x => x.LastModified)
.SetProperty(x => x.EndTimeUtc, x => x.LastModifiedUtc)
.SetProperty(x => x.LastModified, DateTime.Now)
.SetProperty(x => x.LastModifiedUtc, DateTime.UtcNow),
cancellationToken);
_logger.LogInformation("Closed {Count} orphaned reading sessions", closedCount);
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}