Elsa workflowcontext concurrency on forked execution

181 Views Asked by At

An Elsa workflow is set with a player object in the workflow context, however, I am concerned about how the concurrency is managed internally. The scenario is as follows: a FORK activity has 3 subsequent activities that run in parallel. If each activity gets the player object from the context and makes some changes before setting it back to the context, is Elsa intelligent enough to do some optimistic locking? The changes made in parallel by the 3 activities should be consistent and should not overwrite any data made by the other activities.

How this can be guaranteed?

Worflow with a fork and 3 parallel activities

Thanks

Tried to play around with the custom workflow provider.

1

There are 1 best solutions below

2
Sipke Schoorstra On

Both Elsa 2 and Elsa 3 process activities sequentially, even when forking execution into multiple branches as you depicted.

This means that only one activity will execute at any one time, which ensures that there are no concurrency issues.

In your example, Activity 3, 2 and 4 will execute one after the other, and not simultaneously.

For your use case, this seems desirable. However, should you ever do need to run activities in parallel, the way to do that is to have your activities schedule their work as a background job.

This way, all three activities will execute in parallel, even though they were scheduled in sequence.

In Elsa 2, you have to handle scheduling background jobs yourself, using a custom solution, or using libraries such as Hangfire. You then also need to take care of suspending and resuming your custom activities.

In Elsa 3, all this is taken care for you by simply annotating your activity class to have its "kind" be set to "Job" or "Task". Job activities are executed in the background. Task activities execute synchronously by default, but allows the user to configure these to be run from the background by ticking a simple checkbox.

I hope this clarifies things a bit.