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; } }