Skip to content

Commit f314b33

Browse files
Cleanup
1 parent 8685cb3 commit f314b33

File tree

6 files changed

+71
-58
lines changed

6 files changed

+71
-58
lines changed

src/Certify.Server/Certify.Server.Hub.Api/Services/ManagementAPI.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,21 @@ public async Task ReconnectInstances()
6868
/// <returns>An <see cref="InstanceCommandResult"/> that contains the result of the command if available.</returns>
6969
private async Task<InstanceCommandResult?> GetCommandResult(string instanceId, InstanceCommandRequest cmd)
7070
{
71-
var connectionId = _mgmtStateProvider.GetConnectionIdForInstance(instanceId);
72-
73-
if (connectionId == null)
74-
{
75-
_log.LogError("Instance connection info not known for {instanceId}, cannot send command {cmdId} {cmdType} to instance.", instanceId, cmd.CommandId, cmd.CommandType);
76-
}
77-
7871
if (_certifyManager != null && instanceId == _mgmtStateProvider.GetManagementHubInstanceId())
7972
{
8073
// get command result directly from in-process instance
8174
return await _certifyManager.PerformHubCommandWithResult(cmd);
8275
}
8376
else
8477
{
78+
var connectionId = _mgmtStateProvider.GetConnectionIdForInstance(instanceId);
79+
80+
if (connectionId == null)
81+
{
82+
_log.LogError("Instance connection info not known for {instanceId}, cannot send command {cmdId} {cmdType} to instance.", instanceId, cmd.CommandId, cmd.CommandType);
83+
return null;
84+
}
85+
8586
_mgmtStateProvider.AddAwaitedCommandRequest(cmd);
8687
await _mgmtHubContext.Clients.Client(connectionId).SendCommandRequest(cmd);
8788
return await _mgmtStateProvider.ConsumeAwaitedCommandResult(cmd);
@@ -130,7 +131,7 @@ private async Task SendCommandWithNoResult(string instanceId, InstanceCommandReq
130131

131132
if (result?.Value != null)
132133
{
133-
return JsonSerializer.Deserialize<T>(result.Value);
134+
return JsonSerializer.Deserialize<T>(result.Value, Certify.Shared.JsonOptions.DefaultJsonSerializerOptions);
134135
}
135136
else
136137
{

src/Certify.Server/Certify.Server.Hub.Api/Services/ManagementWorker.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class ManagementWorker : IHostedService, IDisposable
2525
/// <param name="logger"></param>
2626
/// <param name="hubContext"></param>
2727
/// <param name="stateProvider"></param>
28+
/// <param name="mgmtAPI"></param>
2829
public ManagementWorker(ILogger<ManagementWorker> logger, IHubContext<InstanceManagementHub> hubContext, IInstanceManagementStateProvider stateProvider, ManagementAPI mgmtAPI)
2930
{
3031
_logger = logger;

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ public interface IInstanceManagementHub
1111
/// Send command to an instance or the current caller if instance not provided
1212
/// </summary>
1313
/// <param name="cmd"></param>
14-
/// <param name="targetInstanceId">If null, command is targetted at all instances</param>
15-
/// <param name="request"></param>
1614
/// <returns></returns>
1715
Task SendCommandRequest(InstanceCommandRequest cmd);
1816

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

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Certify.Management;
22
using Certify.Models.Hub;
33
using Certify.Models.Reporting;
4+
using Certify.Shared;
45
using Microsoft.AspNetCore.SignalR;
56

67
namespace Certify.Server.Hub.Api.SignalR.ManagementHub
@@ -123,15 +124,18 @@ public override Task OnDisconnectedAsync(Exception? exception)
123124
{
124125
var instanceId = _stateProvider.GetInstanceIdForConnection(Context.ConnectionId);
125126

126-
_stateProvider.UpdateInstanceConnectionStatus(instanceId, ConnectionStatus.Disconnected);
127-
128-
if (exception != null)
129-
{
130-
_logger?.LogError("InstanceManagementHub: Instance {instanceId} disconnected unexpectedly from instance management hub. {exp}", instanceId, exception);
131-
}
132-
else
127+
if (instanceId != null)
133128
{
134-
_logger?.LogInformation("InstanceManagementHub: Instance {instanceId} disconnected from instance management hub, with no error.", instanceId);
129+
_stateProvider.UpdateInstanceConnectionStatus(instanceId, ConnectionStatus.Disconnected);
130+
131+
if (exception != null)
132+
{
133+
_logger?.LogError("InstanceManagementHub: Instance {instanceId} disconnected unexpectedly from instance management hub. {exp}", instanceId, exception);
134+
}
135+
else
136+
{
137+
_logger?.LogInformation("InstanceManagementHub: Instance {instanceId} disconnected from instance management hub, with no error.", instanceId);
138+
}
135139
}
136140

137141
return base.OnDisconnectedAsync(exception);
@@ -199,19 +203,19 @@ private async Task ProcessInstanceCommandResult(InstanceCommandResult result, In
199203
// action this message from this instance
200204
_logger?.LogDebug("[ProcessInstanceCommandResult] Received instance command result {instanceId} {cmdType}", instanceId, cmd.CommandType);
201205

202-
if (cmd.CommandType == ManagementHubCommands.GetManagedItems)
206+
if (cmd.CommandType == ManagementHubCommands.GetManagedItems && result.Value != null)
203207
{
204208
// got items from an instance
205-
var val = System.Text.Json.JsonSerializer.Deserialize<ManagedInstanceItems>(result.Value);
209+
var val = System.Text.Json.JsonSerializer.Deserialize<ManagedInstanceItems>(result.Value, JsonOptions.DefaultJsonSerializerOptions);
206210

207-
_stateProvider.UpdateInstanceItemInfo(instanceId, val.Items);
211+
_stateProvider.UpdateInstanceItemInfo(instanceId, val!.Items);
208212
}
209-
else if (cmd.CommandType == ManagementHubCommands.GetStatusSummary && result?.Value != null)
213+
else if (cmd.CommandType == ManagementHubCommands.GetStatusSummary && result.Value != null)
210214
{
211215
// got status summary
212-
var val = System.Text.Json.JsonSerializer.Deserialize<StatusSummary>(result.Value);
216+
var val = System.Text.Json.JsonSerializer.Deserialize<StatusSummary>(result.Value, JsonOptions.DefaultJsonSerializerOptions);
213217

214-
_stateProvider.UpdateInstanceStatusSummary(instanceId, val);
218+
_stateProvider.UpdateInstanceStatusSummary(instanceId, val!);
215219
}
216220
else if (result.IsCommandResponse)
217221
{
@@ -220,15 +224,15 @@ private async Task ProcessInstanceCommandResult(InstanceCommandResult result, In
220224
else
221225
{
222226
// item was not requested, queue for processing
223-
if (result.CommandType == ManagementHubCommands.NotificationUpdatedManagedItem)
227+
if (result.CommandType == ManagementHubCommands.NotificationUpdatedManagedItem && result.Value != null)
224228
{
225-
await _uiStatusHub.Clients.All.SendAsync(Providers.StatusHubMessages.SendManagedCertificateUpdateMsg, System.Text.Json.JsonSerializer.Deserialize<Models.ManagedCertificate>(result.Value));
229+
await _uiStatusHub.Clients.All.SendAsync(Providers.StatusHubMessages.SendManagedCertificateUpdateMsg, System.Text.Json.JsonSerializer.Deserialize<Models.ManagedCertificate>(result.Value, JsonOptions.DefaultJsonSerializerOptions));
226230
}
227-
else if (result.CommandType == ManagementHubCommands.NotificationManagedItemRequestProgress)
231+
else if (result.CommandType == ManagementHubCommands.NotificationManagedItemRequestProgress && result.Value != null)
228232
{
229-
await _uiStatusHub.Clients.All.SendAsync(Providers.StatusHubMessages.SendProgressStateMsg, System.Text.Json.JsonSerializer.Deserialize<Models.RequestProgressState>(result.Value));
233+
await _uiStatusHub.Clients.All.SendAsync(Providers.StatusHubMessages.SendProgressStateMsg, System.Text.Json.JsonSerializer.Deserialize<Models.RequestProgressState>(result.Value, JsonOptions.DefaultJsonSerializerOptions));
230234
}
231-
else if (result.CommandType == ManagementHubCommands.NotificationRemovedManagedItem)
235+
else if (result.CommandType == ManagementHubCommands.NotificationRemovedManagedItem && result.Value != null)
232236
{
233237
// deleted :TODO
234238
await _uiStatusHub.Clients.All.SendAsync(Providers.StatusHubMessages.SendMsg, $"Deleted item {result.Value}");
@@ -238,11 +242,10 @@ private async Task ProcessInstanceCommandResult(InstanceCommandResult result, In
238242

239243
private async Task ProcessInstanceInfoResult(InstanceCommandResult result)
240244
{
241-
var instanceInfo = System.Text.Json.JsonSerializer.Deserialize<ManagedInstanceInfo>(result.Value);
245+
var instanceInfo = result.Value == null ? null : System.Text.Json.JsonSerializer.Deserialize<ManagedInstanceInfo>(result.Value, JsonOptions.DefaultJsonSerializerOptions);
242246

243247
if (instanceInfo != null)
244248
{
245-
246249
instanceInfo.LastReported = DateTimeOffset.UtcNow;
247250
_stateProvider.UpdateInstanceConnectionInfo(Context?.ConnectionId ?? _localInstanceId, instanceInfo);
248251

@@ -274,9 +277,13 @@ private async Task ProcessInstanceInfoResult(InstanceCommandResult result)
274277
}
275278
}
276279

280+
/// <summary>
281+
/// Receives a message from an instance and logs the message details.
282+
/// </summary>
283+
/// <param name="message">The message received from the instance.</param>
284+
/// <returns>A task that represents the asynchronous operation.</returns>
277285
public Task ReceiveInstanceMessage(InstanceMessage message)
278286
{
279-
280287
var instanceId = _stateProvider.GetInstanceIdForConnection(Context?.ConnectionId ?? _localInstanceId);
281288
if (instanceId != null)
282289
{

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

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,33 @@ namespace Certify.Server.Hub.Api.SignalR.ManagementHub
88
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
99
public interface IInstanceManagementStateProvider
1010
{
11-
public void Clear();
12-
public void SetManagementHubInstanceId(string instanceId);
13-
public string GetManagementHubInstanceId();
14-
public void UpdateInstanceConnectionInfo(string? connectionId, ManagedInstanceInfo instanceInfo);
15-
public void UpdateInstanceStatusSummary(string instanceId, StatusSummary summary);
16-
public string GetConnectionIdForInstance(string instanceId);
17-
public string GetInstanceIdForConnection(string connectionId);
18-
public List<ManagedInstanceInfo> GetConnectedInstances();
19-
public void AddAwaitedCommandRequest(InstanceCommandRequest command);
20-
public void RemoveAwaitedCommandRequest(Guid commandId);
21-
public InstanceCommandRequest? GetAwaitedCommandRequest(Guid commandId);
22-
public void AddAwaitedCommandResult(InstanceCommandResult result);
11+
void Clear();
12+
void SetManagementHubInstanceId(string instanceId);
13+
string GetManagementHubInstanceId();
14+
void UpdateInstanceConnectionInfo(string connectionId, ManagedInstanceInfo instanceInfo);
15+
void UpdateInstanceStatusSummary(string instanceId, StatusSummary summary);
16+
string GetConnectionIdForInstance(string instanceId);
17+
string? GetInstanceIdForConnection(string connectionId);
18+
List<ManagedInstanceInfo> GetConnectedInstances();
19+
void AddAwaitedCommandRequest(InstanceCommandRequest command);
20+
void RemoveAwaitedCommandRequest(Guid commandId);
21+
InstanceCommandRequest? GetAwaitedCommandRequest(Guid commandId);
22+
void AddAwaitedCommandResult(InstanceCommandResult result);
2323

2424
/// <summary>
2525
/// Wait for a command result to be available
2626
/// </summary>
27-
/// <param name="commandId"></param>
2827
/// <returns></returns>
29-
public Task<InstanceCommandResult?> ConsumeAwaitedCommandResult(InstanceCommandRequest cmd);
30-
public void UpdateInstanceItemInfo(string instanceId, List<ManagedCertificate> items);
31-
public ConcurrentDictionary<string, ManagedInstanceItems> GetManagedInstanceItems(string? instanceId = null);
32-
public void UpdateCachedManagedInstanceItem(string instanceId, ManagedCertificate managedCertificate);
33-
public void DeleteCachedManagedInstanceItem(string instanceId, string managedCertificateId);
34-
public bool HasItemsForManagedInstance(string instanceId);
35-
36-
public bool HasStatusSummaryForManagedInstance(string instanceId);
37-
public ConcurrentDictionary<string, StatusSummary> GetManagedInstanceStatusSummaries();
38-
public void UpdateInstanceConnectionStatus(string instanceId, string status);
28+
Task<InstanceCommandResult?> ConsumeAwaitedCommandResult(InstanceCommandRequest cmd);
29+
void UpdateInstanceItemInfo(string instanceId, List<ManagedCertificate> items);
30+
ConcurrentDictionary<string, ManagedInstanceItems> GetManagedInstanceItems(string? instanceId = null);
31+
void UpdateCachedManagedInstanceItem(string instanceId, ManagedCertificate managedCertificate);
32+
void DeleteCachedManagedInstanceItem(string instanceId, string managedCertificateId);
33+
bool HasItemsForManagedInstance(string instanceId);
34+
35+
bool HasStatusSummaryForManagedInstance(string instanceId);
36+
ConcurrentDictionary<string, StatusSummary> GetManagedInstanceStatusSummaries();
37+
void UpdateInstanceConnectionStatus(string instanceId, string status);
3938
}
4039
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
4140

@@ -327,14 +326,18 @@ public void DeleteCachedManagedInstanceItem(string instanceId, string managedCer
327326
}
328327
}
329328

329+
/// <summary>
330+
/// Update the connection status for a given instance.
331+
/// </summary>
332+
/// <param name="instanceId">The ID of the instance.</param>
333+
/// <param name="status">The new connection status.</param>
330334
public void UpdateInstanceConnectionStatus(string instanceId, string status)
331335
{
332336
var info = _instanceConnections.FirstOrDefault(k => k.Value.InstanceId == instanceId);
333337
if (info.Value != null)
334338
{
335339
info.Value.ConnectionStatus = status;
336340
UpdateInstanceConnectionInfo(info.Key, info.Value);
337-
338341
}
339342
}
340343
}

src/Certify.Server/Certify.Server.HubService/Program.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,11 @@
231231

232232
statusReporting.OnManagedCertificateUpdated += (ManagedCertificate item) =>
233233
{
234-
var mgmtHubState = app.Services.GetRequiredService<IInstanceManagementStateProvider>();
235-
mgmtHubState.UpdateCachedManagedInstanceItem(item.InstanceId, item);
234+
if (item.InstanceId != null)
235+
{
236+
var mgmtHubState = app.Services.GetRequiredService<IInstanceManagementStateProvider>();
237+
mgmtHubState.UpdateCachedManagedInstanceItem(item.InstanceId, item);
238+
}
236239
};
237240

238241
// setup direct management client, this tells the primary backend CertifyManager instance to talk directly to the management hub instead of via SignalR

0 commit comments

Comments
 (0)