From 40ffe4bf2f4a8f69faeeb7b1000ab1790eb8cc29 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Fri, 4 May 2018 13:48:42 -0400 Subject: [PATCH] Cleanup cleanup --- src/Tgstation.Server.Api/Models/CompileJob.cs | 4 +- .../Models/Internal/CompileJob.cs | 12 --- .../Models/Internal/Job.cs | 13 +++ src/Tgstation.Server.Api/Models/Job.cs | 12 +-- .../Components/DreamMaker.cs | 100 ++++++++---------- .../Controllers/DreamMakerController.cs | 15 +-- src/Tgstation.Server.Host/Core/IJobManager.cs | 4 +- src/Tgstation.Server.Host/Core/JobManager.cs | 9 +- .../Models/CompileJob.cs | 15 ++- 9 files changed, 95 insertions(+), 89 deletions(-) diff --git a/src/Tgstation.Server.Api/Models/CompileJob.cs b/src/Tgstation.Server.Api/Models/CompileJob.cs index 359d7a02df..211ff95951 100644 --- a/src/Tgstation.Server.Api/Models/CompileJob.cs +++ b/src/Tgstation.Server.Api/Models/CompileJob.cs @@ -4,9 +4,9 @@ public sealed class CompileJob : Internal.CompileJob { /// - /// The that triggered the job + /// The relating to this job /// - public User TriggeredBy { get; set; } + public Job Job { get; set; } /// /// Git revision the compiler ran on. Not modifiable diff --git a/src/Tgstation.Server.Api/Models/Internal/CompileJob.cs b/src/Tgstation.Server.Api/Models/Internal/CompileJob.cs index 0833d79e34..7f717c121c 100644 --- a/src/Tgstation.Server.Api/Models/Internal/CompileJob.cs +++ b/src/Tgstation.Server.Api/Models/Internal/CompileJob.cs @@ -13,18 +13,6 @@ namespace Tgstation.Server.Api.Models.Internal /// public long Id { get; set; } - /// - /// When the compilation started - /// - [Required] - public DateTimeOffset StartedAt { get; set; } - - /// - /// When the compilation finished - /// - [Required] - public DateTimeOffset FinishedAt { get; set; } - /// /// If the DMAPI version detected is compatible /// diff --git a/src/Tgstation.Server.Api/Models/Internal/Job.cs b/src/Tgstation.Server.Api/Models/Internal/Job.cs index 205faa7707..1b0104c22d 100644 --- a/src/Tgstation.Server.Api/Models/Internal/Job.cs +++ b/src/Tgstation.Server.Api/Models/Internal/Job.cs @@ -1,4 +1,5 @@ using System; +using Tgstation.Server.Api.Rights; namespace Tgstation.Server.Api.Models.Internal { @@ -43,5 +44,17 @@ namespace Tgstation.Server.Api.Models.Internal /// [Permissions(DenyWrite = true)] public bool Cancelled { get; set; } + + /// + /// The of if it can be cancelled + /// + [Permissions(DenyWrite = true)] + public RightsType? CancelRightsType { get; set; } + + /// + /// The required to cancel the + /// + [Permissions(DenyWrite = true)] + public int? CancelRight { get; set; } } } \ No newline at end of file diff --git a/src/Tgstation.Server.Api/Models/Job.cs b/src/Tgstation.Server.Api/Models/Job.cs index ebbf23bb21..b1141f3400 100644 --- a/src/Tgstation.Server.Api/Models/Job.cs +++ b/src/Tgstation.Server.Api/Models/Job.cs @@ -11,16 +11,16 @@ [Permissions(DenyWrite = true)] public int Progress { get; set; } - /// - /// If the current user has permission to cancel the job - /// - [Permissions(DenyWrite = true)] - public bool UserCanCancel { get; set; } - /// /// The that started the job /// [Permissions(DenyWrite = true)] public User StartedBy { get; set; } + + /// + /// The that cancelled the job + /// + [Permissions(DenyWrite = true)] + public User CancelledBy { get; set; } } } diff --git a/src/Tgstation.Server.Host/Components/DreamMaker.cs b/src/Tgstation.Server.Host/Components/DreamMaker.cs index eecc1b23d2..f43598ccdb 100644 --- a/src/Tgstation.Server.Host/Components/DreamMaker.cs +++ b/src/Tgstation.Server.Host/Components/DreamMaker.cs @@ -194,67 +194,59 @@ namespace Tgstation.Server.Host.Components var job = new Host.Models.CompileJob { DirectoryName = Guid.NewGuid(), - StartedAt = DateTimeOffset.Now, DmeName = dmeName }; - try + await ioManager.CreateDirectory(job.DirectoryName.ToString(), cancellationToken).ConfigureAwait(false); + var dirA = ioManager.ConcatPath(job.DirectoryName.ToString(), ADirectoryName); + var dirB = ioManager.ConcatPath(job.DirectoryName.ToString(), "B"); + + async Task CleanupFailedCompile() { - await ioManager.CreateDirectory(job.DirectoryName.ToString(), cancellationToken).ConfigureAwait(false); - var dirA = ioManager.ConcatPath(job.DirectoryName.ToString(), ADirectoryName); - var dirB = ioManager.ConcatPath(job.DirectoryName.ToString(), "B"); - - async Task CleanupFailedCompile() - { - try - { - await ioManager.DeleteDirectory(job.DirectoryName.ToString(), CancellationToken.None).ConfigureAwait(false); - } - catch { } - }; - try { - //copy the repository - var fullDirA = ioManager.ResolvePath(dirA); - using (repository) - await repository.CopyTo(fullDirA, cancellationToken).ConfigureAwait(false); - - await ModifyDme(job, cancellationToken).ConfigureAwait(false); - - //run compiler, verify api - var ddVerified = await byond.UseExecutables(async (dreamMakerPath, dreamDaemonPath) => - { - await RunDreamMaker(dreamMakerPath, job, cancellationToken).ConfigureAwait(false); - - return job.ExitCode == 0 && await VerifyApi(dreamDaemonPath, job, cancellationToken).ConfigureAwait(false); - }, true).ConfigureAwait(false); - - if (!ddVerified) - //server never validated or compile failed - await CleanupFailedCompile().ConfigureAwait(false); - else - { - job.DMApiValidated = true; - - //duplicate the dmb et al - await ioManager.CopyDirectory(dirA, dirB, null, cancellationToken).ConfigureAwait(false); - - //symlink in the static data - var symATask = configuration.SymlinkStaticFilesTo(fullDirA, cancellationToken); - await configuration.SymlinkStaticFilesTo(ioManager.ResolvePath(dirB), cancellationToken).ConfigureAwait(false); - await symATask.ConfigureAwait(false); - } - return job; + await ioManager.DeleteDirectory(job.DirectoryName.ToString(), CancellationToken.None).ConfigureAwait(false); } - catch - { - await CleanupFailedCompile().ConfigureAwait(false); - throw; - } - } - finally + catch { } + }; + + try { - job.FinishedAt = DateTimeOffset.Now; + //copy the repository + var fullDirA = ioManager.ResolvePath(dirA); + using (repository) + await repository.CopyTo(fullDirA, cancellationToken).ConfigureAwait(false); + + await ModifyDme(job, cancellationToken).ConfigureAwait(false); + + //run compiler, verify api + var ddVerified = await byond.UseExecutables(async (dreamMakerPath, dreamDaemonPath) => + { + await RunDreamMaker(dreamMakerPath, job, cancellationToken).ConfigureAwait(false); + + return job.ExitCode == 0 && await VerifyApi(dreamDaemonPath, job, cancellationToken).ConfigureAwait(false); + }, true).ConfigureAwait(false); + + if (!ddVerified) + //server never validated or compile failed + await CleanupFailedCompile().ConfigureAwait(false); + else + { + job.DMApiValidated = true; + + //duplicate the dmb et al + await ioManager.CopyDirectory(dirA, dirB, null, cancellationToken).ConfigureAwait(false); + + //symlink in the static data + var symATask = configuration.SymlinkStaticFilesTo(fullDirA, cancellationToken); + await configuration.SymlinkStaticFilesTo(ioManager.ResolvePath(dirB), cancellationToken).ConfigureAwait(false); + await symATask.ConfigureAwait(false); + } + return job; + } + catch + { + await CleanupFailedCompile().ConfigureAwait(false); + throw; } } } diff --git a/src/Tgstation.Server.Host/Controllers/DreamMakerController.cs b/src/Tgstation.Server.Host/Controllers/DreamMakerController.cs index 13ce786f5b..6b849d8636 100644 --- a/src/Tgstation.Server.Host/Controllers/DreamMakerController.cs +++ b/src/Tgstation.Server.Host/Controllers/DreamMakerController.cs @@ -40,13 +40,16 @@ namespace Tgstation.Server.Host.Controllers var job = new Job { Description = "Compile active repository code", - StartedBy = AuthenticationContext.User + StartedBy = AuthenticationContext.User, + CancelRightsType = RightsType.DreamMaker, + CancelRight = (int)DreamMakerRights.CancelCompile }; - await jobManager.RegisterOperation(job, (serviceProvider, ct) => RunCompile(serviceProvider, Instance, AuthenticationContext.Clone(), ct), cancellationToken).ConfigureAwait(false); + await jobManager.RegisterOperation(job, (paramJob, serviceProvider, ct) => RunCompile(paramJob, serviceProvider, Instance, ct), cancellationToken).ConfigureAwait(false); return Json(job); } /// + [TgsAuthorize(DreamMakerRights.CancelCompile)] public override async Task Delete([FromBody] Api.Models.CompileJob model, CancellationToken cancellationToken) { //alias for cancelling the latest job @@ -60,12 +63,12 @@ namespace Tgstation.Server.Host.Controllers /// /// Run the compile job and insert it into the database /// + /// The running /// The for the operation /// The for the operation - /// The for the operation /// The for the operation - /// - static async Task RunCompile(IServiceProvider serviceProvider, Models.Instance instanceModel, IAuthenticationContext authenticationContext, CancellationToken cancellationToken) + /// A representing the running operation + static async Task RunCompile(Job job, IServiceProvider serviceProvider, Models.Instance instanceModel, CancellationToken cancellationToken) { var instanceManager = serviceProvider.GetRequiredService(); var databaseContext = serviceProvider.GetRequiredService(); @@ -82,7 +85,7 @@ namespace Tgstation.Server.Host.Controllers compileJob = await instance.DreamMaker.Compile(projectName, repo, cancellationToken).ConfigureAwait(false); } - compileJob.TriggeredBy = authenticationContext.User; + compileJob.Job = job; compileJob.RevisionInformation = await revInfoTask.ConfigureAwait(false); databaseContext.CompileJobs.Add(compileJob); diff --git a/src/Tgstation.Server.Host/Core/IJobManager.cs b/src/Tgstation.Server.Host/Core/IJobManager.cs index ce5798ac3f..0912a64890 100644 --- a/src/Tgstation.Server.Host/Core/IJobManager.cs +++ b/src/Tgstation.Server.Host/Core/IJobManager.cs @@ -15,10 +15,10 @@ namespace Tgstation.Server.Host.Core /// Registers a given and begins running it /// /// The - /// The operation to run + /// The operation to run taking the started , a and a /// The for the operation /// A representing a running operation - Task RegisterOperation(Job job, Func operation, CancellationToken cancellationToken); + Task RegisterOperation(Job job, Func operation, CancellationToken cancellationToken); /// /// Wait for a given to complete diff --git a/src/Tgstation.Server.Host/Core/JobManager.cs b/src/Tgstation.Server.Host/Core/JobManager.cs index 995b544a8f..0676f2cc17 100644 --- a/src/Tgstation.Server.Host/Core/JobManager.cs +++ b/src/Tgstation.Server.Host/Core/JobManager.cs @@ -53,20 +53,21 @@ namespace Tgstation.Server.Host.Core /// The operation for the /// The for the operation /// A representing the running operation - async Task RunJob(Job job, Func operation, CancellationToken cancellationToken) + async Task RunJob(Job job, Func operation, CancellationToken cancellationToken) { using (var scope = serviceProvider.CreateScope()) { IDatabaseContext databaseContext = null; try { + var oldJob = job; + job = new Job { Id = oldJob.Id }; try { - await operation(scope.ServiceProvider, cancellationToken).ConfigureAwait(false); + await operation(job, scope.ServiceProvider, cancellationToken).ConfigureAwait(false); } finally { - job = new Job { Id = job.Id }; databaseContext = scope.ServiceProvider.GetRequiredService(); databaseContext.Jobs.Attach(job); } @@ -85,7 +86,7 @@ namespace Tgstation.Server.Host.Core } /// - public async Task RegisterOperation(Job job, Func operation, CancellationToken cancellationToken) + public async Task RegisterOperation(Job job, Func operation, CancellationToken cancellationToken) { using (var scope = serviceProvider.CreateScope()) { diff --git a/src/Tgstation.Server.Host/Models/CompileJob.cs b/src/Tgstation.Server.Host/Models/CompileJob.cs index 8a34707703..f15efd5859 100644 --- a/src/Tgstation.Server.Host/Models/CompileJob.cs +++ b/src/Tgstation.Server.Host/Models/CompileJob.cs @@ -1,16 +1,25 @@ -namespace Tgstation.Server.Host.Models +using System.ComponentModel.DataAnnotations; + +namespace Tgstation.Server.Host.Models { /// public sealed class CompileJob : Api.Models.Internal.CompileJob { /// - /// See + /// The of /// - public User TriggeredBy { get; set; } + public long JobId { get; set; } + + /// + /// See + /// + [Required] + public Job Job { get; set; } /// /// See /// + [Required] public RevisionInformation RevisionInformation { get; set; } } }