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; /// /// Hosted service that manages the lifecycle of the discovery service. /// public class DiscoveryServiceHostedService : IHostedService { private readonly IDiscoveryService _discoveryService; private readonly ILogger _logger; /// /// Initializes a new instance of the class. /// /// The discovery service to manage. /// The logger used for service lifecycle events. public DiscoveryServiceHostedService( IDiscoveryService discoveryService, ILogger logger) { _discoveryService = discoveryService; _logger = logger; } /// /// Starts the discovery service. /// /// A token used to cancel the startup operation. /// A task that represents the asynchronous start operation. 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"); } /// /// Stops the discovery service. /// /// A token used to cancel the shutdown operation. /// A task that represents the asynchronous stop operation. 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"); } }