|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using NuGet.Jobs.Validation; |
| 8 | +using NuGet.Jobs.Validation.Symbols.Core; |
| 9 | +using NuGet.Services.Validation.Orchestrator; |
| 10 | +using NuGet.Services.Validation.Orchestrator.Telemetry; |
| 11 | + |
| 12 | +namespace NuGet.Services.Validation.Symbols |
| 13 | +{ |
| 14 | + [ValidatorName(ValidatorName.SymbolsIngester)] |
| 15 | + public class SymbolsIngester : BaseValidator, IValidator |
| 16 | + { |
| 17 | + private readonly ISymbolsValidationEntitiesService _symbolsValidationEntitiesService; |
| 18 | + private readonly ISymbolsMessageEnqueuer _symbolMessageEnqueuer; |
| 19 | + private readonly ITelemetryService _telemetryService; |
| 20 | + private readonly ILogger<SymbolsIngester> _logger; |
| 21 | + |
| 22 | + public SymbolsIngester( |
| 23 | + ISymbolsValidationEntitiesService symbolsValidationEntitiesService, |
| 24 | + ISymbolsMessageEnqueuer symbolMessageEnqueuer, |
| 25 | + ITelemetryService telemetryService, |
| 26 | + ILogger<SymbolsIngester> logger) |
| 27 | + { |
| 28 | + _symbolsValidationEntitiesService = symbolsValidationEntitiesService ?? throw new ArgumentNullException(nameof(symbolsValidationEntitiesService)); |
| 29 | + _symbolMessageEnqueuer = symbolMessageEnqueuer ?? throw new ArgumentNullException(nameof(symbolMessageEnqueuer)); |
| 30 | + _telemetryService = telemetryService ?? throw new ArgumentNullException(nameof(telemetryService)); |
| 31 | + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 32 | + } |
| 33 | + |
| 34 | + public async Task<IValidationResult> GetResultAsync(IValidationRequest request) |
| 35 | + { |
| 36 | + if (request == null) |
| 37 | + { |
| 38 | + throw new ArgumentNullException(nameof(request)); |
| 39 | + } |
| 40 | + |
| 41 | + var result = SymbolsValidationEntitiesService.ConvertToIValidationResult(await _symbolsValidationEntitiesService.GetSymbolsServerRequestAsync(request)); |
| 42 | + _logger.LogInformation( |
| 43 | + "Symbols status {Status} for PackageId: {PackageId}, PackageNormalizedVersion {PackageNormalizedVersion}, SymbolsPackageKey {SymbolsPackageKey} ", |
| 44 | + result.Status, |
| 45 | + request.PackageId, |
| 46 | + request.PackageVersion, |
| 47 | + request.PackageKey); |
| 48 | + |
| 49 | + return result; |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// The pattern used for the StartAsync: |
| 54 | + /// 1. Check if an ingestion for the specific symbols package key was already started |
| 55 | + /// 2. Only if a ingestion was not started queue the message to be processed. |
| 56 | + /// 3. After the message is queued, update the SymbolServerRequests table. |
| 57 | + /// </summary> |
| 58 | + /// <param name="request">The request to be sent to the ingester job queue.</param> |
| 59 | + /// <returns>The operation status as <see cref="IValidationResult"/>.</returns> |
| 60 | + public async Task<IValidationResult> StartAsync(IValidationRequest request) |
| 61 | + { |
| 62 | + if (request == null) |
| 63 | + { |
| 64 | + throw new ArgumentNullException(nameof(request)); |
| 65 | + } |
| 66 | + |
| 67 | + var result = SymbolsValidationEntitiesService.ConvertToIValidationResult(await _symbolsValidationEntitiesService.GetSymbolsServerRequestAsync(request)); |
| 68 | + |
| 69 | + if (result.Status != ValidationStatus.NotStarted) |
| 70 | + { |
| 71 | + _logger.LogWarning( |
| 72 | + "Symbol ingestion for {PackageId} {PackageNormalizedVersion} {SymbolsPackageKey} has already started.", |
| 73 | + request.PackageId, |
| 74 | + request.PackageVersion, |
| 75 | + request.PackageKey); |
| 76 | + |
| 77 | + return result; |
| 78 | + } |
| 79 | + |
| 80 | + _telemetryService.TrackSymbolsMessageEnqueued(ValidatorName.SymbolsIngester, request.ValidationId); |
| 81 | + await _symbolMessageEnqueuer.EnqueueSymbolsValidationMessageAsync(request); |
| 82 | + |
| 83 | + var newSymbolsRequest = SymbolsValidationEntitiesService.CreateFromValidationRequest(request, SymbolsPackageIngestRequestStatus.Ingesting); |
| 84 | + var savedSymbolRequest = await _symbolsValidationEntitiesService.AddSymbolsServerRequestAsync(newSymbolsRequest); |
| 85 | + |
| 86 | + if(savedSymbolRequest.RequestStatusKey != SymbolsPackageIngestRequestStatus.Ingesting) |
| 87 | + { |
| 88 | + _logger.LogWarning( |
| 89 | + "The symbols ingestion request already in the database. RequestStatus:{Status} for {PackageId} {PackageNormalizedVersion} {SymbolsPackageKey}.", |
| 90 | + newSymbolsRequest.RequestStatusKey, |
| 91 | + request.PackageId, |
| 92 | + request.PackageVersion, |
| 93 | + request.PackageKey); |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + _logger.LogInformation( |
| 98 | + "The symbols ingestion request added to the database. RequestStatus:{Status} for {PackageId} {PackageNormalizedVersion} {SymbolsPackageKey}.", |
| 99 | + newSymbolsRequest.RequestStatusKey, |
| 100 | + request.PackageId, |
| 101 | + request.PackageVersion, |
| 102 | + request.PackageKey); |
| 103 | + } |
| 104 | + return SymbolsValidationEntitiesService.ConvertToIValidationResult(savedSymbolRequest); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments