pyrox18
pyrox18

Reputation: 21

How do I implement an "undo" action for executed activities in Elsa Workflows?

I am implementing several custom activities that function similarly to the SignalReceived activity, where the activity is suspended after execution and is manually resumed elsewhere in code. I would like to be able to undo activities that have already been executed so that users of my application can re-enter the data for that particular activity.

I attempted something like the following to achieve this functionality:

// Find the workflow instance that I'm undoing the activity for
var instance = await workflowInstanceStore.FindByIdAsync(workflowInstanceId, cancellationToken);

// Manually set the activity that I want to undo to, and clear any current blocking activities
instance.CurrentActivity = new ScheduledActivity(activityId);
instance.BlockingActivities.Clear();

// Grab existing inputs and save the modified workflow instance
var input = await GetWorkflowInputAsync(instance, cancellationToken);
await workflowStorageService.SaveAsync(input, instance, cancellationToken);

// Re-execute workflow instance with the activity that I want to undo to
await workflowInstanceExecutor.ExecuteAsync(instance, activityId, cancellationToken: cancellationToken);

However, I found two issues with this approach:

  1. Manually modifying the activities on the workflow instance and saving it with the workflow storage service doesn't seem to do anything to the instance (i.e. the changes are not preserved and the activity execution log remains unmodified).
  2. Re-executing the workflow instance with workflowInstanceExecutor.ExecuteAsync does re-execute the activity that I want to undo to, but it also immediately resumes that activity and goes straight to the next activity. I would like it to only execute the activity and suspend it like how it works during normal execution.

Is there a better solution for what I want to achieve here? Ideally I want to perform this undoing process outside of the activity implementation, so I do not have access to the ActivityExecutionContext object (unless there's a way to inject it elsewhere in code).

Upvotes: 2

Views: 262

Answers (0)

Related Questions