Skip to content

Commit

Permalink
Merge pull request #1698 from tgstation/Day0Fixes [TGSDeploy][NugetDe…
Browse files Browse the repository at this point in the history
…ploy]

v5.17.1 Fixes
  • Loading branch information
Cyberboss authored Nov 7, 2023
2 parents eec92ca + e68dc5b commit 01f38ad
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build/ControlPanelVersion.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- This is in it's own file to help incremental building, changing it causes a complete rebuild of the web panel -->
<TgsControlPanelVersion>4.26.0</TgsControlPanelVersion>
<TgsControlPanelVersion>4.26.2</TgsControlPanelVersion>
</PropertyGroup>
</Project>
6 changes: 3 additions & 3 deletions build/Version.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<!-- Integration tests will ensure they match across the board -->
<Import Project="ControlPanelVersion.props" />
<PropertyGroup>
<TgsCoreVersion>5.17.0</TgsCoreVersion>
<TgsCoreVersion>5.17.1</TgsCoreVersion>
<TgsConfigVersion>4.7.1</TgsConfigVersion>
<TgsApiVersion>9.13.0</TgsApiVersion>
<TgsCommonLibraryVersion>7.0.0</TgsCommonLibraryVersion>
<TgsApiLibraryVersion>12.0.0</TgsApiLibraryVersion>
<TgsClientVersion>14.0.0</TgsClientVersion>
<TgsApiLibraryVersion>12.0.1</TgsApiLibraryVersion>
<TgsClientVersion>14.1.0</TgsClientVersion>
<TgsDmapiVersion>6.6.2</TgsDmapiVersion>
<TgsInteropVersion>5.6.2</TgsInteropVersion>
<TgsHostWatchdogVersion>1.4.0</TgsHostWatchdogVersion>
Expand Down
2 changes: 1 addition & 1 deletion src/Tgstation.Server.Api/Tgstation.Server.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<!-- Usage: HTTP constants reference -->
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.2.0" />
<!-- Usage: Decoding the 'nbf' property of JWTs -->
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="7.0.2" />
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="7.0.3" />
<!-- Usage: Primary JSON library -->
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<!-- Usage: Data model annotating -->
Expand Down
7 changes: 6 additions & 1 deletion src/Tgstation.Server.Client/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,15 @@ public async ValueTask<IAsyncDisposable> CreateHubConnection<THubImplementation>

retryPolicy ??= new InfiniteThirtySecondMaxRetryPolicy();

var wrappedPolicy = new ApiClientTokenRefreshRetryPolicy(this, retryPolicy);

