From 7f3f253f933f12c64edf1bee6c64d64fadcf9240 Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Mon, 13 Aug 2018 22:36:30 -0400 Subject: [PATCH] Move the Job => Task stuff to the job client --- src/Tgstation.Server.Api/Models/Job.cs | 6 ------ .../Components/IJobsClient.cs | 15 +++++++++++-- .../Components/JobsClient.cs | 21 +++++++++++++++++++ src/Tgstation.Server.Client/IServerClient.cs | 9 -------- src/Tgstation.Server.Client/ServerClient.cs | 17 --------------- src/Tgstation.Server.Host/Models/Job.cs | 3 +-- 6 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/Tgstation.Server.Api/Models/Job.cs b/src/Tgstation.Server.Api/Models/Job.cs index bbb375ca00..3ac1549daa 100644 --- a/src/Tgstation.Server.Api/Models/Job.cs +++ b/src/Tgstation.Server.Api/Models/Job.cs @@ -17,12 +17,6 @@ [Permissions(DenyWrite = true)] public User CancelledBy { get; set; } - /// - /// The the job belongs to - /// - [Permissions(DenyWrite = true)] - public Instance Instance { get; set; } - /// /// Optional progress between 0 and 100 inclusive /// diff --git a/src/Tgstation.Server.Client/Components/IJobsClient.cs b/src/Tgstation.Server.Client/Components/IJobsClient.cs index b35b200462..151dcfc33e 100644 --- a/src/Tgstation.Server.Client/Components/IJobsClient.cs +++ b/src/Tgstation.Server.Client/Components/IJobsClient.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Tgstation.Server.Api.Models; @@ -32,5 +33,15 @@ namespace Tgstation.Server.Client.Components /// The for the operation /// A representing the running operation Task Cancel(Job job, CancellationToken cancellationToken); - } + + /// + /// Creates a that completes when a given is completed + /// + /// The to create a for + /// The rate in to poll the server for results + /// A to run with 0-100 progress + /// The which will trigger the cancellation of the + /// A resulting in a complete + Task CreateTaskFromJob(Job job, TimeSpan requeryRate, Action progressCallback, CancellationToken cancellationToken); + } } diff --git a/src/Tgstation.Server.Client/Components/JobsClient.cs b/src/Tgstation.Server.Client/Components/JobsClient.cs index 283a36a35d..359be4887b 100644 --- a/src/Tgstation.Server.Client/Components/JobsClient.cs +++ b/src/Tgstation.Server.Client/Components/JobsClient.cs @@ -38,5 +38,26 @@ namespace Tgstation.Server.Client.Components /// public Task Read(Job job, CancellationToken cancellationToken) => apiClient.Read(Routes.SetID(Routes.Jobs, job.Id), instance.Id, cancellationToken); + + /// + public async Task CreateTaskFromJob(Job job, TimeSpan requeryRate, Action progressCallback, CancellationToken cancellationToken) + { + if (job == null) + throw new ArgumentNullException(nameof(job)); + + int? lastProgress = null; + while (!job.StoppedAt.HasValue) + { + await Task.Delay(requeryRate, cancellationToken).ConfigureAwait(false); + job = await Read(job, cancellationToken).ConfigureAwait(false); + if(job.Progress.HasValue && job.Progress != lastProgress) + { + progressCallback(job.Progress.Value); + lastProgress = job.Progress; + } + } + return job; + } + } } \ No newline at end of file diff --git a/src/Tgstation.Server.Client/IServerClient.cs b/src/Tgstation.Server.Client/IServerClient.cs index 5766b102a0..3fa1d13666 100644 --- a/src/Tgstation.Server.Client/IServerClient.cs +++ b/src/Tgstation.Server.Client/IServerClient.cs @@ -39,14 +39,5 @@ namespace Tgstation.Server.Client /// The of the /// Task Version(CancellationToken cancellationToken); - - /// - /// Creates a that completes when a given is completed - /// - /// The to create a for - /// The rate in to poll the server for results - /// The which will trigger the cancellation of the - /// A resulting in a complete - Task CreateTaskFromJob(Job job, TimeSpan requeryRate, CancellationToken cancellationToken); } } diff --git a/src/Tgstation.Server.Client/ServerClient.cs b/src/Tgstation.Server.Client/ServerClient.cs index 51b1e971ad..a592c3630d 100644 --- a/src/Tgstation.Server.Client/ServerClient.cs +++ b/src/Tgstation.Server.Client/ServerClient.cs @@ -53,23 +53,6 @@ namespace Tgstation.Server.Client /// public void Dispose() => apiClient.Dispose(); - - /// - public async Task CreateTaskFromJob(Job job, TimeSpan requeryRate, CancellationToken cancellationToken) - { - if (job == null) - throw new ArgumentNullException(nameof(job)); - - var jobsClient = Instances.CreateClient(job.Instance).Jobs; - - while (!job.StoppedAt.HasValue) - { - await Task.Delay(requeryRate, cancellationToken).ConfigureAwait(false); - job = await jobsClient.Read(job, cancellationToken).ConfigureAwait(false); - } - return job; - } - /// public Task Version(CancellationToken cancellationToken) => apiClient.Read(Routes.Root, cancellationToken); } diff --git a/src/Tgstation.Server.Host/Models/Job.cs b/src/Tgstation.Server.Host/Models/Job.cs index 4e9529dc81..0b56cca428 100644 --- a/src/Tgstation.Server.Host/Models/Job.cs +++ b/src/Tgstation.Server.Host/Models/Job.cs @@ -34,8 +34,7 @@ namespace Tgstation.Server.Host.Models CancelRightsType = CancelRightsType, Description = Description, ExceptionDetails = ExceptionDetails, - StartedBy = StartedBy.ToApi(), - Instance = Instance.ToApi() + StartedBy = StartedBy.ToApi() }; } }