retry init users
This commit is contained in:
+15
-40
@@ -42,7 +42,7 @@ namespace SafeBagDE70
|
||||
{
|
||||
try
|
||||
{
|
||||
WaitSQLStart();
|
||||
|
||||
CDMSTATE = new CDMSTATE();
|
||||
DeviceStatus = new DeviceStatus
|
||||
{
|
||||
@@ -70,6 +70,11 @@ namespace SafeBagDE70
|
||||
}
|
||||
|
||||
private static void InitUsers()
|
||||
{
|
||||
int retryCount = 0;
|
||||
int maxRetries = 20; // Max retries before giving up
|
||||
int delay = 5000; // Delay between retries (5 seconds)
|
||||
while (retryCount < maxRetries)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -91,8 +96,13 @@ namespace SafeBagDE70
|
||||
}
|
||||
}
|
||||
CurrentCustomerAccount = customerAccounts[0];
|
||||
Log.Information("InitUsers thành công");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
retryCount++;
|
||||
|
||||
if (retryCount >= maxRetries)
|
||||
{
|
||||
Log.Fatal(ex, "InitUsers: " + ex.Message);
|
||||
if (DeviceStatus != null)
|
||||
@@ -100,48 +110,13 @@ namespace SafeBagDE70
|
||||
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;
|
||||
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