retry init users
This commit is contained in:
+37
-62
@@ -42,7 +42,7 @@ namespace SafeBagDE70
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
WaitSQLStart();
|
|
||||||
CDMSTATE = new CDMSTATE();
|
CDMSTATE = new CDMSTATE();
|
||||||
DeviceStatus = new DeviceStatus
|
DeviceStatus = new DeviceStatus
|
||||||
{
|
{
|
||||||
@@ -71,77 +71,52 @@ namespace SafeBagDE70
|
|||||||
|
|
||||||
private static void InitUsers()
|
private static void InitUsers()
|
||||||
{
|
{
|
||||||
try
|
int retryCount = 0;
|
||||||
|
int maxRetries = 20; // Max retries before giving up
|
||||||
|
int delay = 5000; // Delay between retries (5 seconds)
|
||||||
|
while (retryCount < maxRetries)
|
||||||
{
|
{
|
||||||
|
try
|
||||||
customerAccounts = new List<CustomerAccount>();
|
|
||||||
using (var context = new CDMDbContext())
|
|
||||||
{
|
{
|
||||||
var customers = context.Customers;
|
|
||||||
foreach (var customer in customers)
|
customerAccounts = new List<CustomerAccount>();
|
||||||
|
using (var context = new CDMDbContext())
|
||||||
{
|
{
|
||||||
var ca = new CustomerAccount
|
var customers = context.Customers;
|
||||||
|
foreach (var customer in customers)
|
||||||
{
|
{
|
||||||
Name = customer.TenantName,
|
var ca = new CustomerAccount
|
||||||
CustomerId = customer.AccountNumber,
|
{
|
||||||
QRCode = customer.AccountNumber,
|
Name = customer.TenantName,
|
||||||
PINCode = customer.PinCode
|
CustomerId = customer.AccountNumber,
|
||||||
};
|
QRCode = customer.AccountNumber,
|
||||||
customerAccounts.Add(ca);
|
PINCode = customer.PinCode
|
||||||
|
};
|
||||||
|
customerAccounts.Add(ca);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
CurrentCustomerAccount = customerAccounts[0];
|
||||||
|
Log.Information("InitUsers thành công");
|
||||||
}
|
}
|
||||||
CurrentCustomerAccount = customerAccounts[0];
|
catch (Exception ex)
|
||||||
}
|
|
||||||
catch(Exception ex)
|
|
||||||
{
|
|
||||||
Log.Fatal(ex, "InitUsers: " + ex.Message);
|
|
||||||
if (DeviceStatus != null)
|
|
||||||
{
|
{
|
||||||
DeviceStatus.Description = "Exception in InitUsers: " + ex.Message;
|
retryCount++;
|
||||||
APIModels.SendEvent(DeviceStatus);
|
|
||||||
|
if (retryCount >= maxRetries)
|
||||||
|
{
|
||||||
|
Log.Fatal(ex, "InitUsers: " + ex.Message);
|
||||||
|
if (DeviceStatus != null)
|
||||||
|
{
|
||||||
|
DeviceStatus.Description = "Exception in InitUsers: " + ex.Message;
|
||||||
|
APIModels.SendEvent(DeviceStatus);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Wait for the next retry
|
||||||
|
Thread.Sleep(delay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
private static void WaitSQLStart()
|
|
||||||
{
|
|
||||||
string serviceName = "MSSQLSERVER"; // Default SQL Server service name, change if necessary
|
|
||||||
|
|
||||||
// Create a ServiceController instance for the SQL Server service
|
|
||||||
ServiceController serviceController = new ServiceController(serviceName);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Log.Information("Checking the status of the SQL Server service...");
|
|
||||||
|
|
||||||
// Loop to wait until the service is running
|
|
||||||
while (serviceController.Status != ServiceControllerStatus.Running)
|
|
||||||
{
|
|
||||||
//if (serviceController.Status == ServiceControllerStatus.Stopped)
|
|
||||||
//{
|
|
||||||
// Log.Information("SQL Server service is stopped. Starting it...");
|
|
||||||
// serviceController.Start();
|
|
||||||
//}
|
|
||||||
|
|
||||||
// Refresh the controller to get the updated status
|
|
||||||
serviceController.Refresh();
|
|
||||||
|
|
||||||
Log.Information($"Current status: {serviceController.Status}");
|
|
||||||
Thread.Sleep(5000); // Wait for 5 seconds before checking again
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.Information("SQL Server service is running successfully.");
|
|
||||||
}
|
|
||||||
catch (InvalidOperationException ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Error: Could not access the service '{serviceName}'. {ex.Message}");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Error: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CDMSTATE? CDMSTATE = null;
|
public static CDMSTATE? CDMSTATE = null;
|
||||||
private static DateTime CutOffTime = new DateTime(1, 1, 1, 12, 0, 0); // 12:00 noon as cut-off time
|
private static DateTime CutOffTime = new DateTime(1, 1, 1, 12, 0, 0); // 12:00 noon as cut-off time
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user