forked from daveaglick/discoverdotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FoundationManager.cs
69 lines (64 loc) · 2.29 KB
/
FoundationManager.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
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Polly;
using Polly.Retry;
using Statiq.Common;
namespace DiscoverDotnet
{
public class FoundationManager
{
private const int MaxRetry = 3;
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
private string _readme = null;
public async Task PopulateAsync(IExecutionContext context)
{
AsyncRetryPolicy<string> retryPolicy = Policy<string>
.Handle<Exception>()
.WaitAndRetryAsync(MaxRetry, attempt =>
{
context.LogInformation($"Foundation retry {attempt}");
return TimeSpan.FromSeconds(1 * Math.Pow(2, attempt));
});
await _semaphore.WaitAsync();
try
{
if (_readme == null)
{
// Don't worry about the Foundation readme if we're only validating
if (context.GetBool(SiteKeys.Validate))
{
_readme = string.Empty;
}
else
{
context.LogInformation("Getting .NET Foundation readme");
_readme = await retryPolicy.ExecuteAsync(
async _ =>
{
using (HttpClient httpClient = context.CreateHttpClient())
{
return await httpClient.GetStringAsync("https://raw.githubusercontent.com/dotnet/home/master/README.md");
}
},
context.CancellationToken);
}
}
}
finally
{
_semaphore.Release();
}
}
public bool IsInFoundation(string owner, string name)
{
if (_readme == null)
{
throw new InvalidOperationException("Foundation data not populated");
}
return _readme.Contains($"github.com/{owner}/{name}", StringComparison.OrdinalIgnoreCase);
}
}
}