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

[Question] Would it be advisable to structure our process using sub-workflows? #1269

Closed
santhanuv opened this issue May 29, 2024 · 3 comments

Comments

@santhanuv
Copy link

We have a use case involving nested workflows, where one step includes a sub-workflow that must be completed before proceeding to the next step. Although we found Decision Branching (#486), which allows the creation of branches with multiple steps, we have a few concerns:

  1. Decision branches are condition-based, but our scenario doesn’t involve conditions. We need to execute a sub-workflow as a step without any conditions.
  2. Our workflow is lengthy, and we aim to break it down into smaller, manageable sub-workflows.

Would it be advisable to structure our process using sub-workflows? If so, how can we implement this using the package? We've tried a few options, but they don't wait for the sub-workflow to complete before moving on.

@jordanwallwork
Copy link

How about using Events to signal that the sub-workflow is finished?

        builder
            .StartWith<StartSubWorkflowA>(x=> x.Input(step => step.WorkflowId, (data, ctx) => ctx.Workflow.Id))
            .WaitFor("SubWorkflowACompleted", (data, ctx) => ctx.Workflow.Id))

The StartSubWorkflowA step would trigger the sub workflow, passing it the WorkflowId so it can dispatch it in the event to signal that the subworkflow has finished

public class StartSubWorkflowA : StepBody
{
    private readonly IWorkflowHost _workflowHost;
    
    public string WorkflowId { get; set; }

    public StartSubWorkflowA (IWorkflowHost workflowHost)
    {
        _workflowHost = workflowHost;
    }

    public override ExecutionResult Run(IStepExecutionContext context)
    {
        // kick off sub-workflow. 

        return ExecutionResult.Next();
    }
}

And then as the final step of the sub workflow you'd need to trigger the event letting the "parent" workflow know it can resume:

        await _workflowHost.PublishEvent("SubWorkflowACompleted", _workflowId);

(I've not tried any of this btw, just how I'd approach it if it were me)

@cjundt
Copy link

cjundt commented Jun 4, 2024

Hi @jordanwallwork,

This is a good idea but I'd rather use a middleware to trigger the "workflowcompleted" event so that you can use any workflow as a subworkflow.

@santhanuv
Copy link
Author

@jordanwallwork @cjundt
Thanks for the info! While we were waiting, we actually stumbled on this approach ourselves. We weren't sure if it was the best way to go, but I think it might work.

On a side note, using middleware to publish the event was a great tip – that really helped out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants