Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add service startup failure retry mechanism #96

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions ScadaAgent/ScadaAgent/ScadaAgentWkr/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,34 @@ public class Worker(ILogger<Worker> logger) : BackgroundService
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Manager manager = new();
bool isServiceStarted = false;

stoppingToken.Register(() =>
{
manager.StopService();
logger.LogInformation("Agent is stopped");
});

if (manager.StartService())
logger.LogInformation("Agent is started successfully");
else
logger.LogError("Agent is started with errors");

while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(TaskDelay, stoppingToken);
if (isServiceStarted)
{
await Task.Delay(TaskDelay, stoppingToken);
continue;
}

if (manager.StartService())
{
isServiceStarted = true;
logger.LogInformation("Agent is started successfully");
}
else
{
logger.LogError("Agent is started with errors. Retry after 3s...");
manager.StopService();

await Task.Delay(3 * TaskDelay, stoppingToken);
}
}
}
}
Expand Down
25 changes: 19 additions & 6 deletions ScadaComm/ScadaComm/ScadaCommWkr/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,34 @@ public class Worker(ILogger<Worker> logger) : BackgroundService
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Manager manager = new();
bool isServiceStarted = false;

stoppingToken.Register(() =>
{
manager.StopService();
logger.LogInformation("Communicator is stopped");
});

if (manager.StartService())
logger.LogInformation("Communicator is started successfully");
else
logger.LogError("Communicator is started with errors");

while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(TaskDelay, stoppingToken);
if (isServiceStarted)
{
await Task.Delay(TaskDelay, stoppingToken);
continue;
}

if (manager.StartService())
{
isServiceStarted = true;
logger.LogInformation("Communicator is started successfully");
}
else
{
logger.LogError("Communicator is started with errors. Retry after 3s...");
manager.StopService();

await Task.Delay(3 * TaskDelay, stoppingToken);
}
}
}
}
Expand Down
25 changes: 19 additions & 6 deletions ScadaServer/ScadaServer/ScadaServerWkr/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,34 @@ public class Worker(ILogger<Worker> logger) : BackgroundService
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Manager manager = new();
bool isServiceStarted = false;

stoppingToken.Register(() =>
{
manager.StopService();
logger.LogInformation("Server is stopped");
});

if (manager.StartService())
logger.LogInformation("Server is started successfully");
else
logger.LogError("Server is started with errors");

while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(TaskDelay, stoppingToken);
if (isServiceStarted)
{
await Task.Delay(TaskDelay, stoppingToken);
continue;
}

if (manager.StartService())
{
isServiceStarted = true;
logger.LogInformation("Server is started successfully");
}
else
{
logger.LogError("Server is started with errors. Retry after 3s...");
manager.StopService();

await Task.Delay(3 * TaskDelay, stoppingToken);
}
}
}
}
Expand Down