-
Notifications
You must be signed in to change notification settings - Fork 4
Added the builder to support the do-while task type #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,99 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Linq.Expressions; | ||
| using ConductorSharp.Client.Generated; | ||
| using ConductorSharp.Engine.Interface; | ||
| using ConductorSharp.Engine.Model; | ||
| using ConductorSharp.Engine.Util; | ||
| using ConductorSharp.Engine.Util.Builders; | ||
|
|
||
| namespace ConductorSharp.Engine.Builders | ||
| { | ||
| /// <summary> | ||
| /// Extension methods to add a DO_WHILE loop with multiple inner tasks, based on BaseTaskBuilder. | ||
| /// </summary> | ||
| public static class DoWhileTaskExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds a DO_WHILE loop to the workflow definition. | ||
| /// </summary> | ||
| /// <typeparam name="TWorkflow">Workflow type</typeparam> | ||
| /// <param name="builder">Parent sequence builder</param> | ||
| /// <param name="reference">Expression selecting the workflow property holding the loop's reference name</param> | ||
| /// <param name="input">Expression creating the input</param> | ||
| /// <param name="loopCondition">Loop condition</param> | ||
| /// <param name="configureBody">Delegate to add tasks inside the loop</param> | ||
| public static ITaskOptionsBuilder AddTask<TWorkflow>( | ||
| this ITaskSequenceBuilder<TWorkflow> builder, | ||
| Expression<Func<TWorkflow, DoWhileTaskModel>> reference, | ||
| Expression<Func<TWorkflow, DoWhileInput>> input, | ||
| string loopCondition, | ||
| Action<ITaskSequenceBuilder<TWorkflow>> configureBody | ||
| ) | ||
| where TWorkflow : ITypedWorkflow | ||
| { | ||
| // Extract expressions | ||
| var taskBuilder = new DoWhileTaskBuilder<TWorkflow>(reference.Body, input.Body, loopCondition, builder.BuildConfiguration); | ||
| // Register it with the outer sequence | ||
| builder.AddTaskBuilderToSequence(taskBuilder); | ||
| // Allow the caller to configure inner loop body tasks | ||
| configureBody(taskBuilder); | ||
| return taskBuilder; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Builder for the DO_WHILE task, extending BaseTaskBuilder for consistent patterns. | ||
| /// </summary> | ||
| internal sealed class DoWhileTaskBuilder<TWorkflow> : BaseTaskBuilder<DoWhileInput, NoOutput>, ITaskSequenceBuilder<TWorkflow> | ||
| where TWorkflow : ITypedWorkflow | ||
| { | ||
| private readonly string _loopCondition; | ||
| private readonly DoWhileInput _doWhileInput; | ||
| private readonly List<WorkflowTask> _innerTasks = new(); | ||
| public BuildContext BuildContext { get; } = new(); | ||
| public BuildConfiguration BuildConfiguration { get; } | ||
| public WorkflowBuildItemRegistry WorkflowBuildRegistry { get; } = new(); | ||
| public IEnumerable<ConfigurationProperty> ConfigurationProperties { get; } = new List<ConfigurationProperty>(); | ||
|
|
||
| /// <inheritdoc /> | ||
| public DoWhileTaskBuilder(Expression taskExpression, Expression inputExpression, string loopCondition, BuildConfiguration buildConfiguration) | ||
| : base(taskExpression, inputExpression, buildConfiguration) | ||
| { | ||
| _loopCondition = loopCondition; | ||
| BuildConfiguration = buildConfiguration; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override WorkflowTask[] Build() | ||
| { | ||
| var refName = _taskRefferenceName; | ||
|
|
||
| if (_innerTasks.Any(t => t.WorkflowTaskType == WorkflowTaskType.DO_WHILE)) | ||
| { | ||
| throw new InvalidOperationException("Nested DO_WHILE tasks are not allowed."); | ||
| } | ||
|
|
||
| var loopTask = new WorkflowTask | ||
| { | ||
| Name = refName, | ||
| TaskReferenceName = refName, | ||
| WorkflowTaskType = WorkflowTaskType.DO_WHILE, | ||
| Type = nameof(WorkflowTaskType.DO_WHILE), | ||
| InputParameters = _inputParameters.ToObject<IDictionary<string, object>>(), | ||
| LoopCondition = _loopCondition, | ||
| LoopOver = _innerTasks, | ||
| }; | ||
| return [loopTask]; | ||
| } | ||
|
|
||
| public void AddTaskBuilderToSequence(ITaskBuilder builder) | ||
| { | ||
| foreach (var task in builder.Build()) | ||
| { | ||
| _innerTasks.Add(task); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,18 @@ | ||
| using ConductorSharp.Engine.Builders; | ||
| using MediatR; | ||
|
|
||
| namespace ConductorSharp.Engine.Model | ||
| { | ||
| /// <summary> | ||
| /// Input for configuration of the DO_WHILE task. | ||
| /// </summary> | ||
| public class DoWhileInput : IRequest<NoOutput>, IWorkflowInput | ||
| { | ||
| public object Value { get; set; } = null; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Task Model to reference the do while task in the workflow builders | ||
| /// </summary> | ||
| public class DoWhileTaskModel : TaskModel<DoWhileInput, NoOutput> { } | ||
| } | ||
This file contains hidden or 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 hidden or 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
31 changes: 31 additions & 0 deletions
31
test/ConductorSharp.Engine.Tests/Samples/Workflows/DoWhileTask.cs
This file contains hidden or 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,31 @@ | ||
| namespace ConductorSharp.Engine.Tests.Samples.Workflows | ||
| { | ||
| public sealed class DoWhileTaskInput : WorkflowInput<DoWhileTaskOutput> | ||
| { | ||
| public int Loops { get; set; } | ||
| } | ||
|
|
||
| public sealed class DoWhileTaskOutput : WorkflowOutput { } | ||
|
|
||
| public sealed class DoWhileTask : Workflow<DoWhileTask, DoWhileTaskInput, DoWhileTaskOutput> | ||
| { | ||
| public DoWhileTaskModel DoWhile { get; set; } | ||
| public CustomerGetV1 GetCustomer { get; set; } | ||
|
|
||
| public DoWhileTask(WorkflowDefinitionBuilder<DoWhileTask, DoWhileTaskInput, DoWhileTaskOutput> builder) | ||
| : base(builder) { } | ||
|
|
||
| public override void BuildDefinition() | ||
| { | ||
| _builder.AddTask( | ||
| wf => wf.DoWhile, | ||
| wf => new() { Value = wf.Input.Loops }, | ||
| "$.do_while.iteration < $.value", | ||
| builder => | ||
| { | ||
| builder.AddTask(wf => wf.GetCustomer, wf => new CustomerGetV1Input() { CustomerId = "CUSTOMER-1" }); | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
test/ConductorSharp.Engine.Tests/Samples/Workflows/DoWhileTask.json
This file contains hidden or 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,34 @@ | ||
| { | ||
| "name": "do_while_task", | ||
| "version": 1, | ||
| "tasks": [ | ||
| { | ||
| "name": "do_while", | ||
| "taskReferenceName": "do_while", | ||
| "inputParameters": { | ||
| "value": "${workflow.input.loops}" | ||
| }, | ||
| "type": "DO_WHILE", | ||
| "loopCondition": "$.do_while.iteration < $.value", | ||
| "loopOver": [ | ||
| { | ||
| "name": "CUSTOMER_get", | ||
| "taskReferenceName": "get_customer", | ||
| "inputParameters": { | ||
| "customer_id": "CUSTOMER-1" | ||
| }, | ||
| "type": "SIMPLE", | ||
| "optional": false, | ||
| "workflowTaskType": "SIMPLE" | ||
| } | ||
| ], | ||
| "workflowTaskType": "DO_WHILE" | ||
| } | ||
| ], | ||
| "inputParameters": [ | ||
| "loops" | ||
| ], | ||
| "outputParameters": {}, | ||
| "schemaVersion": 2, | ||
| "timeoutSeconds": 0 | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.