44using System ;
55using System . Threading . Tasks ;
66using Microsoft . Extensions . Logging ;
7+ using Microsoft . Extensions . Options ;
78using NuGet . Services . ServiceBus ;
9+ using NuGet . Services . Validation . Orchestrator . Telemetry ;
810using NuGetGallery ;
911
1012namespace NuGet . Services . Validation . Orchestrator
1113{
1214 public class ValidationMessageHandler : IMessageHandler < PackageValidationMessageData >
1315 {
16+ private readonly ValidationConfiguration _configs ;
1417 private readonly ICorePackageService _galleryPackageService ;
1518 private readonly IValidationSetProvider _validationSetProvider ;
1619 private readonly IValidationSetProcessor _validationSetProcessor ;
1720 private readonly IValidationOutcomeProcessor _validationOutcomeProcessor ;
21+ private readonly ITelemetryService _telemetryService ;
1822 private readonly ILogger < ValidationMessageHandler > _logger ;
1923
2024 public ValidationMessageHandler (
25+ IOptionsSnapshot < ValidationConfiguration > validationConfigsAccessor ,
2126 ICorePackageService galleryPackageService ,
2227 IValidationSetProvider validationSetProvider ,
2328 IValidationSetProcessor validationSetProcessor ,
2429 IValidationOutcomeProcessor validationOutcomeProcessor ,
30+ ITelemetryService telemetryService ,
2531 ILogger < ValidationMessageHandler > logger )
2632 {
33+ if ( validationConfigsAccessor == null )
34+ {
35+ throw new ArgumentNullException ( nameof ( validationConfigsAccessor ) ) ;
36+ }
37+
38+ if ( validationConfigsAccessor . Value == null )
39+ {
40+ throw new ArgumentException (
41+ $ "The { nameof ( IOptionsSnapshot < ValidationConfiguration > ) } .{ nameof ( IOptionsSnapshot < ValidationConfiguration > . Value ) } property cannot be null",
42+ nameof ( validationConfigsAccessor ) ) ;
43+ }
44+
45+ if ( validationConfigsAccessor . Value . MissingPackageRetryCount < 1 )
46+ {
47+ throw new ArgumentOutOfRangeException (
48+ nameof ( validationConfigsAccessor ) ,
49+ $ "{ nameof ( ValidationConfiguration ) } .{ nameof ( ValidationConfiguration . MissingPackageRetryCount ) } must be at least 1") ;
50+ }
51+
52+ _configs = validationConfigsAccessor . Value ;
2753 _galleryPackageService = galleryPackageService ?? throw new ArgumentNullException ( nameof ( galleryPackageService ) ) ;
2854 _validationSetProvider = validationSetProvider ?? throw new ArgumentNullException ( nameof ( validationSetProvider ) ) ;
2955 _validationSetProcessor = validationSetProcessor ?? throw new ArgumentNullException ( nameof ( validationSetProcessor ) ) ;
3056 _validationOutcomeProcessor = validationOutcomeProcessor ?? throw new ArgumentNullException ( nameof ( validationOutcomeProcessor ) ) ;
57+ _telemetryService = telemetryService ?? throw new ArgumentNullException ( nameof ( telemetryService ) ) ;
3158 _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
3259 }
3360
@@ -48,10 +75,28 @@ public async Task<bool> HandleAsync(PackageValidationMessageData message)
4875 if ( package == null )
4976 {
5077 // no package in DB yet. Might have received message a bit early, need to retry later
51- _logger . LogInformation ( "Did not find information in DB for package {PackageId} {PackageVersion}" ,
52- message . PackageId ,
53- message . PackageVersion ) ;
54- return false ;
78+ if ( message . DeliveryCount - 1 >= _configs . MissingPackageRetryCount )
79+ {
80+ _logger . LogWarning ( "Could not find package {PackageId} {PackageVersion} in DB after {DeliveryCount} tries, dropping message" ,
81+ message . PackageId ,
82+ message . PackageVersion ,
83+ message . DeliveryCount ) ;
84+
85+ _telemetryService . TrackMissingPackageForValidationMessage (
86+ message . PackageId ,
87+ message . PackageVersion ,
88+ message . ValidationTrackingId . ToString ( ) ) ;
89+
90+ return true ;
91+ }
92+ else
93+ {
94+ _logger . LogInformation ( "Could not find package {PackageId} {PackageVersion} in DB, retrying" ,
95+ message . PackageId ,
96+ message . PackageVersion ) ;
97+
98+ return false ;
99+ }
55100 }
56101
57102 var validationSet = await _validationSetProvider . TryGetOrCreateValidationSetAsync ( message . ValidationTrackingId , package ) ;
0 commit comments