Skip to content
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

[ENH] Working with multi-instance workflow HttpEndpoints #6038

Open
Reza-Noei opened this issue Oct 17, 2024 · 0 comments
Open

[ENH] Working with multi-instance workflow HttpEndpoints #6038

Reza-Noei opened this issue Oct 17, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@Reza-Noei
Copy link

Reza-Noei commented Oct 17, 2024

Enhancement Request

Clarification on how to handle multiple-instance workflows when we are designing HttpEndPoints.

Enhancement Overview

I've created a workflow when I had some HttpEndpoints to get user approval on the other existing system which uses Elsa as Workflow engine.
I have following workflow:

public class SpecialWorkflow : WorkflowBase
{
    protected override void Build(IWorkflowBuilder builder)
    {
        var queryStringsVariable = builder.WithVariable<IDictionary<string, object>>();
        var messageVariable = builder.WithVariable<string>();

        builder.Root = new Sequence
        {
            Activities =
            {
                new HttpEndpoint
                {
                    SupportedMethods = new Input<ICollection<string>>(["POST"]),
                    Path = new("/SpecialCaseFlow"),
                    CanStartWorkflow = true,
                    QueryStringData = new(queryStringsVariable),
                },
                new WriteHttpResponse
                {
                    Content = new(context => context.GetWorkflowExecutionContext().Id),
                    StatusCode = new Input<HttpStatusCode>(HttpStatusCode.OK),
                },
                new SendHttpRequest
                {
                    Url = new Input<Uri?>(new Uri("https://localhost:7186/AddToSysAdminCartable")),
                    Method = new Input<string>("POST"),
                },
                new HttpEndpoint
                {
                    SupportedMethods = new Input<ICollection<string>>(["POST"]),
                    Path = new("/{workflowInstanceId}/AssignManager"),
                },
                new SendHttpRequest
                {
                    Url = new Input<Uri?>(new Uri("https://localhost:7186/AddToManagerCartable")),
                    Method = new Input<string>("POST"),
                },
                new HttpEndpoint
                {
                    SupportedMethods = new Input<ICollection<string>>(["POST"]),
                    Path = new("/{workflowInstanceId}/AssignExpert"),
                },
                new SendHttpRequest
                {
                    Url = new Input<Uri?>(new Uri("https://localhost:7186/AssingToExperts")),
                    Method = new Input<string>("POST"),
                },
                new End()
                {

                }
            }
        };
    }
}

The problem is that Elsa is unable to find the exact WorkflowInstance using the route parameter that I've provided. It's so bad. I've read the main source code and I didn't find a clue about setting a tricky header or payload to tell Elsa "Bro I'm working with instance # 12" don't get confused 😁.
There must be a way of doing it.

Proposed Enhancement

Add some conventional Route or Header to tell clients that you are able to send your workflow instanceId using this conventionally accepted way.

Use Cases

Suppose that I have multiple instance of a workflow which Aimed to handle document approval process. I've over 100 agents that are handling this document approval. All of them has been already started a new instance of my SpecialFlow they needed to be able Approved message using HttpEndPoints to Elsa and continue the workflow.

Alternative solution

I already resolved this damn issue with following code:

public class SpecialWorkflow : WorkflowBase
{
    protected override void Build(IWorkflowBuilder builder)
    {
        var queryStringsVariable = builder.WithVariable<IDictionary<string, object>>();
        var messageVariable = builder.WithVariable<string>();
        var uniqueId = Guid.New();
        builder.Root = new Sequence
        {
            Activities =
            {
                new HttpEndpoint
                {
                    SupportedMethods = new Input<ICollection<string>>(["POST"]),
                    Path = new("/SpecialCaseFlow"),
                    CanStartWorkflow = true,
                    QueryStringData = new(queryStringsVariable),
                },
                new WriteHttpResponse
                {
                    Content = new(context => context.GetWorkflowExecutionContext().Id),
                    StatusCode = new Input<HttpStatusCode>(HttpStatusCode.OK),
                },
                new SendHttpRequest
                {
                    Url = new Input<Uri?>(new Uri("https://localhost:7186/AddToSysAdminCartable")),
                    Method = new Input<string>("POST"),
                },
                new HttpEndpoint
                {
                    SupportedMethods = new Input<ICollection<string>>(["POST"]),
                    Path = new($"/{uniqueId}/AssignManager"), // This no longer meant to be a route parameter.
                },
                new SendHttpRequest
                {
                    Url = new Input<Uri?>(new Uri("https://localhost:7186/AddToManagerCartable")),
                    Method = new Input<string>("POST"),
                },
                new HttpEndpoint
                {
                    SupportedMethods = new Input<ICollection<string>>(["POST"]),
                    Path = new($"/{uniqueId}/AssignExpert"),  // This no longer meant to be a route parameter.
                },
                new SendHttpRequest
                {
                    Url = new Input<Uri?>(new Uri("https://localhost:7186/AssingToExperts")),
                    Method = new Input<string>("POST"),
                },
                new End()
                {

                }
            }
        };
    }
}
@Reza-Noei Reza-Noei added the enhancement New feature or request label Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant