diff --git a/src/Tgstation.Server.Host/Components/IInstanceFactory.cs b/src/Tgstation.Server.Host/Components/IInstanceFactory.cs index 5e4e3934f8..d9d4f4cda8 100644 --- a/src/Tgstation.Server.Host/Components/IInstanceFactory.cs +++ b/src/Tgstation.Server.Host/Components/IInstanceFactory.cs @@ -11,8 +11,7 @@ namespace Tgstation.Server.Host.Components /// Create an /// /// The - /// The for the operation /// A new - IInstance CreateInstance(Host.Models.Instance metadata, IDatabaseContextFactory databaseContextFactory); + IInstance CreateInstance(Host.Models.Instance metadata); } } \ No newline at end of file diff --git a/src/Tgstation.Server.Host/Components/IInstanceManager.cs b/src/Tgstation.Server.Host/Components/IInstanceManager.cs index c087ad362a..d26d87489b 100644 --- a/src/Tgstation.Server.Host/Components/IInstanceManager.cs +++ b/src/Tgstation.Server.Host/Components/IInstanceManager.cs @@ -20,10 +20,9 @@ namespace Tgstation.Server.Host.Components /// Online an /// /// The of the desired - /// The for the operation /// The for the operation /// A representing the running operation - Task OnlineInstance(Host.Models.Instance metadata, IDatabaseContext databaseContext, CancellationToken cancellationToken); + Task OnlineInstance(Host.Models.Instance metadata, CancellationToken cancellationToken); /// /// Offline an @@ -37,10 +36,9 @@ namespace Tgstation.Server.Host.Components /// Move an /// /// The of the desired - /// The for the operation /// The new path of the . will have this set on if the operation completes successfully /// The for the operation /// A representing the running operation - Task MoveInstance(Host.Models.Instance metadata, IDatabaseContext databaseContext, string newPath, CancellationToken cancellationToken); + Task MoveInstance(Host.Models.Instance metadata, string newPath, CancellationToken cancellationToken); } } diff --git a/src/Tgstation.Server.Host/Components/IRepository.cs b/src/Tgstation.Server.Host/Components/IRepository.cs index 7acd5053df..09acbb72c5 100644 --- a/src/Tgstation.Server.Host/Components/IRepository.cs +++ b/src/Tgstation.Server.Host/Components/IRepository.cs @@ -8,7 +8,7 @@ namespace Tgstation.Server.Host.Components /// /// Represents an on-disk git repository /// - interface IRepository : IDisposable + public interface IRepository : IDisposable { /// /// If the was cloned from GitHub.com diff --git a/src/Tgstation.Server.Host/Components/IRepositoryManager.cs b/src/Tgstation.Server.Host/Components/IRepositoryManager.cs index f7db73a8d7..5f39b45733 100644 --- a/src/Tgstation.Server.Host/Components/IRepositoryManager.cs +++ b/src/Tgstation.Server.Host/Components/IRepositoryManager.cs @@ -1,5 +1,4 @@ -using Microsoft.Extensions.Hosting; -using System.Threading; +using System.Threading; using System.Threading.Tasks; namespace Tgstation.Server.Host.Components diff --git a/src/Tgstation.Server.Host/Components/InstanceFactory.cs b/src/Tgstation.Server.Host/Components/InstanceFactory.cs index 00767fcabd..62112fac9b 100644 --- a/src/Tgstation.Server.Host/Components/InstanceFactory.cs +++ b/src/Tgstation.Server.Host/Components/InstanceFactory.cs @@ -11,14 +11,24 @@ namespace Tgstation.Server.Host.Components /// readonly IIOManager ioManager; + /// + /// The for the + /// + readonly IDatabaseContextFactory databaseContextFactory; + /// /// Construct an /// /// The value of - public InstanceFactory(IIOManager ioManager) => this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager)); + /// The value of + public InstanceFactory(IIOManager ioManager, IDatabaseContextFactory databaseContextFactory) + { + this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager)); + this.databaseContextFactory = databaseContextFactory ?? throw new ArgumentNullException(nameof(databaseContextFactory)); + } /// - public IInstance CreateInstance(Host.Models.Instance metadata, IDatabaseContextFactory databaseContextFactory) + public IInstance CreateInstance(Host.Models.Instance metadata) { //Create the ioManager for the instance diff --git a/src/Tgstation.Server.Host/Components/InstanceManager.cs b/src/Tgstation.Server.Host/Components/InstanceManager.cs index 1b01e2b8e9..3d1a615f2c 100644 --- a/src/Tgstation.Server.Host/Components/InstanceManager.cs +++ b/src/Tgstation.Server.Host/Components/InstanceManager.cs @@ -8,7 +8,6 @@ using System.Threading; using System.Threading.Tasks; using Tgstation.Server.Api.Models.Internal; using Tgstation.Server.Host.Core; -using Tgstation.Server.Host.Models; namespace Tgstation.Server.Host.Components { @@ -16,18 +15,18 @@ namespace Tgstation.Server.Host.Components sealed class InstanceManager : IInstanceManager, IHostedService { /// - /// The for the + /// The for the /// readonly IInstanceFactory instanceFactory; /// - /// The for the - /// - readonly IServiceProvider serviceProvider; - /// - /// The for the + /// The for the /// readonly IIOManager ioManager; /// + /// The for the + /// + readonly IDatabaseContextFactory databaseContextFactory; + /// /// Map of s to respective s /// readonly Dictionary instances; @@ -36,13 +35,13 @@ namespace Tgstation.Server.Host.Components /// Construct an /// /// The value of - /// The value of /// The value of - public InstanceManager(IInstanceFactory instanceFactory, IServiceProvider serviceProvider, IIOManager ioManager) + /// The value of + public InstanceManager(IInstanceFactory instanceFactory, IIOManager ioManager, IDatabaseContextFactory databaseContextFactory) { this.instanceFactory = instanceFactory ?? throw new ArgumentNullException(nameof(instanceFactory)); - this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager)); + this.databaseContextFactory = databaseContextFactory ?? throw new ArgumentNullException(nameof(databaseContextFactory)); instances = new Dictionary(); } @@ -60,7 +59,7 @@ namespace Tgstation.Server.Host.Components } /// - public async Task MoveInstance(Host.Models.Instance instance, IDatabaseContext databaseContext, string newPath, CancellationToken cancellationToken) + public async Task MoveInstance(Host.Models.Instance instance, string newPath, CancellationToken cancellationToken) { if (newPath == null) throw new ArgumentNullException(nameof(newPath)); @@ -72,14 +71,14 @@ namespace Tgstation.Server.Host.Components var oldPath = instance.Path; await ioManager.CopyDirectory(oldPath, newPath, null, cancellationToken).ConfigureAwait(false); instance.Path = ioManager.ResolvePath(newPath); - instanceOnlineTask = OnlineInstance(instance, databaseContext, default); + instanceOnlineTask = OnlineInstance(instance, default); await ioManager.DeleteDirectory(oldPath, cancellationToken).ConfigureAwait(false); } finally { if (instance.Online) if (instanceOnlineTask == null) - await OnlineInstance(instance, databaseContext, default).ConfigureAwait(false); + await OnlineInstance(instance, default).ConfigureAwait(false); else await instanceOnlineTask.ConfigureAwait(false); } @@ -101,11 +100,11 @@ namespace Tgstation.Server.Host.Components } /// - public async Task OnlineInstance(Host.Models.Instance metadata, IDatabaseContext databaseContext, CancellationToken cancellationToken) + public async Task OnlineInstance(Host.Models.Instance metadata, CancellationToken cancellationToken) { if (metadata == null) throw new ArgumentNullException(nameof(metadata)); - var instance = instanceFactory.CreateInstance(metadata, databaseContext); + var instance = instanceFactory.CreateInstance(metadata); lock (this) { if (instances.ContainsKey(metadata.Id)) @@ -116,18 +115,14 @@ namespace Tgstation.Server.Host.Components } /// - public async Task StartAsync(CancellationToken cancellationToken) + public Task StartAsync(CancellationToken cancellationToken) => databaseContextFactory.UseContext(async databaseContext => { - using(var scope = serviceProvider.CreateScope()) - { - var databaseContext = scope.ServiceProvider.GetRequiredService(); - await databaseContext.Initialize(cancellationToken).ConfigureAwait(false); - var dbInstances = databaseContext.Instances.Where(x => x.Online).Include(x => x.RepositorySettings).Include(x => x.ChatSettings).Include(x => x.DreamDaemonSettings).ToAsyncEnumerable(); - var tasks = new List(); - await dbInstances.ForEachAsync(metadata => tasks.Add(OnlineInstance(metadata, databaseContext, cancellationToken)), cancellationToken).ConfigureAwait(false); - await Task.WhenAll(tasks).ConfigureAwait(false); - } - } + await databaseContext.Initialize(cancellationToken).ConfigureAwait(false); + var dbInstances = databaseContext.Instances.Where(x => x.Online).Include(x => x.RepositorySettings).Include(x => x.ChatSettings).Include(x => x.DreamDaemonSettings).ToAsyncEnumerable(); + var tasks = new List(); + await dbInstances.ForEachAsync(metadata => tasks.Add(OnlineInstance(metadata, cancellationToken)), cancellationToken).ConfigureAwait(false); + await Task.WhenAll(tasks).ConfigureAwait(false); + }); /// public async Task StopAsync(CancellationToken cancellationToken) diff --git a/src/Tgstation.Server.Host/Controllers/DreamMakerController.cs b/src/Tgstation.Server.Host/Controllers/DreamMakerController.cs index 56586f7bfd..fd39fdc931 100644 --- a/src/Tgstation.Server.Host/Controllers/DreamMakerController.cs +++ b/src/Tgstation.Server.Host/Controllers/DreamMakerController.cs @@ -18,7 +18,7 @@ namespace Tgstation.Server.Host.Controllers /// Controller for managing the compiler /// [Route("/DreamMaker")] - public sealed class DreamMakerController : ModelController + public sealed class DreamMakerController : ModelController { /// /// The for the @@ -38,7 +38,7 @@ namespace Tgstation.Server.Host.Controllers /// [TgsAuthorize(DreamMakerRights.Compile)] - public override async Task Create([FromBody] Api.Models.CompileJob model, CancellationToken cancellationToken) + public override async Task Create([FromBody] Api.Models.DreamMaker model, CancellationToken cancellationToken) { var job = new Job { @@ -53,16 +53,30 @@ namespace Tgstation.Server.Host.Controllers /// [TgsAuthorize(DreamMakerRights.CancelCompile)] - public override async Task Delete([FromBody] Api.Models.CompileJob model, CancellationToken cancellationToken) + public override async Task Delete([FromBody] Api.Models.DreamMaker model, CancellationToken cancellationToken) { //alias for cancelling the latest job - var job = await DatabaseContext.Jobs.OrderByDescending(x => x.StartedAt).Select(x => new Job { Id = x.Id, StoppedAt = x.StoppedAt }).FirstAsync(cancellationToken).ConfigureAwait(false); + var job = await DatabaseContext.CompileJobs.OrderByDescending(x => x.Job.StartedAt).Select(x => new Job { Id = x.Job.Id, StoppedAt = x.Job.StoppedAt }).FirstAsync(cancellationToken).ConfigureAwait(false); if (job.StoppedAt != null) return StatusCode(HttpStatusCode.Gone); jobManager.CancelJob(job); return Ok(); } + /// + [TgsAuthorize(DreamMakerRights.SetDme)] + public override async Task Update([FromBody] Api.Models.DreamMaker model, CancellationToken cancellationToken) + { + var hostModel = new DreamMakerSettings + { + InstanceId = Instance.Id + }; + DatabaseContext.DreamMakerSettings.Attach(hostModel); + hostModel.ProjectName = model.ProjectName; + await DatabaseContext.Save(cancellationToken).ConfigureAwait(false); + return Ok(); + } + /// /// Run the compile job and insert it into the database /// diff --git a/src/Tgstation.Server.Host/Models/DatabaseContext.cs b/src/Tgstation.Server.Host/Models/DatabaseContext.cs index 3d848add3e..b97991c636 100644 --- a/src/Tgstation.Server.Host/Models/DatabaseContext.cs +++ b/src/Tgstation.Server.Host/Models/DatabaseContext.cs @@ -28,6 +28,9 @@ namespace Tgstation.Server.Host.Models /// public DbSet RevisionInformations { get; set; } + /// + public DbSet DreamMakerSettings { get; set; } + /// /// The for s /// @@ -50,10 +53,6 @@ namespace Tgstation.Server.Host.Models /// public DbSet DreamDaemonSettings { get; set; } /// - /// The in the - /// - public DbSet DreamMakerSettings { get; set; } - /// /// The s in the /// public DbSet Jobs { get; set; } diff --git a/src/Tgstation.Server.Host/Models/IDatabaseContext.cs b/src/Tgstation.Server.Host/Models/IDatabaseContext.cs index da1bea8f12..d027884bc4 100644 --- a/src/Tgstation.Server.Host/Models/IDatabaseContext.cs +++ b/src/Tgstation.Server.Host/Models/IDatabaseContext.cs @@ -34,6 +34,11 @@ namespace Tgstation.Server.Host.Models /// DbSet RevisionInformations { get; } + /// + /// The in the + /// + DbSet DreamMakerSettings { get; set; } + /// /// Get the in the ///