Move the Job => Task stuff to the job client

This commit is contained in:
Jordan Brown
2018-08-13 22:36:30 -04:00
parent 879bb6df73
commit 7f3f253f93
6 changed files with 35 additions and 36 deletions
-6
View File
@@ -17,12 +17,6 @@
[Permissions(DenyWrite = true)]
public User CancelledBy { get; set; }
/// <summary>
/// The <see cref="Instance"/> the job belongs to
/// </summary>
[Permissions(DenyWrite = true)]
public Instance Instance { get; set; }
/// <summary>
/// Optional progress between 0 and 100 inclusive
/// </summary>
@@ -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
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param>
/// <returns>A <see cref="Task"/> representing the running operation</returns>
Task Cancel(Job job, CancellationToken cancellationToken);
}
/// <summary>
/// Creates a <see cref="Task{TResult}"/> that completes when a given <paramref name="job"/> is completed
/// </summary>
/// <param name="job">The <see cref="Job"/> to create a <see cref="Task"/> for</param>
/// <param name="requeryRate">The rate in to poll the server for results</param>
/// <param name="progressCallback">A <see cref="Action{T}"/> to run with 0-100 progress</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> which will trigger the cancellation of the <paramref name="job"/></param>
/// <returns>A <see cref="Task{TResult}"/> resulting in a complete <see cref="Job"/></returns>
Task<Job> CreateTaskFromJob(Job job, TimeSpan requeryRate, Action<int> progressCallback, CancellationToken cancellationToken);
}
}
@@ -38,5 +38,26 @@ namespace Tgstation.Server.Client.Components
/// <inheritdoc />
public Task<Job> Read(Job job, CancellationToken cancellationToken) => apiClient.Read<Job>(Routes.SetID(Routes.Jobs, job.Id), instance.Id, cancellationToken);
/// <inheritdoc />
public async Task<Job> CreateTaskFromJob(Job job, TimeSpan requeryRate, Action<int> 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;
}
}
}
@@ -39,14 +39,5 @@ namespace Tgstation.Server.Client
/// The <see cref="System.Version"/> of the <see cref="IServerClient"/>
/// </summary>
Task<Version> Version(CancellationToken cancellationToken);
/// <summary>
/// Creates a <see cref="Task{TResult}"/> that completes when a given <paramref name="job"/> is completed
/// </summary>
/// <param name="job">The <see cref="Job"/> to create a <see cref="Task"/> for</param>
/// <param name="requeryRate">The rate in to poll the server for results</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> which will trigger the cancellation of the <paramref name="job"/></param>
/// <returns>A <see cref="Task{TResult}"/> resulting in a complete <see cref="Job"/></returns>
Task<Job> CreateTaskFromJob(Job job, TimeSpan requeryRate, CancellationToken cancellationToken);
}
}
@@ -53,23 +53,6 @@ namespace Tgstation.Server.Client
/// <inheritdoc />
public void Dispose() => apiClient.Dispose();
/// <inheritdoc />
public async Task<Job> 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;
}
/// <inheritdoc />
public Task<Version> Version(CancellationToken cancellationToken) => apiClient.Read<Version>(Routes.Root, cancellationToken);
}
+1 -2
View File
@@ -34,8 +34,7 @@ namespace Tgstation.Server.Host.Models
CancelRightsType = CancelRightsType,
Description = Description,
ExceptionDetails = ExceptionDetails,
StartedBy = StartedBy.ToApi(),
Instance = Instance.ToApi()
StartedBy = StartedBy.ToApi()
};
}
}