diff --git a/src/Childrens-Social-Care-CPD-Indexer.Tests/WorkerTests.cs b/src/Childrens-Social-Care-CPD-Indexer.Tests/WorkerTests.cs index 1a663c6..1bbab38 100644 --- a/src/Childrens-Social-Care-CPD-Indexer.Tests/WorkerTests.cs +++ b/src/Childrens-Social-Care-CPD-Indexer.Tests/WorkerTests.cs @@ -1,4 +1,7 @@ using Childrens_Social_Care_CPD_Indexer.Core; +using Microsoft.ApplicationInsights.Channel; +using Microsoft.ApplicationInsights.Extensibility; +using Microsoft.ApplicationInsights; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using NSubstitute.ExceptionExtensions; @@ -11,6 +14,7 @@ public class WorkerTests private IApplicationConfiguration _config; private IResourcesIndexer _indexer; private IHostApplicationLifetime _hostingApplicationLifetime; + private TelemetryClient _telemetryClient; private Worker _sut; [SetUp] @@ -21,7 +25,15 @@ public void Setup() _indexer = Substitute.For(); _hostingApplicationLifetime = Substitute.For(); - _sut = new Worker(_logger, _indexer, _config, _hostingApplicationLifetime); + var configuration = new TelemetryConfiguration(); + var sendItems = new List(); + var channel = Substitute.For(); + configuration.TelemetryChannel = channel; + configuration.ConnectionString = "InstrumentationKey={Guid.NewGuid()};"; + configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer()); + _telemetryClient = new TelemetryClient(configuration); + + _sut = new Worker(_logger, _indexer, _config, _hostingApplicationLifetime, _telemetryClient); } [TearDown] diff --git a/src/Childrens-Social-Care-CPD-Indexer/Worker.cs b/src/Childrens-Social-Care-CPD-Indexer/Worker.cs index fe1bdbd..ae19c8a 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/Worker.cs +++ b/src/Childrens-Social-Care-CPD-Indexer/Worker.cs @@ -1,45 +1,33 @@ using Childrens_Social_Care_CPD_Indexer.Core; +using Microsoft.ApplicationInsights; namespace Childrens_Social_Care_CPD_Indexer; -public class Worker : BackgroundService +public class Worker(ILogger logger, IResourcesIndexer resourcesIndexer, IApplicationConfiguration applicationConfiguration, IHostApplicationLifetime hostApplicationLifetime, TelemetryClient telemetryClient) : BackgroundService { - private readonly ILogger _logger; - private readonly IResourcesIndexer _resourcesIndexer; - private readonly IApplicationConfiguration _config; - private readonly IHostApplicationLifetime _hostApplicationLifetime; - - public Worker(ILogger logger, IResourcesIndexer resourcesIndexer, IApplicationConfiguration applicationConfiguration, IHostApplicationLifetime hostApplicationLifetime) - { - _logger = logger; - _resourcesIndexer = resourcesIndexer; - _hostApplicationLifetime = hostApplicationLifetime; - _config = applicationConfiguration; - } - - protected override async Task ExecuteAsync(CancellationToken stoppingToken) => - await DoWork(stoppingToken).ContinueWith(task => _hostApplicationLifetime.StopApplication(), stoppingToken); + protected override async Task ExecuteAsync(CancellationToken stoppingToken) => await DoWork(stoppingToken).ContinueWith(task => hostApplicationLifetime.StopApplication(), stoppingToken); private async Task DoWork(CancellationToken stoppingToken) { - _logger.LogInformation("Indexing started at: {startTime}", DateTime.Now); + logger.LogInformation("Indexing started at: {startTime}", DateTime.Now); try { - if (_config.SearchIndexing.RecreateIndex) + if (applicationConfiguration.SearchIndexing.RecreateIndex) { - await _resourcesIndexer.DeleteIndexAsync(_config.SearchIndexing.IndexName, stoppingToken); + await resourcesIndexer.DeleteIndexAsync(applicationConfiguration.SearchIndexing.IndexName, stoppingToken); } - await _resourcesIndexer.CreateIndexAsync(_config.SearchIndexing.IndexName, stoppingToken); - await _resourcesIndexer.PopulateIndexAsync(_config.SearchIndexing.IndexName, _config.SearchIndexing.BatchSize, stoppingToken); + await resourcesIndexer.CreateIndexAsync(applicationConfiguration.SearchIndexing.IndexName, stoppingToken); + await resourcesIndexer.PopulateIndexAsync(applicationConfiguration.SearchIndexing.IndexName, applicationConfiguration.SearchIndexing.BatchSize, stoppingToken); } catch (Exception ex) { - _logger.LogError(ex, "Unhandled exception occured"); + logger.LogError(ex, "Unhandled exception occured"); } finally { - _logger.LogInformation("Indexing finished at: {finishTime}", DateTime.Now); + logger.LogInformation("Indexing finished at: {finishTime}", DateTime.Now); + await telemetryClient.FlushAsync(stoppingToken); } } }