lampac/SISI/ModInit.cs
lampac-talks f843f04fd4 chore: initial commit 154.3
Signed-off-by: lampac-talks <lampac-talks@users.noreply.github.com>
2026-01-30 16:23:09 +03:00

48 lines
1.3 KiB
C#

using Microsoft.EntityFrameworkCore;
using Shared.Models.SQL;
using System.Threading;
namespace SISI
{
public class ModInit
{
private static Timer cleanupTimer;
public static void loaded()
{
Directory.CreateDirectory("wwwroot/bookmarks/img");
Directory.CreateDirectory("wwwroot/bookmarks/preview");
cleanupTimer = new Timer(_ => CleanupHistory(), null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(20));
}
static int _updatingDb = 0;
private static void CleanupHistory()
{
if (Interlocked.Exchange(ref _updatingDb, 1) == 1)
return;
try
{
var threshold = DateTime.UtcNow.AddDays(-AppInit.conf.sisi.history.days);
using (var sqlDb = new SisiContext())
{
sqlDb.historys
.AsNoTracking()
.Where(i => i.created < threshold)
.ExecuteDelete();
}
}
catch (Exception ex)
{
Console.WriteLine($"[SISI] Cleanup history failed: {ex.Message}");
}
finally
{
Volatile.Write(ref _updatingDb, 0);
}
}
}
}