-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cancel ScriptExecution without calling CancelScript when the StartScr…
…ipt request has never been transferred to Tentacle (#756) * Enhancing how we check if we have cancelled while starting a script * Additional tests to cover cancellation logic --------- Co-authored-by: Stephen Burman <[email protected]>
- Loading branch information
1 parent
456d1c0
commit ca792e8
Showing
9 changed files
with
271 additions
and
10 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
source/Octopus.Tentacle.Client/ExceptionExtensionMethods.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using Halibut; | ||
using Halibut.Exceptions; | ||
using Halibut.Transport; | ||
|
||
namespace Octopus.Tentacle.Client | ||
{ | ||
public static class ExceptionExtensionMethods | ||
{ | ||
public static bool IsConnectionException(this Exception exception) | ||
{ | ||
return exception is ConnectingRequestCancelledException | ||
|| exception is HalibutClientException {ConnectionState: ConnectionState.Connecting}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...Support/PendingRequestQueueFactories/CancelWhenRequestQueuedPendingRequestQueueFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Halibut; | ||
using Halibut.Diagnostics; | ||
using Halibut.ServiceModel; | ||
using Halibut.Transport.Protocol; | ||
|
||
namespace Octopus.Tentacle.Tests.Integration.Support.PendingRequestQueueFactories | ||
{ | ||
/// <summary> | ||
/// CancelWhenRequestQueuedPendingRequestQueueFactory cancels the cancellation token source when a request is queued | ||
/// </summary> | ||
public class CancelWhenRequestQueuedPendingRequestQueueFactory : IPendingRequestQueueFactory | ||
{ | ||
readonly CancellationTokenSource cancellationTokenSource; | ||
readonly HalibutTimeoutsAndLimits halibutTimeoutsAndLimits; | ||
readonly Func<Task<bool>> shouldCancel; | ||
|
||
public CancelWhenRequestQueuedPendingRequestQueueFactory(CancellationTokenSource cancellationTokenSource, HalibutTimeoutsAndLimits halibutTimeoutsAndLimits, Func<Task<bool>> shouldCancel) | ||
{ | ||
this.cancellationTokenSource = cancellationTokenSource; | ||
this.halibutTimeoutsAndLimits = halibutTimeoutsAndLimits; | ||
this.shouldCancel = shouldCancel; | ||
} | ||
|
||
public IPendingRequestQueue CreateQueue(Uri endpoint) | ||
{ | ||
return new Decorator(new PendingRequestQueueAsync(halibutTimeoutsAndLimits, new LogFactory().ForEndpoint(endpoint)), cancellationTokenSource, shouldCancel); | ||
} | ||
|
||
class Decorator : IPendingRequestQueue | ||
{ | ||
readonly CancellationTokenSource cancellationTokenSource; | ||
readonly Func<Task<bool>> shouldCancel; | ||
readonly IPendingRequestQueue inner; | ||
|
||
public Decorator(IPendingRequestQueue inner, CancellationTokenSource cancellationTokenSource, Func<Task<bool>>? shouldCancel) | ||
{ | ||
this.inner = inner; | ||
this.cancellationTokenSource = cancellationTokenSource; | ||
this.shouldCancel = shouldCancel; | ||
} | ||
|
||
public bool IsEmpty => inner.IsEmpty; | ||
public int Count => inner.Count; | ||
public async Task ApplyResponse(ResponseMessage response, ServiceEndPoint destination) => await inner.ApplyResponse(response, destination); | ||
public async Task<RequestMessageWithCancellationToken?> DequeueAsync(CancellationToken cancellationToken) => await inner.DequeueAsync(cancellationToken); | ||
|
||
public async Task<ResponseMessage> QueueAndWaitAsync(RequestMessage request, CancellationToken cancellationTokens) | ||
{ | ||
var queueAndWait = inner.QueueAndWaitAsync(request, cancellationTokens); | ||
var cancel = Task.Run(async () => | ||
{ | ||
if (await shouldCancel()) | ||
{ | ||
// Allow the PendingRRequest to be queued | ||
cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(1)); | ||
} | ||
}); | ||
|
||
await Task.WhenAll(queueAndWait, cancel); | ||
|
||
return await queueAndWait; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.