-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from OrleansContrib/feature/50/cancellationToken
Adds cancellation token support for long running work grains.
- Loading branch information
Showing
11 changed files
with
260 additions
and
32 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
samples/Orleans.SyncWork.Demo.Services/Grains/CancellableGrain.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,58 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Orleans.SyncWork.Demo.Services.Grains; | ||
|
||
public class CancellableGrain : SyncWorker<SampleCancellationRequest, SampleCancellationResult>, ICancellableGrain | ||
{ | ||
public CancellableGrain( | ||
ILogger<CancellableGrain> logger, | ||
LimitedConcurrencyLevelTaskScheduler limitedConcurrencyScheduler) : base(logger, limitedConcurrencyScheduler) | ||
{ | ||
} | ||
|
||
protected override async Task<SampleCancellationResult> PerformWork( | ||
SampleCancellationRequest request, GrainCancellationToken grainCancellationToken) | ||
{ | ||
var startingValue = request.StartingValue; | ||
|
||
for (var i = 0; i < request.EnumerationMax; i++) | ||
{ | ||
if (grainCancellationToken.CancellationToken.IsCancellationRequested) | ||
{ | ||
Logger.LogInformation("Task cancelled on iteration {Iteration}", i); | ||
|
||
if (request.ThrowOnCancel) | ||
throw new OperationCanceledException(grainCancellationToken.CancellationToken); | ||
|
||
return new SampleCancellationResult() { EndingValue = startingValue }; | ||
} | ||
|
||
startingValue += 1; | ||
await Task.Delay(request.EnumerationDelay); | ||
} | ||
|
||
return new SampleCancellationResult() { EndingValue = startingValue }; | ||
} | ||
} | ||
|
||
[GenerateSerializer] | ||
public class SampleCancellationRequest | ||
{ | ||
[Id(0)] | ||
public TimeSpan EnumerationDelay { get; init; } | ||
[Id(1)] | ||
public int StartingValue { get; init; } | ||
[Id(2)] | ||
public int EnumerationMax { get; init; } = 1_000; | ||
[Id(3)] | ||
public bool ThrowOnCancel { get; init; } | ||
} | ||
|
||
[GenerateSerializer] | ||
public class SampleCancellationResult | ||
{ | ||
[Id(0)] | ||
public int EndingValue { get; init; } | ||
} |
4 changes: 4 additions & 0 deletions
4
samples/Orleans.SyncWork.Demo.Services/Grains/ICancellableGrain.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,4 @@ | ||
namespace Orleans.SyncWork.Demo.Services.Grains; | ||
|
||
public interface ICancellableGrain | ||
: ISyncWorker<SampleCancellationRequest, SampleCancellationResult>, IGrainWithGuidKey; |
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
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
Oops, something went wrong.