mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-27 15:07:03 +01:00
Prevent requests from running until the app is ready.
Removes dead code
This commit is contained in:
@@ -1,22 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using Tgstation.Server.Api.Models.Internal;
|
||||
|
||||
namespace Tgstation.Server.Host.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// For handling <see cref="IInstance"/> shutdowns
|
||||
/// </summary>
|
||||
public interface IInstanceShutdownHandler
|
||||
{
|
||||
//TODO
|
||||
/// <summary>
|
||||
/// OMG
|
||||
/// </summary>
|
||||
/// <param name="launchParameters"></param>
|
||||
/// <param name="accessToken"></param>
|
||||
/// <param name="pid"></param>
|
||||
/// <param name="primary"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> PreserveActiveExecutablesIfNecessary(DreamDaemonLaunchParameters launchParameters, string accessToken, int pid, bool primary);
|
||||
}
|
||||
}
|
||||
@@ -25,14 +25,22 @@ namespace Tgstation.Server.Host.Components
|
||||
/// The <see cref="IInstanceFactory"/> for the <see cref="InstanceManager"/>
|
||||
/// </summary>
|
||||
readonly IInstanceFactory instanceFactory;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="IIOManager"/> for the <see cref="InstanceManager"/>
|
||||
/// </summary>
|
||||
readonly IIOManager ioManager;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="IDatabaseContextFactory"/> for the <see cref="InstanceManager"/>
|
||||
/// </summary>
|
||||
readonly IDatabaseContextFactory databaseContextFactory;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="IApplication"/> for the <see cref="InstanceManager"/>
|
||||
/// </summary>
|
||||
readonly IApplication application;
|
||||
|
||||
/// <summary>
|
||||
/// Map of <see cref="Api.Models.Instance.Id"/>s to respective <see cref="IInstance"/>s
|
||||
/// </summary>
|
||||
@@ -48,11 +56,14 @@ namespace Tgstation.Server.Host.Components
|
||||
/// <param name="instanceFactory">The value of <see cref="instanceFactory"/></param>
|
||||
/// <param name="ioManager">The value of <paramref name="ioManager"/></param>
|
||||
/// <param name="databaseContextFactory">The value of <paramref name="databaseContextFactory"/></param>
|
||||
public InstanceManager(IInstanceFactory instanceFactory, IIOManager ioManager, IDatabaseContextFactory databaseContextFactory)
|
||||
/// <param name="application">The value of <see cref="application"/></param>
|
||||
public InstanceManager(IInstanceFactory instanceFactory, IIOManager ioManager, IDatabaseContextFactory databaseContextFactory, IApplication application)
|
||||
{
|
||||
this.instanceFactory = instanceFactory ?? throw new ArgumentNullException(nameof(instanceFactory));
|
||||
this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager));
|
||||
this.databaseContextFactory = databaseContextFactory ?? throw new ArgumentNullException(nameof(databaseContextFactory));
|
||||
this.application = application ?? throw new ArgumentNullException(nameof(application));
|
||||
|
||||
instances = new Dictionary<long, IInstance>();
|
||||
interopConsumers = new Dictionary<string, IInteropConsumer>();
|
||||
}
|
||||
@@ -129,11 +140,19 @@ namespace Tgstation.Server.Host.Components
|
||||
/// <inheritdoc />
|
||||
public Task StartAsync(CancellationToken cancellationToken) => databaseContextFactory.UseContext(async databaseContext =>
|
||||
{
|
||||
await databaseContext.Initialize(cancellationToken).ConfigureAwait(false);
|
||||
var dbInstances = databaseContext.Instances.Where(x => x.Online.Value).Include(x => x.RepositorySettings).Include(x => x.ChatSettings).Include(x => x.DreamDaemonSettings).ToAsyncEnumerable();
|
||||
var tasks = new List<Task>();
|
||||
await dbInstances.ForEachAsync(metadata => tasks.Add(OnlineInstance(metadata, cancellationToken)), cancellationToken).ConfigureAwait(false);
|
||||
await Task.WhenAll(tasks).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await databaseContext.Initialize(cancellationToken).ConfigureAwait(false);
|
||||
var dbInstances = databaseContext.Instances.Where(x => x.Online.Value).Include(x => x.RepositorySettings).Include(x => x.ChatSettings).Include(x => x.DreamDaemonSettings).ToAsyncEnumerable();
|
||||
var tasks = new List<Task>();
|
||||
await dbInstances.ForEachAsync(metadata => tasks.Add(OnlineInstance(metadata, cancellationToken)), cancellationToken).ConfigureAwait(false);
|
||||
await Task.WhenAll(tasks).ConfigureAwait(false);
|
||||
application.Ready(null);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
application.Ready(e);
|
||||
}
|
||||
});
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -143,16 +162,6 @@ namespace Tgstation.Server.Host.Components
|
||||
instances.Clear();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<bool> PreserveActiveExecutablesIfNecessary(DreamDaemonLaunchParameters launchParameters, string accessToken, int pid, bool primary)
|
||||
{
|
||||
if (launchParameters == null)
|
||||
throw new ArgumentNullException(nameof(launchParameters));
|
||||
if (accessToken == null)
|
||||
throw new ArgumentNullException(nameof(accessToken));
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IInteropContext Register(string accessIdentifier, IInteropConsumer consumer)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Byond.TopicSender;
|
||||
using Cyberboss.AspNetCore.AsyncInitializer;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
@@ -16,6 +17,7 @@ using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using Tgstation.Server.Host.Components;
|
||||
using Tgstation.Server.Host.Components.Chat.Commands;
|
||||
using Tgstation.Server.Host.Components.Watchdog;
|
||||
@@ -54,6 +56,8 @@ namespace Tgstation.Server.Host.Core
|
||||
/// </summary>
|
||||
readonly Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment;
|
||||
|
||||
readonly TaskCompletionSource<object> startupTcs;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="IServerAddressesFeature"/> for the <see cref="Application"/>
|
||||
/// </summary>
|
||||
@@ -69,6 +73,8 @@ namespace Tgstation.Server.Host.Core
|
||||
this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
|
||||
this.hostingEnvironment = hostingEnvironment ?? throw new ArgumentNullException(nameof(hostingEnvironment));
|
||||
|
||||
startupTcs = new TaskCompletionSource<object>();
|
||||
|
||||
Version = Assembly.GetExecutingAssembly().GetName().Version;
|
||||
VersionString = String.Format(CultureInfo.InvariantCulture, "{0} v{1}", VersionPrefix, Version);
|
||||
}
|
||||
@@ -212,9 +218,26 @@ namespace Tgstation.Server.Host.Core
|
||||
|
||||
if (hostingEnvironment.IsDevelopment())
|
||||
applicationBuilder.UseDeveloperExceptionPage();
|
||||
|
||||
|
||||
applicationBuilder.UseAsyncInitialization(async cancellationToken =>
|
||||
{
|
||||
using (cancellationToken.Register(() => startupTcs.SetCanceled()))
|
||||
await startupTcs.Task.ConfigureAwait(false);
|
||||
});
|
||||
|
||||
applicationBuilder.UseAuthentication();
|
||||
applicationBuilder.UseMvc();
|
||||
}
|
||||
|
||||
///<inheritdoc />
|
||||
public void Ready(Exception initializationError)
|
||||
{
|
||||
if (startupTcs.Task.IsCompleted)
|
||||
throw new InvalidOperationException("Ready has already been called!");
|
||||
if (initializationError == null)
|
||||
startupTcs.SetResult(null);
|
||||
else
|
||||
startupTcs.SetException(initializationError);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,5 +21,11 @@ namespace Tgstation.Server.Host.Core
|
||||
/// The url the server can be reached at locally
|
||||
/// </summary>
|
||||
string HostingPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Mark the <see cref="IApplication"/> as ready to run
|
||||
/// </summary>
|
||||
/// <param name="initializationError">The <see cref="Exception"/> that put the application in a corrupted state if any</param>
|
||||
void Ready(Exception initializationError);
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Byond.TopicSender" Version="1.1.1" />
|
||||
<PackageReference Include="Cyberboss.AspNetCore.AsyncInitializer" Version="1.1.0" />
|
||||
<PackageReference Include="Cyberboss.SmartIrc4net.Standard" Version="0.4.5" />
|
||||
<PackageReference Include="Discord.Net.WebSocket" Version="1.0.2" />
|
||||
<PackageReference Include="LibGit2Sharp" Version="0.26.0-preview-0027" />
|
||||
|
||||
Reference in New Issue
Block a user