Skip to content

Commit 5c11534

Browse files
Mgmt hub: connect to hub after init as completed, refactoring
1 parent 83c8910 commit 5c11534

File tree

13 files changed

+81
-59
lines changed

13 files changed

+81
-59
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Certify.Models.Hub;
4+
5+
namespace Certify.Client
6+
{
7+
public interface IManagementServerClient
8+
{
9+
event Action OnConnectionClosed;
10+
event Action OnConnectionReconnected;
11+
event Action OnConnectionReconnecting;
12+
event Func<InstanceCommandRequest, Task<InstanceCommandResult>> OnGetCommandResult;
13+
event Func<ManagedInstanceItems> OnGetInstanceItems;
14+
15+
Task ConnectAsync();
16+
Task Disconnect();
17+
bool IsConnected();
18+
void SendInstanceInfo(Guid commandId, bool isCommandResponse = true);
19+
void SendNotificationToManagementHub(string msgCommandType, object updateMsg);
20+
}
21+
}

src/Certify.Client/ManagementServerClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Certify.Client
99
/// <summary>
1010
/// Implements hub communication with a central management server
1111
/// </summary>
12-
public class ManagementServerClient
12+
public class ManagementServerClient : IManagementServerClient
1313
{
1414

1515
public event Action OnConnectionReconnecting;

src/Certify.Core/Management/CertifyManager/CertifyManager.ManagementHub.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text.Json;
@@ -14,7 +14,7 @@ namespace Certify.Management
1414
{
1515
public partial class CertifyManager
1616
{
17-
private ManagementServerClient _managementServerClient;
17+
private IManagementServerClient _managementServerClient;
1818
private string _managementServerConnectionId = string.Empty;
1919

2020
public async Task<ActionStep> UpdateManagementHub(string url, string joiningKey)
@@ -63,26 +63,30 @@ private void SendHeartbeatToManagementHub()
6363
_managementServerClient.SendInstanceInfo(Guid.NewGuid(), false);
6464
}
6565

66-
private async Task StartManagementHubConnection(string hubUri)
66+
public ManagedInstanceInfo GetManagedInstanceInfo()
6767
{
68-
69-
_serviceLog.Debug("Attempting connection to management hub {hubUri}", hubUri);
70-
71-
var appVersion = Util.GetAppVersion().ToString();
72-
73-
var instanceInfo = new ManagedInstanceInfo
68+
return new ManagedInstanceInfo
7469
{
75-
InstanceId = $"{this.InstanceId}",
70+
InstanceId = InstanceId,
7671
Title = $"{Environment.MachineName}",
7772
OS = EnvironmentUtil.GetFriendlyOSName(detailed: false),
7873
OSVersion = EnvironmentUtil.GetFriendlyOSName(),
79-
ClientVersion = appVersion,
74+
ClientVersion = Util.GetAppVersion().ToString(),
8075
ClientName = ConfigResources.AppName
8176
};
77+
}
78+
private async Task StartManagementHubConnection(string hubUri)
79+
{
80+
81+
_serviceLog.Debug("Attempting connection to management hub {hubUri}", hubUri);
82+
83+
var appVersion = Util.GetAppVersion().ToString();
84+
85+
var instanceInfo = GetManagedInstanceInfo();
8286

8387
if (_managementServerClient != null)
8488
{
85-
_managementServerClient.OnGetCommandResult -= _managementServerClient_OnGetCommandResult;
89+
_managementServerClient.OnGetCommandResult -= PerformDirectHubCommandWithResult;
8690
_managementServerClient.OnConnectionReconnecting -= _managementServerClient_OnConnectionReconnecting;
8791
}
8892

@@ -92,7 +96,7 @@ private async Task StartManagementHubConnection(string hubUri)
9296
{
9397
await _managementServerClient.ConnectAsync();
9498

95-
_managementServerClient.OnGetCommandResult += _managementServerClient_OnGetCommandResult;
99+
_managementServerClient.OnGetCommandResult += PerformDirectHubCommandWithResult;
96100
_managementServerClient.OnConnectionReconnecting += _managementServerClient_OnConnectionReconnecting;
97101
}
98102
catch (Exception ex)
@@ -103,7 +107,7 @@ private async Task StartManagementHubConnection(string hubUri)
103107
}
104108
}
105109

106-
private async Task<InstanceCommandResult> _managementServerClient_OnGetCommandResult(InstanceCommandRequest arg)
110+
public async Task<InstanceCommandResult> PerformDirectHubCommandWithResult(InstanceCommandRequest arg)
107111
{
108112
object val = null;
109113

src/Certify.Core/Management/CertifyManager/CertifyManager.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public partial class CertifyManager : ICertifyManager, IDisposable
9494
/// </summary>
9595
private Shared.ServiceConfig _serverConfig;
9696

97+
private System.Timers.Timer _initTimer;
9798
private System.Timers.Timer _heartbeatTimer;
9899
private System.Timers.Timer _frequentTimer;
99100
private System.Timers.Timer _hourlyTimer;
@@ -197,15 +198,26 @@ public async Task Init()
197198
GenerateDemoItems();
198199
}
199200
#endif
200-
201-
await EnsureMgmtHubConnection();
202201
}
203202

