11namespace ServiceControl . Persistence . RavenDB ;
22
33using System ;
4- using System . Net . Http ;
54using System . Net . Http . Json ;
65using System . Threading ;
76using System . Threading . Tasks ;
7+ using Raven . Client . Documents ;
88
99static class LicenseStatusCheck
1010{
1111 record LicenseStatusFragment ( string Id , string LicensedTo , string Status , bool Expired ) ;
1212
13- public static async Task WaitForLicenseOrThrow ( RavenPersisterSettings configuration , CancellationToken cancellationToken )
13+ public static async Task WaitForLicenseOrThrow ( IDocumentStore documentStore , CancellationToken cancellationToken )
1414 {
15- using var client = new HttpClient
16- {
17- BaseAddress = new Uri ( configuration . ConnectionString ?? configuration . ServerUrl )
18- } ;
15+ var ravenConfiguredHttpClient = documentStore . GetRequestExecutor ( ) . HttpClient ;
16+ var licenseCheckUrl = documentStore . Urls [ 0 ] . TrimEnd ( '/' ) + "/license/status" ;
17+
18+ using var cts = CancellationTokenSource . CreateLinkedTokenSource ( cancellationToken ) ;
19+ cts . CancelAfter ( 30_000 ) ;
1920
20- // Not linking to the incoming cancellationToken to ensure no OperationCancelledException prevents the last InvalidOperationException to be thrown
21- using var cts = new CancellationTokenSource ( 30_000 ) ;
22- while ( ! cts . IsCancellationRequested )
21+ try
2322 {
24- var httpResponse = await client . GetAsync ( "license/status" , cancellationToken ) ;
25- var licenseStatus = await httpResponse . Content . ReadFromJsonAsync < LicenseStatusFragment > ( cancellationToken ) ;
26- if ( licenseStatus . Expired )
23+ while ( ! cts . IsCancellationRequested )
2724 {
28- throw new InvalidOperationException ( "The current RavenDB license is expired. Please, contact support" ) ;
29- }
25+ var httpResponse = await ravenConfiguredHttpClient . GetAsync ( licenseCheckUrl , cancellationToken ) ;
26+ var licenseStatus = await httpResponse . Content . ReadFromJsonAsync < LicenseStatusFragment > ( cancellationToken ) ;
27+ if ( licenseStatus . Expired )
28+ {
29+ throw new InvalidOperationException ( "The current RavenDB license is expired. Please, contact support" ) ;
30+ }
3031
31- if ( licenseStatus . LicensedTo != null && licenseStatus . Id != null )
32- {
33- return ;
34- }
32+ if ( licenseStatus . LicensedTo != null && licenseStatus . Id != null )
33+ {
34+ return ;
35+ }
3536
36- await Task . Delay ( 200 , cancellationToken ) ;
37+ await Task . Delay ( 200 , cts . Token ) ;
38+ }
39+ }
40+ catch ( OperationCanceledException ) when ( ! cancellationToken . IsCancellationRequested )
41+ {
42+ throw new InvalidOperationException ( "Cannot validate the current RavenDB license. Please, contact support" ) ;
3743 }
38-
39- throw new InvalidOperationException ( "Cannot validate the current RavenDB license. Please, contact support" ) ;
4044 }
4145}
0 commit comments