-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathAllowedVmSizesService.cs
91 lines (81 loc) · 2.89 KB
/
AllowedVmSizesService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace TesApi.Web
{
/// <summary>
/// Service that periodically fetches the allowed vms list from storage, and updates the supported vms list.
/// </summary>
public class AllowedVmSizesService : BackgroundService, IAllowedVmSizesService
{
private readonly TimeSpan refreshInterval = TimeSpan.FromHours(24);
private readonly ILogger logger;
private readonly ConfigurationUtils configUtils;
private List<string> allowedVmSizes;
private Task firstTask;
/// <summary>
/// Service that periodically fetches the allowed vms list from storage, and updates the supported vms list.
/// </summary>
/// <param name="configUtils"></param>
/// <param name="logger"></param>
public AllowedVmSizesService(ConfigurationUtils configUtils, ILogger<AllowedVmSizesService> logger)
{
ArgumentNullException.ThrowIfNull(configUtils);
ArgumentNullException.ThrowIfNull(logger);
this.configUtils = configUtils;
this.logger = logger;
}
private async Task GetAllowedVmSizesImpl()
{
try
{
logger.LogInformation("Executing allowed vm sizes config setup");
allowedVmSizes = await configUtils.ProcessAllowedVmSizesConfigurationFileAsync();
}
catch (Exception e)
{
logger.LogError(e, "Failed to execute allowed vm sizes config setup");
throw;
}
}
/// <inheritdoc/>
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
firstTask = GetAllowedVmSizesImpl();
await firstTask;
using PeriodicTimer timer = new(refreshInterval);
try
{
while (await timer.WaitForNextTickAsync(stoppingToken))
{
await GetAllowedVmSizesImpl();
}
}
catch (OperationCanceledException)
{
logger.LogInformation("AllowedVmSizes Service is stopping.");
}
}
/// <summary>
/// Awaits start up and then return allowed vm sizes.
/// </summary>
/// <returns>List of allowed vms.</returns>
public async Task<List<string>> GetAllowedVmSizes()
{
if (allowedVmSizes == null)
{
while (firstTask is null)
{
await Task.Delay(TimeSpan.FromSeconds(1));
}
await firstTask;
}
return allowedVmSizes;
}
}
}