Introduction
Here in this article I am going to provide the solution on how to replay faulted sequential workflow if that was terminated in
between before completing all the activity successful. And the main focus to article is to replay the workflow from the activity it was faulted and continue
executing rest other activity as BAU. Why I came to this solution because, I am seeing this problem since long time in my project, and every time manual work was
required to complete the Orchestration of the workflow.
In my previous article
I had provided the solution of re-playing the activity in workflow. And here how to re-play the workflow. Both are two different concept and different requirement as per our need.
Background
Idea behind this solution is to provide advance replay functionality to our Orchestration system, built on Windows workflow foundation.
Suppose you have many sets of activity in your workflow and each activity do some write/update/calculation/etc with data in its respective activity.
And in some case one of the activity get faulted and terminates the workflow. Now, in this scenario we can't replay the workflow from start,
because it will/may execute same activity which was already completed before. Basically we want replay to happen from the point where it was faulted.
Basic Understanding
Before I approach to solution to the problem. Let me explain the basics of WWF and we will try to find the solution in its basic understanding.
As we know WorkflowRuntime is used to create workflow in run-time.
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(WorkflowConsoleApplication1.Workflow1));
CreateWorkflow gives the WorkflowInstace which load the complete workflow in the memory thread.
instance.Start();
And we tell workflow to start executing each activity one by one.
There is a class WorkflowChanges which can we used during run time to make any changes in workflow.
Preparing strategy for solution
- You should have logging mechanism in your workflow application using workflow tracking service
- You should log each activity state, so that you know due which activity faulting your workflow got terminated
- At time of replay of workflow you should know the name of activity from where you want to replay the workflow.
Once you have above functionality in your application, now i can proceed explaining other bit required for replay.
- Create the workflow instance
- Access the workflow inside WorkflowInstace
- Initiate WorkflowChanges object for your original workflow
- Workflow change object will load your original workflow in TransientWorkflow
- Now loop in all activities inside TransientWorkflow and remove all activities which already executed before and leave all those activity
requires to execute.
- Now apply the WorkflowChanges to orginal WorkflowInstace
- Then start the WorkflowInstace
- Done you have made required change which need to replay.
Using the code
This is the workflow in my sample POC project. And i am assuming my workflow got terminated at ReplayfromThisActivity
Activity in this below workflow.
Now, Let see how this code will look like as per our requirement to replay the workflow from the faulted activity and skip activities which already executed.
var workflowRoot = (Workflow1)instance.GetWorkflowDefinition();
WorkflowChanges changes = new WorkflowChanges(workflowRoot);
System.Collections.Generic.List<Activity> exectuedActivities =
new System.Collections.Generic.List<Activity>();
foreach (Activity childActivity in changes.TransientWorkflow.Activities)
{
if (string.Compare(childActivity.Name, "ReplayfromThisActivity") == 0)
{
break;
}
exectuedActivities.Add(childActivity);
}
if (exectuedActivities.Count > 0)
{
exectuedActivities.ForEach(childActivty =>
{
changes.TransientWorkflow.Activities.Remove(childActivty);
});
}
instance.ApplyWorkflowChanges(changes);
As you can see in this code I have followed all my steps which i have pointed above. To skip executing activity,
I have removed those activities from the workflow and kept rest other as it is, so that when workflow start executing the 1st activity it will get to execute is the activity from where
I wanted to replay the workflow.
That all in this article, hope this will help you.