63 lines
2.6 KiB
C#
Executable File
63 lines
2.6 KiB
C#
Executable File
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Serilog.Context;
|
|
using ZB.MOM.WW.CBDDC.Network;
|
|
|
|
namespace ZB.MOM.WW.CBDDC.Hosting.HostedServices;
|
|
|
|
/// <summary>
|
|
/// Hosted service that manages the lifecycle of the discovery service.
|
|
/// </summary>
|
|
public class DiscoveryServiceHostedService : IHostedService
|
|
{
|
|
private readonly IDiscoveryService _discoveryService;
|
|
private readonly ILogger<DiscoveryServiceHostedService> _logger;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DiscoveryServiceHostedService"/> class.
|
|
/// </summary>
|
|
/// <param name="discoveryService">The discovery service to manage.</param>
|
|
/// <param name="logger">The logger used for service lifecycle events.</param>
|
|
public DiscoveryServiceHostedService(
|
|
IDiscoveryService discoveryService,
|
|
ILogger<DiscoveryServiceHostedService> logger)
|
|
{
|
|
_discoveryService = discoveryService;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Starts the discovery service.
|
|
/// </summary>
|
|
/// <param name="cancellationToken">A token used to cancel the startup operation.</param>
|
|
/// <returns>A task that represents the asynchronous start operation.</returns>
|
|
public async Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
using var serviceContext = LogContext.PushProperty("Service", nameof(DiscoveryServiceHostedService));
|
|
using var operationContext = LogContext.PushProperty("OperationId", Guid.NewGuid().ToString("N"));
|
|
using var actionContext = LogContext.PushProperty("Action", "Start");
|
|
|
|
_logger.LogInformation("Starting Discovery Service...");
|
|
await _discoveryService.Start();
|
|
_logger.LogInformation("Discovery Service started");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stops the discovery service.
|
|
/// </summary>
|
|
/// <param name="cancellationToken">A token used to cancel the shutdown operation.</param>
|
|
/// <returns>A task that represents the asynchronous stop operation.</returns>
|
|
public async Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
using var serviceContext = LogContext.PushProperty("Service", nameof(DiscoveryServiceHostedService));
|
|
using var operationContext = LogContext.PushProperty("OperationId", Guid.NewGuid().ToString("N"));
|
|
using var actionContext = LogContext.PushProperty("Action", "Stop");
|
|
|
|
_logger.LogInformation("Stopping Discovery Service...");
|
|
await _discoveryService.Stop();
|
|
_logger.LogInformation("Discovery Service stopped");
|
|
}
|
|
}
|