HubConnection? hubConnection = null;
var hubConnectionBuilder = new HubConnectionBuilder()
.AddNewtonsoftJsonProtocol(options =>
{
options.PayloadSerializerSettings = SerializerSettings;
})
.WithAutomaticReconnect(retryPolicy)
.WithAutomaticReconnect(wrappedPolicy)
.WithUrl(
new Uri(Url, Routes.JobsHub),
HttpTransportType.ServerSentEvents,
Expand Down Expand Up @@ -480,6 +482,9 @@ protected virtual async ValueTask<TResult> RunRequest<TResult>(
if (content == null && (method == HttpMethod.Post || method == HttpMethod.Put))
throw new InvalidOperationException("content cannot be null for POST or PUT!");

if (disposed)
throw new ObjectDisposedException(nameof(ApiClient));

HttpResponseMessage response;
var fullUri = new Uri(Url, route);
var serializerSettings = SerializerSettings;
Expand Down
61 changes: 61 additions & 0 deletions src/Tgstation.Server.Client/ApiClientTokenRefreshRetryPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Threading;

using Microsoft.AspNetCore.SignalR.Client;

namespace Tgstation.Server.Client
{
/// <summary>
/// A <see cref="IRetryPolicy"/> that attempts to refresh a given <see cref="apiClient"/>'s token on the first disconnect.
/// </summary>
sealed class ApiClientTokenRefreshRetryPolicy : IRetryPolicy
{
/// <summary>
/// The backing <see cref="ApiClient"/>.
/// </summary>
readonly ApiClient apiClient;

/// <summary>
/// The wrapped <see cref="IRetryPolicy"/>.
/// </summary>
readonly IRetryPolicy wrappedPolicy;

/// <summary>
/// Initializes a new instance of the <see cref="ApiClientTokenRefreshRetryPolicy"/> class.
/// </summary>
/// <param name="apiClient">The value of <see cref="apiClient"/>.</param>
/// <param name="wrappedPolicy">The value of <see cref="wrappedPolicy"/>.</param>
public ApiClientTokenRefreshRetryPolicy(ApiClient apiClient, IRetryPolicy wrappedPolicy)
{
this.apiClient = apiClient ?? throw new ArgumentNullException(nameof(apiClient));
this.wrappedPolicy = wrappedPolicy ?? throw new ArgumentNullException(nameof(wrappedPolicy));
}

/// <inheritdoc />
public TimeSpan? NextRetryDelay(RetryContext retryContext)
{
if (retryContext == null)
throw new ArgumentNullException(nameof(retryContext));

if (retryContext.PreviousRetryCount == 0)
AttemptTokenRefresh();

return wrappedPolicy.NextRetryDelay(retryContext);
}

/// <summary>
/// Attempt to refresh the <see cref="apiClient"/>s token asynchronously.
/// </summary>
async void AttemptTokenRefresh()
{
try
{
await apiClient.RefreshToken(CancellationToken.None);
}
catch
{
// intentionally ignored
}
}
}
}
4 changes: 3 additions & 1 deletion src/Tgstation.Server.Host/Core/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,9 @@ void AddTypedContext<TContext>()
services.AddSingleton<IJobService>(provider => provider.GetRequiredService<JobService>());
services.AddSingleton<IJobsHubUpdater>(provider => provider.GetRequiredService<JobService>());
services.AddSingleton<IJobManager>(x => x.GetRequiredService<IJobService>());
services.AddSingleton<IPermissionsUpdateNotifyee, JobsHubGroupMapper>();
services.AddSingleton<JobsHubGroupMapper>();
services.AddSingleton<IPermissionsUpdateNotifyee>(provider => provider.GetRequiredService<JobsHubGroupMapper>());
services.AddSingleton<IHostedService>(x => x.GetRequiredService<JobsHubGroupMapper>()); // bit of a hack, but we need this to load immediated

services.AddSingleton<InstanceManager>();
services.AddSingleton<IBridgeDispatcher>(x => x.GetRequiredService<InstanceManager>());
Expand Down
9 changes: 8 additions & 1 deletion src/Tgstation.Server.Host/Jobs/JobsHubGroupMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

using Tgstation.Server.Api.Hubs;
Expand All @@ -19,7 +20,7 @@ namespace Tgstation.Server.Host.Jobs
/// <summary>
/// Handles mapping groups for the <see cref="JobsHub"/>.
/// </summary>
sealed class JobsHubGroupMapper : IPermissionsUpdateNotifyee
sealed class JobsHubGroupMapper : IPermissionsUpdateNotifyee, IHostedService
{
/// <summary>
/// The <see cref="IHubContext"/> for the <see cref="JobsHub"/>.
Expand Down Expand Up @@ -96,6 +97,12 @@ public ValueTask InstancePermissionSetDeleted(PermissionSet permissionSet, Cance
cancellationToken);
}

/// <inheritdoc />
public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;

/// <inheritdoc />
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;

/// <summary>
/// Implementation of <see cref="IConnectionMappedHubContext{THub, THubMethods}.OnConnectionMapGroups"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ public ValueTask UserConnected(IAuthenticationContext authenticationContext, THu
userId,
context.ConnectionId);

var mappingTask = OnConnectionMapGroups(
var mappingTask = OnConnectionMapGroups?.Invoke(
authenticationContext,
mappedGroups => Task.WhenAll(
mappedGroups.Select(
group => hub.Groups.AddToGroupAsync(context.ConnectionId, group, cancellationToken))),
cancellationToken);
cancellationToken)
?? ValueTask.CompletedTask;
userConnections.AddOrUpdate(
userId,
_ => new Dictionary<string, HubCallerContext>
Expand Down

0 comments on commit 01f38ad

Please sign in to comment.