Azure / durabletask

Durable Task Framework allows users to write long running persistent workflows in C# using the async/await capabilities.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Recommendation to implement cancellation of a TaskActivity

edouard-chevalier opened this issue · comments

Hello, we have the current setup of orchestration and activities:

public class LongActivity: AsyncTaskActivity<string,string> {
  protected override async Task<string> ExecuteAsync( TaskContext context,  string input ) {
     /// DO a pretty long task
     return input;
  }
}

public class ParallelOrchestration :  TaskOrchestration {
  public override async Task RunTask( OrchestrationContext ctx, string _ ){
    var task1 = ctx.ScheduleActivity<LongActivity>( "foo" );
    var task2= ctx.ScheduleActivity<LongActivity>( "bar" );
    await Task.WhenAll( task1, task2);
  }
}

With TaskHubClient we can start the orchestration, and we want to be able to abort it, currently, we "terminate" the orchestration:

  TaskHubClient client = /// create client
  var orchestrationInstance = await  client.CreateOrchestrationAsync( typeof( ParallelOrchestration), "input");
 
  /// do other stuffs

 /// terminate the orchestration
  await client.TerminateInstanceAsync( orchestrationInstance);

The ADTF framework will indeed terminate the orchestration, but the task activity are still on going. These activities are pretty long to execute and, unfortunately, we cannot divide them in smaller activities. But they can take a cancellationToken.

Is there any recommendation on how to implement cancellation of activities ?
Bonus question: any recommendation on how to ensure that a sub-orchestration is also cancelled/terminated if the parent orchestration is terminated ?

Both of these features (cascading terminate of sub-orchestrations and aborting activities) are currently on our roadmap to implement in the next 6 months or so.

In the meantime, I think the best you can do for aborting activities is to create a background thread that polls for the status of the parent. If the background thread discovers that the parent orchestration was terminated, abort the current activity execution (using a cancellation token, or whatever is appropriate for your scenario). We're likely to implement something similar to this in the framework down the road.