204203
/// <summary>
205204
/// Setup the continuous job tasks for renewals and maintenance
206205
/// </summary>
207206
private void SetupJobs()
208207
{
208+
// 1 shot init of async startup dependencyies (e.g. initial connection to mgmt hub instance)
209+
_initTimer = new System.Timers.Timer(2 * 1000); // 2 seconds
210+
_initTimer.Elapsed += async (s, e) =>
211+
{
212+
_initTimer.Stop();
213+
await EnsureMgmtHubConnection();
214+
};
215+
_initTimer.Start();
216+
217+
_heartbeatTimer = new System.Timers.Timer(30 * 1000); // every n seconds
218+
_heartbeatTimer.Elapsed += _heartbeatTimer_Elapsed;
219+
_heartbeatTimer.Start();
220+
209221
// n second job timer (reporting etc)
210222
_heartbeatTimer = new System.Timers.Timer(30 * 1000); // every n seconds
211223
_heartbeatTimer.Elapsed += _heartbeatTimer_Elapsed;
Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System;
1+
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Threading.Tasks;
5+
using Certify.Client;
56
using Certify.Config;
67
using Certify.Models;
78
using Certify.Models.Config;
@@ -16,36 +17,23 @@ namespace Certify.Management
1617
public interface ICertifyManager
1718
{
1819
Task Init();
19-
2020
void SetStatusReporting(IStatusReporting statusReporting);
21-
2221
Task<bool> IsServerTypeAvailable(StandardServerTypes serverType);
23-
2422
Task<Version> GetServerTypeVersion(StandardServerTypes serverType);
25-
2623
Task<List<ActionStep>> RunServerDiagnostics(StandardServerTypes serverType, string siteId);
27-
2824
Task<ManagedCertificate> GetManagedCertificate(string id);
29-
3025
Task<List<ManagedCertificate>> GetManagedCertificates(ManagedCertificateFilter filter = null);
3126
Task<ManagedCertificateSearchResult> GetManagedCertificateResults(ManagedCertificateFilter filter = null);
3227
Task<Certify.Models.Reporting.StatusSummary> GetManagedCertificateSummary(ManagedCertificateFilter filter = null);
33-
3428
Task<ManagedCertificate> UpdateManagedCertificate(ManagedCertificate site);
35-
3629
Task<ActionResult> DeleteManagedCertificate(string id);
37-
3830
Task<ImportExportPackage> PerformExport(ExportRequest exportRequest);
3931
Task<List<ActionStep>> PerformImport(ImportRequest importRequest);
40-
4132
Task<List<SimpleAuthorizationChallengeItem>> GetCurrentChallengeResponses(string challengeType, string key = null);
4233

4334
Task<List<AccountDetails>> GetAccountRegistrations();
44-
4535
Task<ActionResult> AddAccount(ContactRegistration reg);
46-
4736
Task<ActionResult> UpdateAccountContact(string storageKey, ContactRegistration contact);
48-
4937
Task<ActionResult> RemoveAccount(string storageKey, bool includeAccountDeactivation = false);
5038
Task<ActionResult<AccountDetails>> ChangeAccountKey(string storageKey, string newKeyPEM = null);
5139

@@ -55,50 +43,30 @@ public interface ICertifyManager
5543
Task<List<DnsZone>> GetDnsProviderZones(string providerTypeId, string credentialId);
5644
Task<ActionResult> UpdateCertificateAuthority(CertificateAuthority certificateAuthority);
5745
Task<List<CertificateAuthority>> GetCertificateAuthorities();
58-
5946
Task<StatusMessage> RevokeCertificate(ILog log, ManagedCertificate managedCertificate);
60-
6147
Task<CertificateRequestResult> PerformDummyCertificateRequest(ManagedCertificate managedCertificate, IProgress<RequestProgressState> progress = null);
6248
Task<ActionResult> RemoveCertificateAuthority(string id);
6349
Task<List<SiteInfo>> GetPrimaryWebSites(StandardServerTypes serverType, bool ignoreStoppedSites, string itemId = null);
64-
6550
Task<List<CertificateRequestResult>> RedeployManagedCertificates(ManagedCertificateFilter filter, IProgress<RequestProgressState> progress = null, bool isPreviewOnly = false, bool includeDeploymentTasks = false);
66-
6751
Task<CertificateRequestResult> DeployCertificate(ManagedCertificate managedCertificate, IProgress<RequestProgressState> progress = null, bool isPreviewOnly = false, bool includeDeploymentTasks = false);
68-
6952
Task<CertificateRequestResult> PerformCertificateRequest(ILog log, ManagedCertificate managedCertificate, IProgress<RequestProgressState> progress = null, bool resumePaused = false, bool skipRequest = false, bool failOnSkip = false, bool skipTasks = false, bool isInteractive = false, string reason = null);
70-
7153
Task<List<DomainOption>> GetDomainOptionsFromSite(StandardServerTypes serverType, string siteId);
72-
7354
Task<List<CertificateRequestResult>> PerformRenewAll(RenewalSettings settings, ConcurrentDictionary<string, Progress<RequestProgressState>> progressTrackers = null);
74-
7555
Task<bool> PerformRenewalTasks();
76-
7756
Task<bool> PerformDailyMaintenanceTasks();
78-
7957
Task PerformCertificateCleanup();
80-
8158
Task<List<ActionResult>> PerformCertificateMaintenanceTasks(string managedItemId = null);
82-
8359
Task<List<ActionStep>> GeneratePreview(ManagedCertificate item);
84-
8560
void ReportProgress(IProgress<RequestProgressState> progress, RequestProgressState state, bool logThisEvent = true);
86-
8761
Task<List<ActionStep>> PerformDeploymentTask(ILog log, string managedCertificateId, string taskId, bool isPreviewOnly, bool skipDeferredTasks, bool forceTaskExecution);
88-
8962
Task<List<DeploymentProviderDefinition>> GetDeploymentProviders();
90-
9163
Task<List<ActionResult>> ValidateDeploymentTask(ManagedCertificate managedCertificate, DeploymentTaskConfig taskConfig);
92-
9364
Task<DeploymentProviderDefinition> GetDeploymentProviderDefinition(string id, DeploymentTaskConfig config);
94-
9565
Task<LogItem[]> GetItemLog(string id, int limit = 1000);
9666

9767
Task<string[]> GetServiceLog(string logType, int limit = 10000);
98-
9968
ICredentialsManager GetCredentialsManager();
10069
IManagedItemStore GetManagedItemStore();
101-
10270
Task ApplyPreferences();
10371

10472
Task<List<ProviderDefinition>> GetDataStoreProviders();
@@ -117,6 +85,10 @@ public interface ICertifyManager
11785
Task<ActionResult> DeleteManagedChallenge(string id);
11886
Task<ActionResult> PerformManagedChallengeRequest(ManagedChallengeRequest request);
11987
Task<ActionResult> CleanupManagedChallengeRequest(ManagedChallengeRequest request);
88+
12089
Task<ActionStep> UpdateManagementHub(string url, string joiningKey);
90+
Task<InstanceCommandResult> PerformDirectHubCommandWithResult(InstanceCommandRequest arg);
91+
92+
ManagedInstanceInfo GetManagedInstanceInfo();
12193
}
12294
}

