|
| 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.Diagnostics; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Microsoft.Extensions.Hosting; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using Microsoft.Extensions.Options; |
| 12 | + |
| 13 | +namespace NuGet.Insights.Website |
| 14 | +{ |
| 15 | + public class CachedAdminViewModelService : BackgroundService |
| 16 | + { |
| 17 | + public class AdminViewModelCache : IAdminViewModelCache |
| 18 | + { |
| 19 | + public bool Refreshing { get; set; } |
| 20 | + public CachedAdminViewModel Value { get; set; } |
| 21 | + public NuGetInsightsWebsiteSettings Settings { get; set; } |
| 22 | + } |
| 23 | + |
| 24 | + private readonly IServiceProvider _serviceProvider; |
| 25 | + private readonly AdminViewModelCache _cache; |
| 26 | + private readonly IOptions<NuGetInsightsWebsiteSettings> _options; |
| 27 | + private readonly ILogger<CachedAdminViewModelService> _logger; |
| 28 | + |
| 29 | + public CachedAdminViewModelService( |
| 30 | + IServiceProvider serviceProvider, |
| 31 | + AdminViewModelCache cache, |
| 32 | + IOptions<NuGetInsightsWebsiteSettings> options, |
| 33 | + ILogger<CachedAdminViewModelService> logger) |
| 34 | + { |
| 35 | + _serviceProvider = serviceProvider; |
| 36 | + _cache = cache; |
| 37 | + _options = options; |
| 38 | + _logger = logger; |
| 39 | + } |
| 40 | + |
| 41 | + protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| 42 | + { |
| 43 | + if (!_options.Value.ShowAdminMetadata) |
| 44 | + { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + while (!stoppingToken.IsCancellationRequested) |
| 49 | + { |
| 50 | + _cache.Refreshing = true; |
| 51 | + _logger.LogInformation("Loading latest admin view model."); |
| 52 | + var sw = Stopwatch.StartNew(); |
| 53 | + try |
| 54 | + { |
| 55 | + var asOfTimestamp = DateTimeOffset.UtcNow; |
| 56 | + using (var scope = _serviceProvider.CreateScope()) |
| 57 | + { |
| 58 | + var factory = scope.ServiceProvider.GetRequiredService<ViewModelFactory>(); |
| 59 | + var data = await factory.GetAdminViewModelAsync(); |
| 60 | + _cache.Value = new CachedAdminViewModel(asOfTimestamp, data); |
| 61 | + _cache.Settings = scope.ServiceProvider.GetRequiredService<IOptions<NuGetInsightsWebsiteSettings>>().Value; |
| 62 | + } |
| 63 | + _logger.LogInformation( |
| 64 | + "Latest admin view model loaded after {DurationMs}ms. Sleeping for {SleepMs}ms.", |
| 65 | + sw.Elapsed.TotalMilliseconds, |
| 66 | + _options.Value.CachedAdminViewModelMaxAge.TotalMilliseconds); |
| 67 | + } |
| 68 | + catch (Exception ex) |
| 69 | + { |
| 70 | + _logger.LogError( |
| 71 | + ex, |
| 72 | + "Failed to load admin view model after {DurationMs}ms. Sleeping for {SleepMs}ms.", |
| 73 | + sw.Elapsed.TotalMilliseconds, |
| 74 | + _options.Value.CachedAdminViewModelMaxAge.TotalMilliseconds); |
| 75 | + } |
| 76 | + finally |
| 77 | + { |
| 78 | + _cache.Refreshing = false; |
| 79 | + } |
| 80 | + |
| 81 | + await Task.Delay(_options.Value.CachedAdminViewModelMaxAge, stoppingToken); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments