From 1300a921188183ee9562abd0bdbb9635e5e5fb92 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Thu, 10 Nov 2022 16:24:10 +0100 Subject: [PATCH] Add ForEach sample workflow --- .../Elsa.Samples.LoopingWorkflows/Program.cs | 7 ++- .../Workflows/ForEachWorkflow.cs | 45 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/samples/console/Elsa.Samples.LoopingWorkflows/Workflows/ForEachWorkflow.cs diff --git a/src/samples/console/Elsa.Samples.LoopingWorkflows/Program.cs b/src/samples/console/Elsa.Samples.LoopingWorkflows/Program.cs index c1a547498..119a192d8 100644 --- a/src/samples/console/Elsa.Samples.LoopingWorkflows/Program.cs +++ b/src/samples/console/Elsa.Samples.LoopingWorkflows/Program.cs @@ -15,5 +15,8 @@ var serviceProvider = services.BuildServiceProvider(); // Resolve a workflow runner to run the workflow. var workflowRunner = serviceProvider.GetRequiredService(); -// Run the ForWorkflow. -await workflowRunner.RunAsync(ForWorkflow.Create()); \ No newline at end of file +// Run the For workflow. +await workflowRunner.RunAsync(ForWorkflow.Create()); + +// Run the For Each workflow. +await workflowRunner.RunAsync(ForEachWorkflow.Create()); \ No newline at end of file diff --git a/src/samples/console/Elsa.Samples.LoopingWorkflows/Workflows/ForEachWorkflow.cs b/src/samples/console/Elsa.Samples.LoopingWorkflows/Workflows/ForEachWorkflow.cs new file mode 100644 index 000000000..a5efb3604 --- /dev/null +++ b/src/samples/console/Elsa.Samples.LoopingWorkflows/Workflows/ForEachWorkflow.cs @@ -0,0 +1,45 @@ +using Elsa.Expressions.Models; +using Elsa.Scheduling.Activities; +using Elsa.Scheduling.Models; +using Elsa.Workflows.Core.Activities; +using Elsa.Workflows.Core.Models; +using Elsa.Workflows.Core.Services; + +namespace Elsa.Samples.LoopingWorkflows.Workflows; + +public static class ForEachWorkflow +{ + public static IActivity Create() + { + var currentValueVariable = new Variable(); + var shoppingList = new[] { "Apples", "Bananas", "Potatoes", "Coffee", "Honey", "Rice" }; + + return new Sequence + { + Variables = { currentValueVariable }, + Activities = + { + new WriteLine("Going through the shopping list..."), + new ForEach(shoppingList) + { + CurrentValue = new Output(currentValueVariable), + Body = new Sequence + { + Activities = + { + new WriteLine(context => $"- [ ] {currentValueVariable.Get(context)}"), + new Delay + { + // The strategy determines whether the workflow should suspend or wait synchronously. + // For console applications without a host, long-running workflows aren't supported, so we use the Blocking strategy. + Strategy = new Input(DelayBlockingStrategy.Blocking), + TimeSpan = new Input(TimeSpan.FromMilliseconds(250)) + } + } + } + }, + new WriteLine("Let's not forget anything!") + } + }; + } +} \ No newline at end of file