src/Certify.Server/Certify.Server.Core/Certify.Server.Core/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
1+

22
var builder = WebApplication.CreateBuilder(args);
33

4+
builder.Configuration.AddJsonFile("appsettings-core.json", optional: false, reloadOnChange: true);
5+
46
builder.AddServiceDefaults();
57

68
var startup = new Certify.Server.Core.Startup(builder.Configuration);

src/Certify.Server/Certify.Server.Core/Certify.Server.Core/appsettings.Development.json renamed to src/Certify.Server/Certify.Server.Core/Certify.Server.Core/appsettings-core.Development.json

File renamed without changes.

src/Certify.Server/Certify.Server.Core/Certify.Server.Core/appsettings.json renamed to src/Certify.Server/Certify.Server.Core/Certify.Server.Core/appsettings-core.json

File renamed without changes.

src/Certify.Server/Certify.Server.Hub.Api/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
var builder = WebApplication.CreateBuilder(args);
44

5+
builder.Configuration.AddJsonFile("appsettings-api.json", optional: false, reloadOnChange: true);
6+
57
#if ASPIRE
68
builder.AddServiceDefaults();
79
#endif

src/Certify.Server/Certify.Server.Hub.Api/SignalR/ManagementHub/InstanceManagementStateProvider.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace Certify.Server.Hub.Api.SignalR.ManagementHub
88
public interface IInstanceManagementStateProvider
99
{
1010
public void Clear();
11+
public void SetManagementHubInstanceId(string instanceId);
12+
public string GetManagementHubInstanceId();
1113
public void UpdateInstanceConnectionInfo(string connectionId, ManagedInstanceInfo instanceInfo);
1214
public void UpdateInstanceStatusSummary(string instanceId, StatusSummary summary);
1315
public string GetConnectionIdForInstance(string instanceId);
@@ -40,6 +42,8 @@ public class InstanceManagementStateProvider : IInstanceManagementStateProvider
4042
private ConcurrentDictionary<string, ManagedInstanceItems> _managedInstanceItems = [];
4143
private ConcurrentDictionary<string, StatusSummary> _managedInstanceStatusSummary = [];
4244
private ILogger<InstanceManagementStateProvider> _logger;
45+
private string _mgmtHubInstanceId = string.Empty;
46+
4347
public InstanceManagementStateProvider(ILogger<InstanceManagementStateProvider> logger)
4448
{
4549
_logger = logger;
@@ -56,6 +60,16 @@ public void Clear()
5660

5761
}
5862

63+
public void SetManagementHubInstanceId(string instanceId)
64+
{
65+
_mgmtHubInstanceId = instanceId;
66+
}
67+
68+
public string GetManagementHubInstanceId()
69+
{
70+
return _mgmtHubInstanceId;
71+
}
72+
5973
public List<ManagedInstanceInfo> GetConnectedInstances()
6074
{
6175
return _instanceConnections.Values.ToList();

0 commit comments

Comments
 (0)