Skip to content

Commit

Permalink
More support for cancellation tokens (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-richardson authored Jun 19, 2023
1 parent 07a80c4 commit 1e047d1
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8527,26 +8527,47 @@ Octopus.Client.Repositories.Async
Octopus.Client.Repositories.Async.ICanExtendSpaceContext<ITaskRepository>
{
Task Cancel(Octopus.Client.Model.TaskResource)
Task Cancel(Octopus.Client.Model.TaskResource, CancellationToken)
Task<TaskResource> ExecuteActionTemplate(Octopus.Client.Model.ActionTemplateResource, Dictionary<String, PropertyValueResource>, String[], String[], String[], String, Nullable<TargetType>)
Task<TaskResource> ExecuteActionTemplate(Octopus.Client.Model.ActionTemplateResource, Dictionary<String, PropertyValueResource>, CancellationToken, String[], String[], String[], String, Nullable<TargetType>)
Task<TaskResource> ExecuteAdHocScript(String, String[], String[], String[], String, String, Nullable<TargetType>)
Task<TaskResource> ExecuteAdHocScript(String, CancellationToken, String[], String[], String[], String, String, Nullable<TargetType>)
Task<TaskResource> ExecuteBackup(String)
Task<TaskResource> ExecuteBackup(CancellationToken, String)
Task<TaskResource> ExecuteCalamariUpdate(String, String[])
Task<TaskResource> ExecuteCalamariUpdate(CancellationToken, String, String[])
Task<TaskResource> ExecuteCommunityActionTemplatesSynchronisation(String)
Task<TaskResource> ExecuteCommunityActionTemplatesSynchronisation(CancellationToken, String)
Task<TaskResource> ExecuteHealthCheck(String, Int32, Int32, String, String[], String, String, String[])
Task<TaskResource> ExecuteHealthCheck(CancellationToken, String, Int32, Int32, String, String[], String, String, String[])
Task<TaskResource> ExecuteTentacleUpgrade(String, String, String[], String, String, String[])
Task<TaskResource> ExecuteTentacleUpgrade(CancellationToken, String, String, String[], String, String, String[])
Task<TaskResourceCollection> GetActiveWithSummary(Int32, Int32)
Task<TaskResourceCollection> GetActiveWithSummary(CancellationToken, Int32, Int32)
Task<List<TaskResource>> GetAllActive(Int32)
Task<List<TaskResource>> GetAllActive(CancellationToken, Int32)
Task<TaskResourceCollection> GetAllWithSummary(Int32, Int32)
Task<TaskResourceCollection> GetAllWithSummary(CancellationToken, Int32, Int32)
Task<TaskDetailsResource> GetDetails(Octopus.Client.Model.TaskResource, Nullable<Boolean>, Nullable<Int32>)
Task<TaskDetailsResource> GetDetails(Octopus.Client.Model.TaskResource, CancellationToken, Nullable<Boolean>, Nullable<Int32>)
Task<IReadOnlyList<TaskResource>> GetQueuedBehindTasks(Octopus.Client.Model.TaskResource)
Task<IReadOnlyList<TaskResource>> GetQueuedBehindTasks(Octopus.Client.Model.TaskResource, CancellationToken)
Task<String> GetRawOutputLog(Octopus.Client.Model.TaskResource)
Task<String> GetRawOutputLog(Octopus.Client.Model.TaskResource, CancellationToken)
Task<TaskTypeResource[]> GetTaskTypes()
Task<TaskTypeResource[]> GetTaskTypes(CancellationToken)
Task ModifyState(Octopus.Client.Model.TaskResource, Octopus.Client.Model.TaskState, String)
Task ModifyState(Octopus.Client.Model.TaskResource, Octopus.Client.Model.TaskState, String, CancellationToken)
Task Rerun(Octopus.Client.Model.TaskResource)
Task Rerun(Octopus.Client.Model.TaskResource, CancellationToken)
Task WaitForCompletion(Octopus.Client.Model.TaskResource, Int32, Int32, Action<TaskResource[]>)
Task WaitForCompletion(Octopus.Client.Model.TaskResource, CancellationToken, Int32, Int32, Action<TaskResource[], CancellationToken>)
Task WaitForCompletion(Octopus.Client.Model.TaskResource[], Int32, Int32, Action<TaskResource[]>)
Task WaitForCompletion(Octopus.Client.Model.TaskResource[], CancellationToken, Int32, Int32, Action<TaskResource[], CancellationToken>)
Task WaitForCompletion(Octopus.Client.Model.TaskResource[], Int32, Int32, Func<TaskResource[], Task>)
Task WaitForCompletion(Octopus.Client.Model.TaskResource[], CancellationToken, Int32, Int32, Func<TaskResource[], CancellationToken, Task>)
Task WaitForCompletion(Octopus.Client.Model.TaskResource[], Int32, Nullable<TimeSpan>, Func<TaskResource[], Task>)
Task WaitForCompletion(Octopus.Client.Model.TaskResource[], CancellationToken, Int32, Nullable<TimeSpan>, Func<TaskResource[], CancellationToken, Task>)
}
interface ITeamsRepository
Octopus.Client.Repositories.Async.ICreate<TeamResource>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using NSubstitute;
Expand All @@ -24,7 +25,8 @@ public void WaitForCompletionReportsProgress_ActionOverload()
var repository = new TaskRepository(new OctopusAsyncRepository(client));
var taskResource = new TaskResource { Links = new LinkCollection() { { "Self", "" } }, State = TaskState.Queued };

client.Get<TaskResource>(Arg.Any<string>(), Arg.Any<Dictionary<string, object>>()).Returns(c => Task.FromResult(taskResource));
client.Get<TaskResource>(Arg.Any<string>(), Arg.Any<Dictionary<string, object>>(), Arg.Any<CancellationToken>())
.Returns(c => Task.FromResult(taskResource));

var callbackCount = 0;

Expand Down Expand Up @@ -53,7 +55,7 @@ public void WaitForCompletionReportsProgress_TaskOverload()
var repository = new TaskRepository(new OctopusAsyncRepository(client));
var taskResource = new TaskResource { Links = new LinkCollection() { { "Self", "" } }, State = TaskState.Queued };

client.Get<TaskResource>(Arg.Any<string>(), Arg.Any<Dictionary<string, object>>()).Returns(c => Task.FromResult(taskResource));
client.Get<TaskResource>(Arg.Any<string>(), Arg.Any<Dictionary<string, object>>(), Arg.Any<CancellationToken>()).Returns(c => Task.FromResult(taskResource));

var callbackCount = 0;

Expand Down Expand Up @@ -81,7 +83,7 @@ public void WaitForCompletion_CancelsInATimelyManner()
var repository = new TaskRepository(new OctopusAsyncRepository(client));
var taskResource = new TaskResource { Links = new LinkCollection() { { "Self", "" } }, State = TaskState.Queued };

client.Get<TaskResource>(Arg.Any<string>(), Arg.Any<Dictionary<string, object>>()).Returns(c => Task.FromResult(taskResource));
client.Get<TaskResource>(Arg.Any<string>(), Arg.Any<Dictionary<string, object>>(), Arg.Any<CancellationToken>()).Returns(c => Task.FromResult(taskResource));

Action exec = () =>
{
Expand Down Expand Up @@ -117,4 +119,4 @@ void SetupClient(IOctopusAsyncClient client)
client.Get<UserResource>(Arg.Any<string>()).Throws(new OctopusSecurityException(401, "Test"));
}
}
}
}
9 changes: 7 additions & 2 deletions source/Octopus.Server.Client/OctopusAsyncClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,12 @@ public Uri QualifyUri(string path, object parameters = null)
return serverEndpoint.OctopusServer.Resolve(path);
}

