11// Copyright (c) .NET Foundation. All rights reserved.
22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
4+ using System ;
5+ using System . IO ;
46using System . Threading . Tasks ;
7+ using Microsoft . Extensions . Logging ;
8+ using Newtonsoft . Json ;
9+ using NuGet . Services . Status ;
10+ using NuGetGallery ;
511
612namespace NuGet . Services . Revalidate
713{
814 public class HealthService : IHealthService
915 {
10- public Task < bool > IsHealthyAsync ( )
16+ private readonly ICoreFileStorageService _storage ;
17+ private readonly HealthConfiguration _config ;
18+ private readonly ILogger < HealthService > _logger ;
19+
20+ public HealthService (
21+ ICoreFileStorageService storage ,
22+ HealthConfiguration config ,
23+ ILogger < HealthService > logger )
24+ {
25+ _storage = storage ?? throw new ArgumentNullException ( nameof ( storage ) ) ;
26+ _config = config ?? throw new ArgumentNullException ( nameof ( config ) ) ;
27+ _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
28+ }
29+
30+ public async Task < bool > IsHealthyAsync ( )
1131 {
12- // TODO:
13- // We are software gods that never make mistakes.
14- return Task . FromResult ( true ) ;
32+ using ( var stream = await _storage . GetFileAsync ( _config . ContainerName , _config . StatusBlobName ) )
33+ using ( var reader = new StreamReader ( stream ) )
34+ {
35+ var json = await reader . ReadToEndAsync ( ) ;
36+ var status = JsonConvert . DeserializeObject < ServiceStatus > ( json ) ;
37+ var component = status . ServiceRootComponent . GetByPath ( _config . ComponentPath ) ;
38+
39+ if ( component == null )
40+ {
41+ _logger . LogError (
42+ "Assuming that the service is unhealthy as the component path {ComponentPath} could not be found" ,
43+ _config . ComponentPath ) ;
44+
45+ return false ;
46+ }
47+
48+ return component . Status == ComponentStatus . Up ;
49+ }
1550 }
1651 }
17- }
52+ }
0 commit comments