From aacafe929c3739331db01087e62c89a08b4bdcdb Mon Sep 17 00:00:00 2001 From: killij <51908793+killij@users.noreply.github.com> Date: Tue, 16 Jan 2024 14:31:13 +0000 Subject: [PATCH 1/4] Create build-docker-image.yml --- .github/workflows/build-docker-image.yml | 82 ++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 .github/workflows/build-docker-image.yml diff --git a/.github/workflows/build-docker-image.yml b/.github/workflows/build-docker-image.yml new file mode 100644 index 0000000..0d5c4b6 --- /dev/null +++ b/.github/workflows/build-docker-image.yml @@ -0,0 +1,82 @@ +name: Docker Publish +on: + release: + types: [published] +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + RELEASE_TAG: ${{ github.event.release.tag_name }} +jobs: + build: + name: 'Docker Publish' + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + # This is used to complete the identity challenge with sigstore/fulcio when running outside of PRs. + id-token: write + steps: + # Checkout the release tag version + - name: Checkout repository ${{ env.RELEASE_TAG }} + uses: actions/checkout@v3 + with: + ref: ${{ env.RELEASE_TAG }} + # Get git commit hash + - name: Get short hash + run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_ENV + # Need to lower case the image name for the docker tags when publishing + - name: Downcase IMAGE_NAME variable + run: echo "IMAGE_NAME_LOWER=${IMAGE_NAME,,}" >> $GITHUB_ENV + + # Sort out the image tags + - name: Set initial tag + run: echo "IMAGE_TAGS=${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LOWER }}:${{ env.RELEASE_TAG }}" >> $GITHUB_ENV + - name: Add latest tag if we're not production release + if: contains(env.RELEASE_TAG, 'next') + run: echo "IMAGE_TAGS=${{ env.IMAGE_TAGS }},${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LOWER }}:latest" >> $GITHUB_ENV + #debug + - name: Log the tags + run: echo "Calculated tags value => ${{ env.IMAGE_TAGS }}" + # Setup docker build tool + - name: Setup Docker buildx + uses: docker/setup-buildx-action@v3 + # Login against a Docker registry + - name: Log into registry ${{ env.REGISTRY }} + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + # Extract metadata (tags, labels) for Docker + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + # Build and push Docker image with Buildx (don't push on PR) + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@v5 + with: + context: ./src + file: ./src/Childrens-Social-Care-CPD-Indexer/Dockerfile + push: true + tags: ${{ env.IMAGE_TAGS }} + labels: ${{ steps.meta.outputs.labels }} + build-args: | + VCSREF=${{ env.sha_short }} + VCSTAG=${{ env.RELEASE_TAG }} + cache-from: type=gha + cache-to: type=gha,mode=max + # Sign the resulting Docker image digest. + # This will only write to the public Rekor transparency log when the Docker + # repository is public to avoid leaking data. If you would like to publish + # transparency data even for private images, pass --force to cosign below. + # https://github.com/sigstore/cosign + - name: Install Cosign + uses: sigstore/cosign-installer@v3.2.0 + - name: Check install! + run: cosign version + - name: Sign the published Docker image + # This step uses the identity token to provision an ephemeral certificate against the sigstore community Fulcio instance. + run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign --yes {}@${{ steps.build-and-push.outputs.digest }} \ No newline at end of file From 7a01fb64110677acb8c7208e7c024de09dae048e Mon Sep 17 00:00:00 2001 From: killij <51908793+killij@users.noreply.github.com> Date: Tue, 16 Jan 2024 16:45:54 +0000 Subject: [PATCH 2/4] Convert the function back into a Container App Job --- ...IndexingServiceTests.cs => WorkerTests.cs} | 31 ++++-- src/Childrens-Social-Care-CPD-Indexer.sln | 24 ++--- .../Childrens-Social-Care-CPD-Indexer.csproj | 39 +++---- .../Dockerfile | 11 +- .../Indexer.cs | 37 ------- .../Program.cs | 101 +++++++++--------- .../Properties/launchSettings.json | 13 +-- .../Properties/serviceDependencies.json | 11 -- .../Properties/serviceDependencies.local.json | 11 -- .../Worker.cs | 45 ++++++++ .../appsettings.Development.json | 8 ++ .../appsettings.json | 8 ++ .../host.json | 12 --- 13 files changed, 167 insertions(+), 184 deletions(-) rename src/Childrens-Social-Care-CPD-Indexer.Tests/{IndexingServiceTests.cs => WorkerTests.cs} (66%) delete mode 100644 src/Childrens-Social-Care-CPD-Indexer/Indexer.cs delete mode 100644 src/Childrens-Social-Care-CPD-Indexer/Properties/serviceDependencies.json delete mode 100644 src/Childrens-Social-Care-CPD-Indexer/Properties/serviceDependencies.local.json create mode 100644 src/Childrens-Social-Care-CPD-Indexer/Worker.cs create mode 100644 src/Childrens-Social-Care-CPD-Indexer/appsettings.Development.json create mode 100644 src/Childrens-Social-Care-CPD-Indexer/appsettings.json delete mode 100644 src/Childrens-Social-Care-CPD-Indexer/host.json diff --git a/src/Childrens-Social-Care-CPD-Indexer.Tests/IndexingServiceTests.cs b/src/Childrens-Social-Care-CPD-Indexer.Tests/WorkerTests.cs similarity index 66% rename from src/Childrens-Social-Care-CPD-Indexer.Tests/IndexingServiceTests.cs rename to src/Childrens-Social-Care-CPD-Indexer.Tests/WorkerTests.cs index 782e78a..3b00d8b 100644 --- a/src/Childrens-Social-Care-CPD-Indexer.Tests/IndexingServiceTests.cs +++ b/src/Childrens-Social-Care-CPD-Indexer.Tests/WorkerTests.cs @@ -1,25 +1,33 @@ using Childrens_Social_Care_CPD_Indexer.Core; -using Microsoft.ApplicationInsights; -using Microsoft.Azure.Functions.Worker; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using NSubstitute.ExceptionExtensions; namespace Childrens_Social_Care_CPD_Indexer.Tests; -public class IndexingServiceTests +public class WorkerTests { - private ILogger _logger; + private ILogger _logger; private IResourcesIndexerConfig _config; private IResourcesIndexer _indexer; - private Indexer _sut; + private IHostApplicationLifetime _hostingApplicationLifetime; + private Worker _sut; [SetUp] public void Setup() { - _logger = Substitute.For>(); + _logger = Substitute.For>(); _config = Substitute.For(); _indexer = Substitute.For(); - _sut = new Indexer(_logger, _indexer, _config); + _hostingApplicationLifetime = Substitute.For(); + + _sut = new Worker(_logger, _indexer, _config, _hostingApplicationLifetime); + } + + [TearDown] + public void Teardown() + { + _sut.Dispose(); } [Test] @@ -27,9 +35,10 @@ public async Task StartAsync_Deletes_Index_If_Configured() { // arrange _config.RecreateIndex.Returns(true); + var cancellationTokenSource = new CancellationTokenSource(); // act - await _sut.Run(new TimerInfo()); + await _sut.StartAsync(cancellationTokenSource.Token); // assert await _indexer.Received(1).DeleteIndexAsync(Arg.Any(), Arg.Any()); @@ -41,9 +50,10 @@ public async Task StartAsync_Populates_Index() { // arrange _config.RecreateIndex.Returns(false); + var cancellationTokenSource = new CancellationTokenSource(); // act - await _sut.Run(new TimerInfo()); + await _sut.StartAsync(cancellationTokenSource.Token); // assert await _indexer.Received(1).PopulateIndexAsync(Arg.Any(), Arg.Any(), Arg.Any()); @@ -56,9 +66,10 @@ public async Task StartAsync_Logs_Exception() var exception = new InvalidOperationException(); _config.RecreateIndex.Returns(true); _indexer.DeleteIndexAsync(Arg.Any(), Arg.Any()).Throws(exception); + var cancellationTokenSource = new CancellationTokenSource(); // act - await _sut.Run(new TimerInfo()); + await _sut.StartAsync(cancellationTokenSource.Token); // assert _logger.Received(1).LogError(exception, "Unhandled exception occured"); diff --git a/src/Childrens-Social-Care-CPD-Indexer.sln b/src/Childrens-Social-Care-CPD-Indexer.sln index 21908a1..0a76fb1 100644 --- a/src/Childrens-Social-Care-CPD-Indexer.sln +++ b/src/Childrens-Social-Care-CPD-Indexer.sln @@ -1,11 +1,11 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 -VisualStudioVersion = 17.8.34322.80 +VisualStudioVersion = 17.8.34330.188 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Childrens-Social-Care-CPD-Indexer.Tests", "Childrens-Social-Care-CPD-Indexer.Tests\Childrens-Social-Care-CPD-Indexer.Tests.csproj", "{6984BF56-808E-4294-949D-FFE4B02CCE16}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Childrens-Social-Care-CPD-Indexer", "Childrens-Social-Care-CPD-Indexer\Childrens-Social-Care-CPD-Indexer.csproj", "{F2710DEC-7B59-484A-9D04-F73B1E09563A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Childrens-Social-Care-CPD-Indexer", "Childrens-Social-Care-CPD-Indexer\Childrens-Social-Care-CPD-Indexer.csproj", "{3555944D-1913-4979-B5BF-991C7064613E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Childrens-Social-Care-CPD-Indexer.Tests", "Childrens-Social-Care-CPD-Indexer.Tests\Childrens-Social-Care-CPD-Indexer.Tests.csproj", "{F0344BA1-9B92-42A7-B650-B8064EDD2B9E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -13,19 +13,19 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {6984BF56-808E-4294-949D-FFE4B02CCE16}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6984BF56-808E-4294-949D-FFE4B02CCE16}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6984BF56-808E-4294-949D-FFE4B02CCE16}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6984BF56-808E-4294-949D-FFE4B02CCE16}.Release|Any CPU.Build.0 = Release|Any CPU - {3555944D-1913-4979-B5BF-991C7064613E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {3555944D-1913-4979-B5BF-991C7064613E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {3555944D-1913-4979-B5BF-991C7064613E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {3555944D-1913-4979-B5BF-991C7064613E}.Release|Any CPU.Build.0 = Release|Any CPU + {F2710DEC-7B59-484A-9D04-F73B1E09563A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F2710DEC-7B59-484A-9D04-F73B1E09563A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F2710DEC-7B59-484A-9D04-F73B1E09563A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F2710DEC-7B59-484A-9D04-F73B1E09563A}.Release|Any CPU.Build.0 = Release|Any CPU + {F0344BA1-9B92-42A7-B650-B8064EDD2B9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F0344BA1-9B92-42A7-B650-B8064EDD2B9E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F0344BA1-9B92-42A7-B650-B8064EDD2B9E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F0344BA1-9B92-42A7-B650-B8064EDD2B9E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {EEC0CA75-7434-4AC0-9F45-31CA6CC7D59A} + SolutionGuid = {5F3E3B0D-C6EA-4304-851B-75680DAB332E} EndGlobalSection EndGlobal diff --git a/src/Childrens-Social-Care-CPD-Indexer/Childrens-Social-Care-CPD-Indexer.csproj b/src/Childrens-Social-Care-CPD-Indexer/Childrens-Social-Care-CPD-Indexer.csproj index 1692839..5c81250 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/Childrens-Social-Care-CPD-Indexer.csproj +++ b/src/Childrens-Social-Care-CPD-Indexer/Childrens-Social-Care-CPD-Indexer.csproj @@ -1,39 +1,24 @@ - + + net8.0 - v4 - Exe - enable enable + enable + dotnet-Childrens_Social_Care_CPD_Indexer-907bada8-7f54-479d-b04e-45ec4f148332 Childrens_Social_Care_CPD_Indexer - /home/site/wwwroot Linux - - - - + + + + + - - - - - + + - - - PreserveNewest - - - PreserveNewest - Never - - - - - - \ No newline at end of file + diff --git a/src/Childrens-Social-Care-CPD-Indexer/Dockerfile b/src/Childrens-Social-Care-CPD-Indexer/Dockerfile index c4a2687..9b63d70 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/Dockerfile +++ b/src/Childrens-Social-Care-CPD-Indexer/Dockerfile @@ -1,8 +1,8 @@ #See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. -FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0 AS base -WORKDIR /home/site/wwwroot -EXPOSE 8080 +FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base +USER app +WORKDIR /app FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build ARG BUILD_CONFIGURATION=Release @@ -18,7 +18,6 @@ ARG BUILD_CONFIGURATION=Release RUN dotnet publish "./Childrens-Social-Care-CPD-Indexer.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false FROM base AS final -WORKDIR /home/site/wwwroot +WORKDIR /app COPY --from=publish /app/publish . -ENV AzureWebJobsScriptRoot=/home/site/wwwroot \ - AzureFunctionsJobHost__Logging__Console__IsEnabled=true \ No newline at end of file +ENTRYPOINT ["dotnet", "Childrens-Social-Care-CPD-Indexer.dll"] \ No newline at end of file diff --git a/src/Childrens-Social-Care-CPD-Indexer/Indexer.cs b/src/Childrens-Social-Care-CPD-Indexer/Indexer.cs deleted file mode 100644 index b7c069d..0000000 --- a/src/Childrens-Social-Care-CPD-Indexer/Indexer.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Childrens_Social_Care_CPD_Indexer.Core; -using Microsoft.Azure.Functions.Worker; -using Microsoft.Extensions.Logging; - -namespace Childrens_Social_Care_CPD_Indexer; - -public class Indexer(ILogger logger, IResourcesIndexer resourcesIndexer, IResourcesIndexerConfig config) -{ - [Function("IndexResources")] - public async Task Run([TimerTrigger("0 0 * * SUN" - #if DEBUG - , RunOnStartup= true - #endif - )] TimerInfo myTimer, CancellationToken cancellationToken = default) - { - logger.LogInformation("Indexing started at: {startTime}", DateTime.Now); - try - { - if (config.RecreateIndex) - { - await resourcesIndexer.DeleteIndexAsync(config.IndexName, cancellationToken); - } - - await resourcesIndexer.CreateIndexAsync(config.IndexName, cancellationToken); - await resourcesIndexer.PopulateIndexAsync(config.IndexName, config.BatchSize, cancellationToken); - - } - catch (Exception ex) - { - logger.LogError(ex, "Unhandled exception occured"); - } - finally - { - logger.LogInformation("Indexing finished at: {finishTime}", DateTime.Now); - } - } -} diff --git a/src/Childrens-Social-Care-CPD-Indexer/Program.cs b/src/Childrens-Social-Care-CPD-Indexer/Program.cs index f3352b8..e61132b 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/Program.cs +++ b/src/Childrens-Social-Care-CPD-Indexer/Program.cs @@ -1,59 +1,56 @@ -using Childrens_Social_Care_CPD_Indexer.Core; -using Childrens_Social_Care_CPD_Indexer; -using Microsoft.Azure.Functions.Worker; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.ApplicationInsights.WorkerService; using Azure.Search.Documents.Indexes; using Azure; +using Childrens_Social_Care_CPD_Indexer; +using Childrens_Social_Care_CPD_Indexer.Core; using Contentful.Core.Configuration; using Contentful.Core; +using Microsoft.ApplicationInsights.WorkerService; using Microsoft.ApplicationInsights; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Logging; +using System.Diagnostics.CodeAnalysis; + +var builder = Host.CreateApplicationBuilder(args); -var host = new HostBuilder() - .ConfigureFunctionsWorkerDefaults() - .ConfigureServices((context, services) => +builder.Services.AddTransient(); +var config = new ResourcesIndexerConfig(builder.Configuration); + +// Logging +var options = new ApplicationInsightsServiceOptions() +{ + ApplicationVersion = config.ApplicationVersion, + ConnectionString = config.AppInsightsConnectionString, +}; + +builder.Services.AddApplicationInsightsTelemetryWorkerService(options); + +// Code dependencies +builder.Services.AddTransient(); +builder.Services.AddTransient(servicesProvider => { + var httpClient = servicesProvider.GetRequiredService(); + var resourcesIndexerConfig = servicesProvider.GetRequiredService(); + var contentfulOptions = new ContentfulOptions() { - services.AddTransient(); - var config = new ResourcesIndexerConfig(context.Configuration); - - - // Logging - var options = new ApplicationInsightsServiceOptions() - { - ApplicationVersion = config.ApplicationVersion, - ConnectionString = config.AppInsightsConnectionString, - }; - - services.AddApplicationInsightsTelemetryWorkerService(options); - services.ConfigureFunctionsApplicationInsights(); - - // Code dependencies - services.TryAddTransient(); - services.AddTransient(servicesProvider => { - var httpClient = servicesProvider.GetRequiredService(); - var resourcesIndexerConfig = servicesProvider.GetRequiredService(); - var contentfulOptions = new ContentfulOptions() - { - DeliveryApiKey = resourcesIndexerConfig.ContentfulApiKey, - SpaceId = resourcesIndexerConfig.ContentfulSpaceId, - Environment = resourcesIndexerConfig.ContentfulEnvironmentId - }; - return new ContentfulClient(httpClient, contentfulOptions); - }); - services.AddTransient(); - services.AddTransient(servicesProvider => { - var logger = servicesProvider.GetRequiredService>(); - var config = servicesProvider.GetRequiredService(); - var documentFetcher = servicesProvider.GetRequiredService(); - var searchEndpointUri = new Uri(config.Endpoint); - var searchIndexClient = new SearchIndexClient(searchEndpointUri, new AzureKeyCredential(config.ApiKey)); - var telemtryClient = servicesProvider.GetRequiredService(); - return new ResourcesIndexer(searchIndexClient, documentFetcher, logger, telemtryClient); - }); - }) - .Build(); - -host.Run(); + DeliveryApiKey = resourcesIndexerConfig.ContentfulApiKey, + SpaceId = resourcesIndexerConfig.ContentfulSpaceId, + Environment = resourcesIndexerConfig.ContentfulEnvironmentId + }; + return new ContentfulClient(httpClient, contentfulOptions); +}); + +builder.Services.AddTransient(); +builder.Services.AddTransient(servicesProvider => { + var logger = servicesProvider.GetRequiredService>(); + var config = servicesProvider.GetRequiredService(); + var documentFetcher = servicesProvider.GetRequiredService(); + var searchEndpointUri = new Uri(config.Endpoint); + var searchIndexClient = new SearchIndexClient(searchEndpointUri, new AzureKeyCredential(config.ApiKey)); + var telemtryClient = servicesProvider.GetRequiredService(); + return new ResourcesIndexer(searchIndexClient, documentFetcher, logger, telemtryClient); +}); + +builder.Services.AddHostedService(); + +var host = builder.Build(); +await host.RunAsync(); + +[ExcludeFromCodeCoverage] +public partial class Program() { } \ No newline at end of file diff --git a/src/Childrens-Social-Care-CPD-Indexer/Properties/launchSettings.json b/src/Childrens-Social-Care-CPD-Indexer/Properties/launchSettings.json index 3939375..cae296e 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/Properties/launchSettings.json +++ b/src/Childrens-Social-Care-CPD-Indexer/Properties/launchSettings.json @@ -2,13 +2,14 @@ "profiles": { "Childrens_Social_Care_CPD_Indexer": { "commandName": "Project", - "commandLineArgs": "--port 7016" + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": true }, "Docker": { - "commandName": "Docker", - "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", - "httpPort": 31453, - "useSSL": false + "commandName": "Docker" } - } + }, + "$schema": "http://json.schemastore.org/launchsettings.json" } \ No newline at end of file diff --git a/src/Childrens-Social-Care-CPD-Indexer/Properties/serviceDependencies.json b/src/Childrens-Social-Care-CPD-Indexer/Properties/serviceDependencies.json deleted file mode 100644 index df4dcc9..0000000 --- a/src/Childrens-Social-Care-CPD-Indexer/Properties/serviceDependencies.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "dependencies": { - "appInsights1": { - "type": "appInsights" - }, - "storage1": { - "type": "storage", - "connectionId": "AzureWebJobsStorage" - } - } -} \ No newline at end of file diff --git a/src/Childrens-Social-Care-CPD-Indexer/Properties/serviceDependencies.local.json b/src/Childrens-Social-Care-CPD-Indexer/Properties/serviceDependencies.local.json deleted file mode 100644 index b804a28..0000000 --- a/src/Childrens-Social-Care-CPD-Indexer/Properties/serviceDependencies.local.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "dependencies": { - "appInsights1": { - "type": "appInsights.sdk" - }, - "storage1": { - "type": "storage.emulator", - "connectionId": "AzureWebJobsStorage" - } - } -} \ No newline at end of file diff --git a/src/Childrens-Social-Care-CPD-Indexer/Worker.cs b/src/Childrens-Social-Care-CPD-Indexer/Worker.cs new file mode 100644 index 0000000..9db030e --- /dev/null +++ b/src/Childrens-Social-Care-CPD-Indexer/Worker.cs @@ -0,0 +1,45 @@ +using Childrens_Social_Care_CPD_Indexer.Core; + +namespace Childrens_Social_Care_CPD_Indexer; + +public class Worker : BackgroundService +{ + private readonly ILogger _logger; + private readonly IResourcesIndexer _resourcesIndexer; + private readonly IResourcesIndexerConfig _config; + private readonly IHostApplicationLifetime _hostApplicationLifetime; + + public Worker(ILogger logger, IResourcesIndexer resourcesIndexer, IResourcesIndexerConfig config, IHostApplicationLifetime hostApplicationLifetime) + { + _logger = logger; + _resourcesIndexer = resourcesIndexer; + _config = config; + _hostApplicationLifetime = hostApplicationLifetime; + } + + 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); + try + { + if (_config.RecreateIndex) + { + await _resourcesIndexer.DeleteIndexAsync(_config.IndexName, stoppingToken); + } + await _resourcesIndexer.CreateIndexAsync(_config.IndexName, stoppingToken); + await _resourcesIndexer.PopulateIndexAsync(_config.IndexName, _config.BatchSize, stoppingToken); + + } + catch (Exception ex) + { + _logger.LogError(ex, "Unhandled exception occured"); + } + finally + { + _logger.LogInformation("Indexing finished at: {finishTime}", DateTime.Now); + } + } +} diff --git a/src/Childrens-Social-Care-CPD-Indexer/appsettings.Development.json b/src/Childrens-Social-Care-CPD-Indexer/appsettings.Development.json new file mode 100644 index 0000000..b2dcdb6 --- /dev/null +++ b/src/Childrens-Social-Care-CPD-Indexer/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/src/Childrens-Social-Care-CPD-Indexer/appsettings.json b/src/Childrens-Social-Care-CPD-Indexer/appsettings.json new file mode 100644 index 0000000..b2dcdb6 --- /dev/null +++ b/src/Childrens-Social-Care-CPD-Indexer/appsettings.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/src/Childrens-Social-Care-CPD-Indexer/host.json b/src/Childrens-Social-Care-CPD-Indexer/host.json deleted file mode 100644 index ee5cf5f..0000000 --- a/src/Childrens-Social-Care-CPD-Indexer/host.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "version": "2.0", - "logging": { - "applicationInsights": { - "samplingSettings": { - "isEnabled": true, - "excludedTypes": "Request" - }, - "enableLiveMetricsFilters": true - } - } -} \ No newline at end of file From 808e82e8f5ce671566c6b66a0f6a9d78ce443d95 Mon Sep 17 00:00:00 2001 From: killij <51908793+killij@users.noreply.github.com> Date: Tue, 16 Jan 2024 16:48:20 +0000 Subject: [PATCH 3/4] Delete unrequired github actions --- .github/workflows/dotnet-publish.yml | 43 ----------------------- .github/workflows/function-app-deploy.yml | 37 ------------------- 2 files changed, 80 deletions(-) delete mode 100644 .github/workflows/dotnet-publish.yml delete mode 100644 .github/workflows/function-app-deploy.yml diff --git a/.github/workflows/dotnet-publish.yml b/.github/workflows/dotnet-publish.yml deleted file mode 100644 index 927d758..0000000 --- a/.github/workflows/dotnet-publish.yml +++ /dev/null @@ -1,43 +0,0 @@ -name: Dotnet Publish - -on: - release: - types: [published] - -env: - RELEASE_TAG: ${{ github.event.release.tag_name }} - -jobs: - build: - name: 'Build Deployment Package' - runs-on: ubuntu-latest - steps: - - name: Checkout repository ${{ env.RELEASE_TAG }} - uses: actions/checkout@v3 - with: - ref: ${{ env.RELEASE_TAG }} - - - name: Setup dotnet - uses: actions/setup-dotnet@v3 - with: - dotnet-version: '8.x' - - - name: dotnet publish - run: dotnet publish ./Childrens-Social-Care-CPD-Indexer/Childrens-Social-Care-CPD-Indexer.csproj -c Release -o ./publish - working-directory: ./src - - - name: Zip output - run: zip -r ../${{ env.RELEASE_TAG }}.build.zip ./ - working-directory: ./src/publish - - - name: Upload to release - uses: softprops/action-gh-release@v1 - if: startsWith(github.ref, 'refs/tags/') - with: - files: ./src/${{ env.RELEASE_TAG }}.build.zip - - # - name: Upload artifacts - # uses: actions/upload-artifact@v3 - # with: - # name: resource-indexing-deployment - # path: ./src/${{ env.RELEASE_TAG }}.zip \ No newline at end of file diff --git a/.github/workflows/function-app-deploy.yml b/.github/workflows/function-app-deploy.yml deleted file mode 100644 index 6ad4028..0000000 --- a/.github/workflows/function-app-deploy.yml +++ /dev/null @@ -1,37 +0,0 @@ -name: Function App Deploy - -on: - workflow_dispatch: - inputs: - tag: - description: 'App Tag to deploy' - required: true - default: 'v1.0.0' - workspace: - description: 'Environment to deploy to' - required: true - default: 'Dev' - type: choice - options: - - Dev - - Test - - Pre-Prod - - Prod - - Load-Test - -jobs: - build: - name: 'Function App Deploy' - runs-on: ubuntu-latest - environment: ${{ inputs.workspace }} - steps: - - name: Sign in to Azure - uses: azure/login@v1 - with: - creds: '{"clientId":"${{ secrets.FAD_CLIENT_ID }}","clientSecret":"${{ secrets.FAD_CLIENT_SECRET }}","subscriptionId":"${{ secrets.FAD_SUBSCRIPTION_ID }}","tenantId":"${{ secrets.FAD_TENANT_ID }}"}' - - - name: Fetch release - run: wget https://github.com/${{ github.repository }}/releases/download/${{ inputs.tag }}/${{ inputs.tag }}.build.zip - - - name: Deploy Function App - run: az functionapp deployment source config-zip --name ${{ vars.FAD_FUNCTION_APP_NAME }} --resource-group ${{ vars.FAD_RESOURCE_GROUP }} --src ${{ inputs.tag }}.build.zip From 901520bde1efdfa188ded91df4fd628f506eb439 Mon Sep 17 00:00:00 2001 From: killij <51908793+killij@users.noreply.github.com> Date: Tue, 16 Jan 2024 17:24:31 +0000 Subject: [PATCH 4/4] Make sure *everything* gets logged to ApplicationInsights --- src/Childrens-Social-Care-CPD-Indexer/appsettings.json | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Childrens-Social-Care-CPD-Indexer/appsettings.json b/src/Childrens-Social-Care-CPD-Indexer/appsettings.json index b2dcdb6..96a7301 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/appsettings.json +++ b/src/Childrens-Social-Care-CPD-Indexer/appsettings.json @@ -3,6 +3,13 @@ "LogLevel": { "Default": "Information", "Microsoft.Hosting.Lifetime": "Information" + }, + "ApplicationInsights": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } } } }