Initial import of the CBDDC codebase with docs and tests. Add a .NET-focused gitignore to keep generated artifacts out of source control.
Some checks failed
CI / verify (push) Has been cancelled
Some checks failed
CI / verify (push) Has been cancelled
This commit is contained in:
85
src/ZB.MOM.WW.CBDDC.Network/CBDDCNodeService.cs
Executable file
85
src/ZB.MOM.WW.CBDDC.Network/CBDDCNodeService.cs
Executable file
@@ -0,0 +1,85 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Serilog.Context;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ZB.MOM.WW.CBDDC.Network;
|
||||
|
||||
/// <summary>
|
||||
/// Hosted service that automatically starts and stops the CBDDC node.
|
||||
/// </summary>
|
||||
public class CBDDCNodeService : IHostedService
|
||||
{
|
||||
private readonly ICBDDCNode _node;
|
||||
private readonly ILogger<CBDDCNodeService> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CBDDCNodeService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="node">The CBDDC node to manage.</param>
|
||||
/// <param name="logger">The logger instance.</param>
|
||||
public CBDDCNodeService(ICBDDCNode node, ILogger<CBDDCNodeService> logger)
|
||||
{
|
||||
_node = node;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the managed CBDDC node.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">A token used to cancel startup.</param>
|
||||
/// <returns>A task that represents the asynchronous start operation.</returns>
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
using var serviceContext = LogContext.PushProperty("Service", nameof(CBDDCNodeService));
|
||||
using var operationContext = LogContext.PushProperty("OperationId", Guid.NewGuid().ToString("N"));
|
||||
using var actionContext = LogContext.PushProperty("Action", "Start");
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Starting CBDDC Node Service...");
|
||||
|
||||
// Check for cancellation before starting
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
await _node.Start();
|
||||
_logger.LogInformation("CBDDC Node Service started successfully");
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
_logger.LogWarning("CBDDC Node Service start was cancelled");
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to start CBDDC Node Service");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops the managed CBDDC node.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">A token used to cancel shutdown.</param>
|
||||
/// <returns>A task that represents the asynchronous stop operation.</returns>
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
using var serviceContext = LogContext.PushProperty("Service", nameof(CBDDCNodeService));
|
||||
using var operationContext = LogContext.PushProperty("OperationId", Guid.NewGuid().ToString("N"));
|
||||
using var actionContext = LogContext.PushProperty("Action", "Stop");
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Stopping CBDDC Node Service...");
|
||||
await _node.Stop();
|
||||
_logger.LogInformation("CBDDC Node Service stopped successfully");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error occurred while stopping CBDDC Node Service");
|
||||
// Don't rethrow during shutdown to avoid breaking the shutdown process
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user