protected virtual async Task<OctopusResponse<TResponseResource>> DispatchRequest<TResponseResource>(OctopusRequest request, bool readResponse, CancellationToken cancellationToken = default)
[Obsolete("Please use the overload with cancellation token instead.", false)]
protected async Task<OctopusResponse<TResponseResource>> DispatchRequest<TResponseResource>(
OctopusRequest request, bool readResponse)
=> await DispatchRequest<TResponseResource>(request, readResponse, CancellationToken.None);

protected virtual async Task<OctopusResponse<TResponseResource>> DispatchRequest<TResponseResource>(OctopusRequest request, bool readResponse, CancellationToken cancellationToken)
{
using (var message = new HttpRequestMessage())
{
Expand Down Expand Up @@ -716,4 +721,4 @@ public void Dispose()
client?.Dispose();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Octopus.Client.Model;
using Octopus.Client.Model.Endpoints;
Expand All @@ -24,14 +25,14 @@ public ArchivedEventFileRepository(IOctopusAsyncRepository repository) : base(re

public async Task<Stream> GetContent(ArchivedEventFileResource archiveEventFile)
{
await ThrowIfServerVersionIsNotCompatible();
await ThrowIfServerVersionIsNotCompatible(CancellationToken.None);

return await Client.GetContent(archiveEventFile.Link("Self"));
}

public async Task<ResourceCollection<ArchivedEventFileResource>> List(int skip = 0, int? take = null)
{
await ThrowIfServerVersionIsNotCompatible();
await ThrowIfServerVersionIsNotCompatible(CancellationToken.None);

return await Client.List<ArchivedEventFileResource>(
await Repository.Link(CollectionLinkName).ConfigureAwait(false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ private async Task AssertSpaceIdMatchesResource(TResource resource)
await CheckSpaceResource(spaceResource).ConfigureAwait(false);
}

// TODO: default value on token is a stop-gap until cancellation token is fully supported across.
protected async Task<bool> ThrowIfServerVersionIsNotCompatible(CancellationToken cancellationToken = default)
protected async Task<bool> ThrowIfServerVersionIsNotCompatible(CancellationToken cancellationToken)
{
if (!hasMinimumRequiredVersion) return false;

Expand All @@ -94,8 +93,7 @@ await EnsureServerIsMinimumVersion(
return false;
}

// TODO: default value on token is a stop-gap until cancellation token is fully supported across.
protected async Task EnsureServerIsMinimumVersion(SemanticVersion requiredVersion, Func<string, string> messageGenerator, CancellationToken cancellationToken = default)
protected async Task EnsureServerIsMinimumVersion(SemanticVersion requiredVersion, Func<string, string> messageGenerator, CancellationToken cancellationToken)
{
var currentServerVersion = (await Repository.LoadRootDocument(cancellationToken)).Version;

Expand All @@ -105,6 +103,7 @@ protected async Task EnsureServerIsMinimumVersion(SemanticVersion requiredVersio
}
}

[Obsolete("Please use the overload with cancellation token instead.", false)]
public virtual async Task<TResource> Create(TResource resource, object pathParameters = null)
{
return await Create(resource, pathParameters, CancellationToken.None);
Expand All @@ -125,6 +124,7 @@ public virtual async Task<TResource> Create(TResource resource, object pathPara
return await Client.Create(link, resource, pathParameters, cancellationToken).ConfigureAwait(false);
}

[Obsolete("Please use the overload with cancellation token instead.", false)]
public virtual async Task<TResource> Modify(TResource resource)
{
return await Modify(resource, CancellationToken.None);
Expand Down Expand Up @@ -373,7 +373,7 @@ public Task<TResource> Refresh(TResource resource, CancellationToken cancellatio

protected virtual void EnrichSpaceId(TResource resource)
{
ThrowIfServerVersionIsNotCompatible().ConfigureAwait(false);
ThrowIfServerVersionIsNotCompatible(CancellationToken.None).ConfigureAwait(false);

if (resource is IHaveSpaceResource spaceResource)
{
Expand All @@ -383,8 +383,7 @@ protected virtual void EnrichSpaceId(TResource resource)
}
}

// TODO: default value on token is a stop-gap until cancellation token is fully supported across.
protected async Task<string> ResolveLink(CancellationToken cancellationToken = default)
protected async Task<string> ResolveLink(CancellationToken cancellationToken)
{
await ThrowIfServerVersionIsNotCompatible(cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Octopus.Client.Editors.Async;
using Octopus.Client.Model;
Expand Down Expand Up @@ -30,14 +31,14 @@ public Task<ProjectTriggerResource> FindByName(ProjectResource project, string n

public Task<ProjectTriggerEditor> CreateOrModify(ProjectResource project, string name, TriggerFilterResource filter, TriggerActionResource action)
{
ThrowIfServerVersionIsNotCompatible().ConfigureAwait(false);
ThrowIfServerVersionIsNotCompatible(CancellationToken.None).ConfigureAwait(false);

return new ProjectTriggerEditor(this).CreateOrModify(project, name, filter, action);
}

public async Task<ResourceCollection<ProjectTriggerResource>> FindByRunbook(params string[] runbookIds)
{
await ThrowIfServerVersionIsNotCompatible();
await ThrowIfServerVersionIsNotCompatible(CancellationToken.None);

return await Client.List<ProjectTriggerResource>(await Repository.Link("Triggers"), new { runbooks = runbookIds });
}
Expand Down
Loading

0 comments on commit 1e047d1

Please sign in to comment.