75 lines
No EOL
2.8 KiB
C#
75 lines
No EOL
2.8 KiB
C#
using System.Text;
|
|
using LarpixServer.Filesystem;
|
|
|
|
namespace LarpixServer.Account;
|
|
|
|
public class BackgroundDelete : BackgroundService
|
|
{
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
{
|
|
Console.WriteLine("Running Background Delete");
|
|
await ScanAndDelete(stoppingToken);
|
|
|
|
await Task.Delay(TimeSpan.FromDays(0.5), stoppingToken);
|
|
}
|
|
Console.WriteLine("Stopped Background Delete");
|
|
}
|
|
|
|
private async Task ScanAndDelete(CancellationToken stoppingToken)
|
|
{
|
|
try
|
|
{
|
|
if (!Fs.Exists(LarpixServer.Utils.Utils.ACCOUNTS_DATA_DIR)) return;
|
|
foreach (var accountName in Fs.ReadDirectory(LarpixServer.Utils.Utils.ACCOUNTS_DATA_DIR))
|
|
{
|
|
string account = Path.Combine(LarpixServer.Utils.Utils.ACCOUNTS_DATA_DIR, accountName);
|
|
if (stoppingToken.IsCancellationRequested) return;
|
|
try
|
|
{
|
|
string lpath = Path.Combine(account, "lastlogin");
|
|
if (!Fs.Exists(lpath))
|
|
{
|
|
continue;
|
|
}
|
|
DateTimeOffset lastLogin =
|
|
DateTimeOffset.FromUnixTimeSeconds(
|
|
long.Parse(Encoding.UTF8.GetString(await Fs.ReadFile(lpath))));
|
|
if (lastLogin < DateTimeOffset.UtcNow.AddMonths(-12))
|
|
{
|
|
string id = Path.GetFileName(account);
|
|
SemaphoreSlim userLock = Utils.GetUserLock(id);
|
|
await Requests.createLock.WaitAsync();
|
|
await userLock.WaitAsync();
|
|
try
|
|
{
|
|
string username = await Account.Utils.NameFromId(id);
|
|
if (!string.IsNullOrEmpty(username))
|
|
{
|
|
Fs.DeleteFile($"{LarpixServer.Utils.Utils.ACCOUNTS_NAME_DIR}/{username.ToLower()}");
|
|
}
|
|
Fs.DeleteDirectory(account);
|
|
Fs.WriteFile($"{LarpixServer.Utils.Utils.ACCOUNTS_FREEID_DIR}/{id}", []);
|
|
Console.WriteLine($"Deleted {id} ({username})");
|
|
}
|
|
finally
|
|
{
|
|
userLock.Release();
|
|
Requests.createLock.Release();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
await Task.Delay(5);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
}
|
|
} |