This commit is contained in:
Jordan Brown
2018-07-19 01:29:42 -04:00
parent 37dab01632
commit d76b2d561f
7 changed files with 94 additions and 49 deletions
@@ -8,7 +8,7 @@ namespace Tgstation.Server.Host.Components
/// <summary>
/// For interacting with the instance services
/// </summary>
public interface IInstance : IHostedService, IReattachInfoHandler
public interface IInstance : IHostedService
{
/// <summary>
/// The <see cref="IRepositoryManager"/> for the <see cref="IInstance"/>
@@ -13,8 +13,7 @@ namespace Tgstation.Server.Host.Components
/// </summary>
/// <param name="metadata">The <see cref="Models.Instance"/></param>
/// <param name="interopRegistrar">The <see cref="IInteropRegistrar"/> for the <see cref="IInstance"/></param>
/// <param name="reattachInfoHandler">The <see cref="IReattachInfoHandler"/> for the <see cref="IInstance"/></param>
/// <returns>A new <see cref="IInstance"/></returns>
IInstance CreateInstance(Models.Instance metadata, IInteropRegistrar interopRegistrar, IReattachInfoHandler reattachInfoHandler);
IInstance CreateInstance(Models.Instance metadata, IInteropRegistrar interopRegistrar);
}
}
@@ -157,46 +157,5 @@ namespace Tgstation.Server.Host.Components
timerTask = TimerLoop(newInterval.Value, timerCts.Token);
}
}
/// <inheritdoc />
public Task Save(WatchdogReattachInformation reattachInformation, CancellationToken cancellationToken) => databaseContextFactory.UseContext(async (db) =>
{
var instance = new Models.Instance { Id = metadata.Id };
db.Instances.Attach(instance);
Models.ReattachInformation ConvertReattachInfo(ReattachInformation wdInfo)
{
db.CompileJobs.Attach(wdInfo.Dmb.CompileJob);
return new Models.ReattachInformation
{
AccessIdentifier = wdInfo.AccessIdentifier,
ChatChannelsJson = wdInfo.ChatChannelsJson,
ChatCommandsJson = wdInfo.ChatCommandsJson,
CompileJob = wdInfo.Dmb.CompileJob,
IsPrimary = wdInfo.IsPrimary,
Port = wdInfo.Port,
ProcessId = wdInfo.ProcessId,
RebootState = wdInfo.RebootState
};
}
instance.WatchdogReattachInformation = new Models.WatchdogReattachInformation
{
Alpha = ConvertReattachInfo(reattachInformation.Alpha),
Bravo = ConvertReattachInfo(reattachInformation.Bravo),
AlphaIsActive = reattachInformation.AlphaIsActive,
};
await db.Save(cancellationToken).ConfigureAwait(false);
});
/// <inheritdoc />
public async Task<WatchdogReattachInformation> Load(CancellationToken cancellationToken)
{
Models.WatchdogReattachInformation result = null;
await databaseContextFactory.UseContext(async (db) =>
result = await db.Instances.Where(x => x.Id == metadata.Id).Select(x => x.WatchdogReattachInformation).FirstAsync(cancellationToken).ConfigureAwait(false)
).ConfigureAwait(false);
return new WatchdogReattachInformation(result, dmbFactory);
}
}
}
@@ -56,7 +56,7 @@ namespace Tgstation.Server.Host.Components
/// The <see cref="ICommandFactory"/> for the <see cref="InstanceFactory"/>
/// </summary>
readonly ICommandFactory commandFactory;
/// <summary>
/// Construct an <see cref="InstanceFactory"/>
/// </summary>
@@ -64,23 +64,26 @@ namespace Tgstation.Server.Host.Components
/// <param name="databaseContextFactory">The value of <see cref="databaseContextFactory"/></param>
/// <param name="application">The value of <see cref="application"/></param>
/// <param name="loggerFactory">The value of <see cref="loggerFactory"/></param>
/// <param name="byondTopicSender">The value of <see cref="byondTopicSender"/></param>
/// <param name="serverUpdater">The value of <see cref="serverUpdater"/></param>
/// <param name="cryptographySuite">The value of <see cref="cryptographySuite"/></param>
/// <param name="executor">The value of <see cref="executor"/></param>
/// <param name="commandFactory">The value of <see cref="commandFactory"/></param>
public InstanceFactory(IIOManager ioManager, IDatabaseContextFactory databaseContextFactory, IApplication application, ILoggerFactory loggerFactory, IServerUpdater serverUpdater, ICryptographySuite cryptographySuite, IExecutor executor, ICommandFactory commandFactory)
public InstanceFactory(IIOManager ioManager, IDatabaseContextFactory databaseContextFactory, IApplication application, ILoggerFactory loggerFactory, IByondTopicSender byondTopicSender, IServerUpdater serverUpdater, ICryptographySuite cryptographySuite, IExecutor executor, ICommandFactory commandFactory)
{
this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager));
this.databaseContextFactory = databaseContextFactory ?? throw new ArgumentNullException(nameof(databaseContextFactory));
this.application = application ?? throw new ArgumentNullException(nameof(application));
this.loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
this.byondTopicSender = byondTopicSender ?? throw new ArgumentNullException(nameof(byondTopicSender));
this.serverUpdater = serverUpdater ?? throw new ArgumentNullException(nameof(serverUpdater));
this.cryptographySuite = cryptographySuite ?? throw new ArgumentNullException(nameof(cryptographySuite ));
this.executor = executor ?? throw new ArgumentNullException(nameof(executor));
this.commandFactory = commandFactory ?? throw new ArgumentNullException(nameof(commandFactory));
}
/// <inheritdoc />
public IInstance CreateInstance(Models.Instance metadata, IInteropRegistrar interopRegistrar, IReattachInfoHandler reattachInfoHandler)
public IInstance CreateInstance(Models.Instance metadata, IInteropRegistrar interopRegistrar)
{
//Create the ioManager for the instance
var instanceIoManager = new ResolvingIOManager(ioManager, metadata.Path);
@@ -103,6 +106,7 @@ namespace Tgstation.Server.Host.Components
var chat = chatFactory.CreateChat();
var sessionControllerFactory = new SessionControllerFactory(executor, byond, byondTopicSender, interopRegistrar, cryptographySuite, application, gameIoManager, chat, loggerFactory, metadata);
var reattachInfoHandler = new ReattachInfoHandler(databaseContextFactory, dmbFactory, metadata);
var watchdogFactory = new WatchdogFactory(chat, sessionControllerFactory, serverUpdater, loggerFactory, reattachInfoHandler, databaseContextFactory, byondTopicSender, metadata);
var watchdog = watchdogFactory.CreateWatchdog(dmbFactory, metadata.DreamDaemonSettings);
@@ -115,7 +115,7 @@ namespace Tgstation.Server.Host.Components
{
if (metadata == null)
throw new ArgumentNullException(nameof(metadata));
var instance = instanceFactory.CreateInstance(metadata);
var instance = instanceFactory.CreateInstance(metadata, this);
lock (this)
{
if (instances.ContainsKey(metadata.Id))
@@ -0,0 +1,83 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Tgstation.Server.Host.Components.Watchdog;
using Tgstation.Server.Host.Core;
namespace Tgstation.Server.Host.Components
{
/// <inheritdoc />
sealed class ReattachInfoHandler: IReattachInfoHandler
{
/// <summary>
/// The <see cref="IDatabaseContextFactory"/> for the <see cref="ReattachInfoHandler"/>
/// </summary>
readonly IDatabaseContextFactory databaseContextFactory;
/// <summary>
/// The <see cref="IDmbFactory"/> for the <see cref="ReattachInfoHandler"/>
/// </summary>
readonly IDmbFactory dmbFactory;
/// <summary>
/// The <see cref="Models.Instance"/> for the <see cref="ReattachInfoHandler"/>
/// </summary>
readonly Models.Instance metadata;
/// <summary>
/// Construct a <see cref="ReattachInfoHandler"/>
/// </summary>
/// <param name="databaseContextFactory">The value of <see cref="databaseContextFactory"/></param>
/// <param name="dmbFactory">The value of <see cref="dmbFactory"/></param>
/// <param name="metadata">The value of <see cref="metadata"/></param>
public ReattachInfoHandler(IDatabaseContextFactory databaseContextFactory, IDmbFactory dmbFactory, Models.Instance metadata)
{
this.databaseContextFactory = databaseContextFactory ?? throw new ArgumentNullException(nameof(databaseContextFactory));
this.dmbFactory = dmbFactory ?? throw new ArgumentNullException(nameof(dmbFactory));
this.metadata = metadata ?? throw new ArgumentNullException(nameof(metadata));
}
/// <inheritdoc />
public Task Save(WatchdogReattachInformation reattachInformation, CancellationToken cancellationToken) => databaseContextFactory.UseContext(async (db) =>
{
var instance = new Models.Instance { Id = metadata.Id };
db.Instances.Attach(instance);
Models.ReattachInformation ConvertReattachInfo(ReattachInformation wdInfo)
{
db.CompileJobs.Attach(wdInfo.Dmb.CompileJob);
return new Models.ReattachInformation
{
AccessIdentifier = wdInfo.AccessIdentifier,
ChatChannelsJson = wdInfo.ChatChannelsJson,
ChatCommandsJson = wdInfo.ChatCommandsJson,
CompileJob = wdInfo.Dmb.CompileJob,
IsPrimary = wdInfo.IsPrimary,
Port = wdInfo.Port,
ProcessId = wdInfo.ProcessId,
RebootState = wdInfo.RebootState
};
}
instance.WatchdogReattachInformation = new Models.WatchdogReattachInformation
{
Alpha = ConvertReattachInfo(reattachInformation.Alpha),
Bravo = ConvertReattachInfo(reattachInformation.Bravo),
AlphaIsActive = reattachInformation.AlphaIsActive,
};
await db.Save(cancellationToken).ConfigureAwait(false);
});
/// <inheritdoc />
public async Task<WatchdogReattachInformation> Load(CancellationToken cancellationToken)
{
Models.WatchdogReattachInformation result = null;
await databaseContextFactory.UseContext(async (db) =>
result = await db.Instances.Where(x => x.Id == metadata.Id).Select(x => x.WatchdogReattachInformation).FirstAsync(cancellationToken).ConfigureAwait(false)
).ConfigureAwait(false);
return new WatchdogReattachInformation(result, dmbFactory);
}
}
}
@@ -148,7 +148,7 @@ namespace Tgstation.Server.Host.Core
services.AddSingleton<IPasswordHasher<Models.User>, PasswordHasher<Models.User>>();
services.AddSingleton<ITokenFactory, TokenFactory>();
services.AddSingleton<ISystemIdentityFactory, SystemIdentityFactory>();
services.AddSingleton<IExecutor, Executor>();
services.AddSingleton<ICommandFactory, CommandFactory>();
services.AddSingleton<IByondTopicSender>(new ByondTopicSender