Skip to content
3 changes: 3 additions & 0 deletions src/ServiceControl.Audit.Persistence.RavenDB/DatabaseSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Raven.Client.Documents;
Expand All @@ -25,6 +27,7 @@ public async Task Execute(IDocumentStore documentStore, CancellationToken cancel

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

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

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

using System;
using System.Net.Http;
using System.Text.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)
{
var client = new HttpClient() { BaseAddress = new Uri(configuration.ServerConfiguration.ConnectionString ?? configuration.ServerConfiguration.ServerUrl) };
var licenseCorrectlySetup = false;
var attempts = 0;
while (!licenseCorrectlySetup)
{
var httpResponse = await client.GetAsync("license/status", cancellationToken);
var responseJsonString = await httpResponse.Content.ReadAsStringAsync(cancellationToken);
var licenseStatus = JsonSerializer.Deserialize<LicenseStatusFragment>(responseJsonString);
if (licenseStatus.Expired)
{
throw new NotSupportedException("The current RavenDB license is expired. Please, contact support");
}

if (licenseStatus.LicensedTo != null && licenseStatus.Id != null)
{
licenseCorrectlySetup = true;
Comment thread
ramonsmits marked this conversation as resolved.
Outdated
}

if (++attempts > 10)
Comment thread
ramonsmits marked this conversation as resolved.
Outdated
{
throw new NotSupportedException("Cannot validate the current RavenDB license. Please, contact support");
}

await Task.Delay(500, cancellationToken);
}
}
}
1 change: 1 addition & 0 deletions src/ServiceControl.Persistence.RavenDB/DatabaseSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public async Task Execute(CancellationToken cancellationToken)

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

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

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

using System;
using System.Net.Http;
using System.Text.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(RavenPersisterSettings configuration, CancellationToken cancellationToken)
{
var client = new HttpClient() { BaseAddress = new Uri(configuration.ConnectionString ?? configuration.ServerUrl) };
var licenseCorrectlySetup = false;
var attempts = 0;
while (!licenseCorrectlySetup)
{
var httpResponse = await client.GetAsync("license/status", cancellationToken);
var responseJsonString = await httpResponse.Content.ReadAsStringAsync(cancellationToken);
var licenseStatus = JsonSerializer.Deserialize<LicenseStatusFragment>(responseJsonString);
if (licenseStatus.Expired)
{
throw new NotSupportedException("The current RavenDB license is expired. Please, contact support");
}

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

if (++attempts > 10)
{
throw new NotSupportedException("Cannot validate the current RavenDB license. Please, contact support");
}

await Task.Delay(500, cancellationToken);
}
}
}
Loading