| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System;
- using System.Data;
- using System.Threading;
- using System.Threading.Tasks;
- using Microsoft.Extensions.Hosting;
- using ResfullApi.Models;
- using ResfullApi.Models.balance;
- using log4net;
- namespace ApiProcess.Process
- {
- public class AdminUserCheckService : BackgroundService
- {
- private static readonly ILog logger = LogManager.GetLogger(typeof(AdminUserCheckService));
- private const int INTERVAL_MINUTES = 1;
- protected override async Task ExecuteAsync(CancellationToken stoppingToken)
- {
- logger.Info($"AdminUserCheckService started. Will check every {INTERVAL_MINUTES} minutes.");
- while (!stoppingToken.IsCancellationRequested)
- {
- try
- {
- bool result = CheckAdminUser();
- logger.Info($"AdminUserCheckService: Check result = {result}");
- // Wait for 10 minutes before next check
- await Task.Delay(TimeSpan.FromMinutes(INTERVAL_MINUTES), stoppingToken);
- }
- catch (OperationCanceledException)
- {
- logger.Info("AdminUserCheckService: Service is stopping.");
- break;
- }
- catch (Exception ex)
- {
- logger.Error($"AdminUserCheckService: Error occurred: {ex.ToString()}");
- // Wait 1 minute before retrying on error
- await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
- }
- }
- logger.Info("AdminUserCheckService: Service stopped.");
- }
- private bool CheckAdminUser()
- {
- try
- {
- DataSet ds = balanceDataAccess.callCheckDB();
- // Check if DataSet is valid and has data
- if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
- {
- logger.Info("AdminUserCheckService: No data returned. Result = false");
- return false;
- }
- // If we have data, consider it as success (true)
- logger.Info("AdminUserCheckService: Data returned successfully. Result = true");
- return true;
- }
- catch (Exception ex)
- {
- logger.Error($"AdminUserCheckService: Exception in CheckAdminUser: {ex.ToString()}");
- return false;
- }
- }
- }
- }
|