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

[BUG] Restore variables values after resume from a bookmark. #5547

Closed
edward-yuen-tfs opened this issue Jun 6, 2024 · 4 comments · Fixed by #5558
Closed

[BUG] Restore variables values after resume from a bookmark. #5547

edward-yuen-tfs opened this issue Jun 6, 2024 · 4 comments · Fixed by #5558
Assignees
Labels
bug Something isn't working elsa 3 This issue is specific to Elsa 3
Milestone

Comments

@edward-yuen-tfs
Copy link

Description

For a variable defined in the parent workflow, create an activity to decrement the variable and creates a bookmark afterwards. After the bookmark is resumed, the variable value is not restored properly in the parent workflow and the variable value continues to stay as default.

Steps to Reproduce

To help us identify the issue more quickly, please follow these guidelines:

  1. Create a sample workflow like below.
    public class CountDownWorkflow : WorkflowBase
    {
    protected override void Build(IWorkflowBuilder builder)
    {
    var numberOfItems = builder.WithVariable("NumberOfItems", 5).WithWorkflowStorage();

      builder.Root = new Sequence
      {
          Variables = { numberOfItems },
          Activities =
          {
          new SetVariable<int>(numberOfItems, 5),  // Try to explicit set here.
          new WriteLine(context => "Initial number of items = " + numberOfItems.Get(context) ),
          new While(context => numberOfItems.Get(context) > 0)
          {
              Body = new Sequence
              {
                  Activities =
                  {
                      // Increment counter variable.
                      new CustomActivity(),
                      new WriteLine(context => "Number of items = " + numberOfItems.Get(context) ),
                  }
              }
          }
      }
      };
    

    }
    }

  2. The CustomActivity code is like below.
    public class CustomActivity : CodeActivity
    {
    public CustomActivity()
    {
    }
    protected override void Execute(ActivityExecutionContext context)
    {
    var numberOfItems = context.GetVariable("NumberOfItems");
    context.SetVariable("NumberOfItems", numberOfItems - 1);
    context?.CreateBookmark();
    }
    }

  3. Trigger the workflow. Jot down the instance Id.

  4. Call resume multiple times. Observed that the value does not decrement further in the While loop.

  5. You can call the following 2 apis to try to test it with the attached sample program,
    https://localhost:5001/testapi/submit - jot down the instance id.
    https://localhost:5001/testapi/resume - put the instance id in the body.

Expected Behavior

Variable is decremented and the parent workflow can print it until the variable reaches zero and exit the While loop

Actual Behavior

The variable value stays as 4 all the time. Do not decrement to zero.

Screenshots

image

Environment

Log Output

Troubleshooting Attempts

Additional Context

Related Issues

#4148

ResumeWorkflowApp.Web.zip

@edward-yuen-tfs edward-yuen-tfs added the bug Something isn't working label Jun 6, 2024
@sfmskywalker sfmskywalker added the elsa 3 This issue is specific to Elsa 3 label Jun 7, 2024
@sfmskywalker sfmskywalker self-assigned this Jun 7, 2024
@sfmskywalker sfmskywalker added this to the Elsa 3.2 milestone Jun 7, 2024
@elsa-workflows elsa-workflows deleted a comment from glime-ai bot Jun 7, 2024
@sfmskywalker
Copy link
Member

Thanks @edward-yuen-tfs for the level of detail + working repro - I can indeed reproduce the issue.

@sfmskywalker
Copy link
Member

I figured out what the problem is. Actually, there are two issues:

1. Variable Persistence

Notice the following line in your workflow:

image

When using builder.WithVariable(), it will create a variable on the workflow level.
But when you assign a variable with the same name on a child activity, such as Sequence in your workflow, that variable becomes closer to your custom activity, which means that your custom activity will be setting the variable's value in the scope of the Sequence activity and not in the scope of the workflow.

That on its own wouldn't be a big deal, if it weren't for the fact that as of this writing, only workflow-level variables are persisted.

Given that the workflow level variable is never updated, it explains that its value is lost after resuming the workflow instance.

So, if you remove that line that I enrectangled red in the screenshot, it will work - almost.

2. Variable Scoping Bug

While troubleshooting this issue, I encountered a bug that causes your workflow to print the same number twice (the number 4, as you will see).
This is due to a scoping inconsistency bug in Elsa. I am already working on a fix that I will push out over the weekend, which means that you'll be able to take advantage by referencing the preview packages once they are built.

I'll update progress here soon.

@edward-yuen-tfs
Copy link
Author

Thank you for looking into that. Just want to be confirm on this.

  1. "only workflow-level variables are persisted." - that means
    a. activity level variables are not persisted.
    b. For the picture below, which I have commented out the line you suggest.
    image
  • As I already set the value in the orange box, I do not need to set the value in the red box. Both lines in fact refer to the same scope.
  • I do not need the commented line as it shall be considered as defining a new variable within the sequence scope. It is not considered as passing the workflow scope variable to the sequence scope (it is not needed.)
    c. After I commented the line, CustomActivity is actually setting the variable value to the workflow scope.

Just want to confirm if my understanding is correct.

@sfmskywalker
Copy link
Member

sfmskywalker commented Jun 8, 2024

Confirmed on all points 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working elsa 3 This issue is specific to Elsa 3
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants