Files
caliverse_server/UGQApiServer/BackGroundService/ReserveAccountGradeBackGroundService.cs
2025-05-01 07:20:41 +09:00

62 lines
1.7 KiB
C#

using UGQDataAccess.Service;
using ServerCore; using ServerBase;
public class ReserveAccountGradeBackGroundService : BackgroundService
{
private const int BaseDelayMs = 1 * 60 * 1_000;
private PeriodicTimer? m_timer;
private AccountService _accountService;
public ReserveAccountGradeBackGroundService(AccountService accountService)
{
_accountService = accountService;
}
public override void Dispose()
{
base.Dispose();
m_timer?.Dispose();
GC.SuppressFinalize(this);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
CreateTimer();
try
{
if (!await m_timer!.WaitForNextTickAsync(stoppingToken).ConfigureAwait(false))
{
return;
}
while (!stoppingToken.IsCancellationRequested)
{
await RunAsync().ConfigureAwait(false);
if (!await m_timer!.WaitForNextTickAsync(stoppingToken).ConfigureAwait(false))
{
break;
}
}
}
catch (Exception e)
{
Log.getLogger().error($"Background Exception : {e.Message}");
}
}
private void CreateTimer() => m_timer = new PeriodicTimer(TimeSpan.FromMilliseconds(BaseDelayMs));
protected async Task RunAsync()
{
var list = await _accountService.completeReserveAccountGrade();
foreach(var reserveAccount in list)
{
await _accountService.modifyAccountGrade(reserveAccount.UserGuid, reserveAccount.ReserveGradeType);
}
await Task.CompletedTask;
}
}