From d76b2d561fb5cdd21b5a74c2f29807e51958a429 Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Thu, 19 Jul 2018 01:29:42 -0400 Subject: [PATCH] Stufff --- .../Components/IInstance.cs | 2 +- .../Components/IInstanceFactory.cs | 3 +- .../Components/Instance.cs | 41 --------- .../Components/InstanceFactory.cs | 10 ++- .../Components/InstanceManager.cs | 2 +- .../Components/ReattachInfoHandler.cs | 83 +++++++++++++++++++ src/Tgstation.Server.Host/Core/Application.cs | 2 +- 7 files changed, 94 insertions(+), 49 deletions(-) create mode 100644 src/Tgstation.Server.Host/Components/ReattachInfoHandler.cs diff --git a/src/Tgstation.Server.Host/Components/IInstance.cs b/src/Tgstation.Server.Host/Components/IInstance.cs index 54cd4468d6..9868430342 100644 --- a/src/Tgstation.Server.Host/Components/IInstance.cs +++ b/src/Tgstation.Server.Host/Components/IInstance.cs @@ -8,7 +8,7 @@ namespace Tgstation.Server.Host.Components /// /// For interacting with the instance services /// - public interface IInstance : IHostedService, IReattachInfoHandler + public interface IInstance : IHostedService { /// /// The for the diff --git a/src/Tgstation.Server.Host/Components/IInstanceFactory.cs b/src/Tgstation.Server.Host/Components/IInstanceFactory.cs index 600a71ecc0..ea9ed59f44 100644 --- a/src/Tgstation.Server.Host/Components/IInstanceFactory.cs +++ b/src/Tgstation.Server.Host/Components/IInstanceFactory.cs @@ -13,8 +13,7 @@ namespace Tgstation.Server.Host.Components /// /// The /// The for the - /// The for the /// A new - IInstance CreateInstance(Models.Instance metadata, IInteropRegistrar interopRegistrar, IReattachInfoHandler reattachInfoHandler); + IInstance CreateInstance(Models.Instance metadata, IInteropRegistrar interopRegistrar); } } \ No newline at end of file diff --git a/src/Tgstation.Server.Host/Components/Instance.cs b/src/Tgstation.Server.Host/Components/Instance.cs index b97f722e0c..d607053ab2 100644 --- a/src/Tgstation.Server.Host/Components/Instance.cs +++ b/src/Tgstation.Server.Host/Components/Instance.cs @@ -157,46 +157,5 @@ namespace Tgstation.Server.Host.Components timerTask = TimerLoop(newInterval.Value, timerCts.Token); } } - - /// - 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); - }); - - /// - public async Task 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); - } } } diff --git a/src/Tgstation.Server.Host/Components/InstanceFactory.cs b/src/Tgstation.Server.Host/Components/InstanceFactory.cs index e31d403ace..efbefbe737 100644 --- a/src/Tgstation.Server.Host/Components/InstanceFactory.cs +++ b/src/Tgstation.Server.Host/Components/InstanceFactory.cs @@ -56,7 +56,7 @@ namespace Tgstation.Server.Host.Components /// The for the /// readonly ICommandFactory commandFactory; - + /// /// Construct an /// @@ -64,23 +64,26 @@ namespace Tgstation.Server.Host.Components /// The value of /// The value of /// The value of + /// The value of /// The value of /// The value of /// The value of /// The value of - 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)); } /// - 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); diff --git a/src/Tgstation.Server.Host/Components/InstanceManager.cs b/src/Tgstation.Server.Host/Components/InstanceManager.cs index ff78b157b8..24dd107c18 100644 --- a/src/Tgstation.Server.Host/Components/InstanceManager.cs +++ b/src/Tgstation.Server.Host/Components/InstanceManager.cs @@ -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)) diff --git a/src/Tgstation.Server.Host/Components/ReattachInfoHandler.cs b/src/Tgstation.Server.Host/Components/ReattachInfoHandler.cs new file mode 100644 index 0000000000..93c7e6aa91 --- /dev/null +++ b/src/Tgstation.Server.Host/Components/ReattachInfoHandler.cs @@ -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 +{ + /// + sealed class ReattachInfoHandler: IReattachInfoHandler + { + /// + /// The for the + /// + readonly IDatabaseContextFactory databaseContextFactory; + + /// + /// The for the + /// + readonly IDmbFactory dmbFactory; + + /// + /// The for the + /// + readonly Models.Instance metadata; + + /// + /// Construct a + /// + /// The value of + /// The value of + /// The value of + 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)); + } + + /// + 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); + }); + + /// + public async Task 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); + } + } +} diff --git a/src/Tgstation.Server.Host/Core/Application.cs b/src/Tgstation.Server.Host/Core/Application.cs index 3395876d70..ae4489d387 100644 --- a/src/Tgstation.Server.Host/Core/Application.cs +++ b/src/Tgstation.Server.Host/Core/Application.cs @@ -148,7 +148,7 @@ namespace Tgstation.Server.Host.Core services.AddSingleton, PasswordHasher>(); services.AddSingleton(); services.AddSingleton(); - + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(new ByondTopicSender