-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathLicenseStatusCheck.cs
More file actions
41 lines (34 loc) · 1.58 KB
/
LicenseStatusCheck.cs
File metadata and controls
41 lines (34 loc) · 1.58 KB
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
namespace ServiceControl.Audit.Persistence.RavenDB;
using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading;
using System.Threading.Tasks;
static class LicenseStatusCheck
{
record LicenseStatusFragment(string Id, string LicensedTo, string Status, bool Expired);
public static async Task WaitForLicenseOrThrow(DatabaseConfiguration configuration, CancellationToken cancellationToken)
{
using var client = new HttpClient
{
BaseAddress = new Uri(configuration.ServerConfiguration.ConnectionString ?? configuration.ServerConfiguration.ServerUrl)
};
// Not linking to the incoming cancellationToken to ensure no OperationCancelledException prevents the last InvalidOperationException to be thrown
using var cts = new CancellationTokenSource(30_000);
while (!cts.IsCancellationRequested)
{
var httpResponse = await client.GetAsync("license/status", cancellationToken);
var licenseStatus = await httpResponse.Content.ReadFromJsonAsync<LicenseStatusFragment>(cancellationToken);
if (licenseStatus.Expired)
{
throw new InvalidOperationException("The current RavenDB license is expired. Please, contact support");
}
if (licenseStatus.LicensedTo != null && licenseStatus.Id != null)
{
return;
}
await Task.Delay(200, cancellationToken);
}
throw new InvalidOperationException("Cannot validate the current RavenDB license. Please, contact support");
}
}