Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task Execute(IDocumentStore documentStore, CancellationToken cancel

await CreateIndexes(documentStore, configuration.EnableFullTextSearch, cancellationToken);

await LicenseStatusCheck.WaitForLicenseOrThrow(configuration, cancellationToken);
await LicenseStatusCheck.WaitForLicenseOrThrow(documentStore, cancellationToken);
await ConfigureExpiration(documentStore, cancellationToken);
}

Expand Down
48 changes: 26 additions & 22 deletions src/ServiceControl.Audit.Persistence.RavenDB/LicenseStatusCheck.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
namespace ServiceControl.Audit.Persistence.RavenDB;

using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading;
using System.Threading.Tasks;
using Raven.Client.Documents;

static class LicenseStatusCheck
{
record LicenseStatusFragment(string Id, string LicensedTo, string Status, bool Expired);

public static async Task WaitForLicenseOrThrow(DatabaseConfiguration configuration, CancellationToken cancellationToken)
public static async Task WaitForLicenseOrThrow(IDocumentStore documentStore, CancellationToken cancellationToken)
{
using var client = new HttpClient
{
BaseAddress = new Uri(configuration.ServerConfiguration.ConnectionString ?? configuration.ServerConfiguration.ServerUrl)
};
var ravenConfiguredHttpClient = documentStore.GetRequestExecutor().HttpClient;
var licenseCheckUrl = documentStore.Urls[0].TrimEnd('/') + "/license/status";

using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(30_000);

// 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)
try
{
var httpResponse = await client.GetAsync("license/status", cancellationToken);
var licenseStatus = await httpResponse.Content.ReadFromJsonAsync<LicenseStatusFragment>(cancellationToken);
if (licenseStatus.Expired)
while (true)
{
throw new InvalidOperationException("The current RavenDB license is expired. Please, contact support");
}
var httpResponse = await ravenConfiguredHttpClient.GetAsync(licenseCheckUrl, cts.Token);
var licenseStatus = await httpResponse.Content.ReadFromJsonAsync<LicenseStatusFragment>(cts.Token);
if (licenseStatus.Expired)
{
throw new InvalidOperationException("The current RavenDB license is expired. Please, contact support");
}

if (licenseStatus.LicensedTo != null && licenseStatus.Id != null)
{
return;
}
if (licenseStatus.LicensedTo != null && licenseStatus.Id != null)
{
return;
}

await Task.Delay(200, cancellationToken);
await Task.Delay(200, cts.Token);
}
}
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
{
throw new InvalidOperationException("Cannot validate the current RavenDB license. Please, contact support");
}

throw new InvalidOperationException("Cannot validate the current RavenDB license. Please, contact support");
}
}
}
2 changes: 1 addition & 1 deletion src/ServiceControl.Persistence.RavenDB/DatabaseSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task Execute(CancellationToken cancellationToken)

await IndexCreation.CreateIndexesAsync(typeof(DatabaseSetup).Assembly, documentStore, null, null, cancellationToken);

await LicenseStatusCheck.WaitForLicenseOrThrow(settings, cancellationToken);
await LicenseStatusCheck.WaitForLicenseOrThrow(documentStore, cancellationToken);
await ConfigureExpiration(settings, cancellationToken);
}

Expand Down
46 changes: 25 additions & 21 deletions src/ServiceControl.Persistence.RavenDB/LicenseStatusCheck.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
namespace ServiceControl.Persistence.RavenDB;

using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading;
using System.Threading.Tasks;
using Raven.Client.Documents;

static class LicenseStatusCheck
{
record LicenseStatusFragment(string Id, string LicensedTo, string Status, bool Expired);

public static async Task WaitForLicenseOrThrow(RavenPersisterSettings configuration, CancellationToken cancellationToken)
public static async Task WaitForLicenseOrThrow(IDocumentStore documentStore, CancellationToken cancellationToken)
{
using var client = new HttpClient
{
BaseAddress = new Uri(configuration.ConnectionString ?? configuration.ServerUrl)
};
var ravenConfiguredHttpClient = documentStore.GetRequestExecutor().HttpClient;
var licenseCheckUrl = documentStore.Urls[0].TrimEnd('/') + "/license/status";

using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(30_000);

// 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)
try
{
var httpResponse = await client.GetAsync("license/status", cancellationToken);
var licenseStatus = await httpResponse.Content.ReadFromJsonAsync<LicenseStatusFragment>(cancellationToken);
if (licenseStatus.Expired)
while (!cts.IsCancellationRequested)
{
throw new InvalidOperationException("The current RavenDB license is expired. Please, contact support");
}
var httpResponse = await ravenConfiguredHttpClient.GetAsync(licenseCheckUrl, 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;
}
if (licenseStatus.LicensedTo != null && licenseStatus.Id != null)
{
return;
}

await Task.Delay(200, cancellationToken);
await Task.Delay(200, cts.Token);
}
}
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
{
throw new InvalidOperationException("Cannot validate the current RavenDB license. Please, contact support");
}

throw new InvalidOperationException("Cannot validate the current RavenDB license. Please, contact support");
}
}