Skip to content

Instantly share code, notes, and snippets.

@cgillum
Created February 18, 2023 01:00
Show Gist options
  • Save cgillum/412db74082f48570e4e90c1a9564040d to your computer and use it in GitHub Desktop.
Save cgillum/412db74082f48570e4e90c1a9564040d to your computer and use it in GitHub Desktop.
Shows how to fan out to a multiple parallel task chaining sequences using simple C# constructs in Dapr Workflow.
class ParallelTaskChaining : Workflow<string, string>
{
public override async Task<string> RunAsync(WorkflowContext context, string input)
{
// Fan-out to 10 parallel task chains
List<Task> parallelChains = new();
for (int i = 0; i < 10; i++)
{
// RunSequence encapsulates the task chain as a single task.
Task chainTask = RunSequence(context);
parallelChains.Add(chainTask);
}
// This Task.WhenAll completes after every task in every chain has completed.
await Task.WhenAll(parallelChains);
return "done";
}
private static async Task RunSequence(WorkflowContext context)
{
// This task chain calls three workflow activities in a row
await context.CallActivityAsync(nameof(MyActivity));
await context.CallActivityAsync(nameof(MyActivity));
await context.CallActivityAsync(nameof(MyActivity));
}
}
// Simple activity definition that just returns the input (if any)
class MyActivity : WorkflowActivity<string, string>
{
public override Task<string> RunAsync(WorkflowActivityContext context, string input)
{
return Task.FromResult(input);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment