elsa-workflows / elsa-core

A .NET workflows library
https://v3.elsaworkflows.io/
MIT License
5.87k stars 1.06k forks source link

How to make the activity execute automatically after the process is restored? #5281

Open 15859096137 opened 2 weeks ago

15859096137 commented 2 weeks ago

Hello, I have a scenario like this: an employee submits information, and after the department approves it, it is transferred to the manager approval node. However, at the manager approval node, a withdrawal operation may be performed, withdrawing to the previous approval node, which is the department node. At this time, the department supervisor needs to re-approve, but why doesn’t it automatically transfer to the manager approval node after re-approval? Below is my related code, I hope to get your reply, thank you!

public class Department : CodeActivity
{
    protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
    {
        await context.CompleteActivityAsync();

        //The Manager node can be executed automatically the first time it is run

        //When it is run for the second time, the next node to be executed should also be Manager
    }
}

public class Manager : Activity { protected override void Execute(ActivityExecutionContext context) { var list = new List() {"Tag1","Tag2","Tag3" }; context.CreateBookmarks(list, ResumeAsync, false);

    }

    private async ValueTask ResumeAsync(ActivityExecutionContext context)
    {
        //7c6e64b08741151a is Department
        var aa = context.WorkflowExecutionContext.FindActivityById("7c6e64b08741151a");
        context.WorkflowExecutionContext.ScheduleActivity(aa);
    }
}

    public async Task TestWorkflow(string workflowInstanceId)
    {

        var instanceFilter = new WorkflowInstanceFilter()
        {
            Id = workflowInstanceId
        };
        var workflowInstance = await _workflowInstanceStore.FindAsync(instanceFilter);
        var workflowState = workflowInstance.WorkflowState;

        if (workflowState.Bookmarks.Count > 0)
        {
            var bookmark = workflowState.Bookmarks.FirstOrDefault();
            var options = new Elsa.Workflows.Options.RunWorkflowOptions { BookmarkId = bookmark.Id };
            var message = new Elsa.Workflows.Runtime.Models.NewWorkflowInboxMessage
            {
                BookmarkPayload = bookmark.Payload,
                ActivityTypeName = ActivityTypeNameHelper.GenerateTypeName<Manager>(),
                WorkflowInstanceId = workflowInstanceId
            };
        }
    }

111

After the department node is executed, it should trigger the manager node, right? This is also what I hope to happen.

glime-ai[bot] commented 2 weeks ago

To address your scenario where an employee's submission is approved by a department and then needs to be approved by a manager, with the possibility of withdrawal to the previous approval node, you need to ensure that the workflow is designed to handle the state transitions correctly.

From the code snippets you provided, it seems that you are using the Department and Manager activities to represent the department and manager approval nodes, respectively. However, without seeing the complete workflow definition, it's difficult to pinpoint the exact issue.

Here are some general steps you can take to troubleshoot and resolve the issue:

  1. Check Workflow Design: Ensure that your workflow is designed to handle the transitions between the department and manager nodes correctly. This includes setting up the correct outcomes from the Department activity that lead to the Manager activity.

  2. Bookmarking and Resuming: When the Department activity is approved, it should create a bookmark for the Manager activity. When the Manager activity is withdrawn, it should resume the Department activity and, upon re-approval, create a new bookmark for the Manager activity again.

  3. Workflow State: Inspect the workflow state after the department re-approval to ensure that the correct bookmarks are in place and that the workflow is in the expected state to transition to the manager node.

  4. Activity Execution: In the Manager activity's ResumeAsync method, ensure that you are scheduling the next activity correctly. It might be necessary to check if the activity has already been executed and handle the logic accordingly.

  5. Workflow Instance Store: Verify that the workflow instance store is being updated correctly after each transition. This will ensure that the workflow can be resumed from the correct state.

  6. Logging and Debugging: Add logging to your activities to trace the workflow execution path. This can help identify where the workflow might be diverging from the expected path.

  7. Elsa Documentation and Community: Refer to the Elsa documentation and community discussions for examples and best practices on handling such scenarios. For instance, the discussion on GitHub issue #4058 mentions the use of "Withdraw" outcomes and signaling between activities, which might be relevant to your case.

If you continue to face issues, consider providing more details about your workflow definition and the specific problem you're encountering. This will help in giving a more precise solution.

This comment was generated by Glime.