Merge pull request #6501 from elsa-workflows/bug/faulting-executing
Refactor activity execution state handling with IDisposable
This commit is contained in:
commit
714d8b6f3d
|
|
@ -177,6 +177,11 @@ public partial class ActivityExecutionContext : IExecutionContext, IDisposable
|
|||
}
|
||||
}
|
||||
|
||||
public IDisposable EnterExecution()
|
||||
{
|
||||
return new WorkflowExecutionState(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the current status of the activity.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -48,10 +48,9 @@ public class DefaultActivityInvokerMiddleware(ActivityMiddlewareDelegate next, I
|
|||
context.AddExecutionLogEntry("Precondition Failed", "Cannot execute at this time");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Mark workflow and activity as executing.
|
||||
context.WorkflowExecutionContext.IsExecuting = true;
|
||||
context.IsExecuting = true;
|
||||
using var executionState = context.EnterExecution();
|
||||
|
||||
// Conditionally commit the workflow state.
|
||||
if (ShouldCommit(context, ActivityLifetimeEvent.ActivityExecuting))
|
||||
|
|
@ -87,10 +86,6 @@ public class DefaultActivityInvokerMiddleware(ActivityMiddlewareDelegate next, I
|
|||
workflowExecutionContext.Bookmarks.AddRange(context.Bookmarks);
|
||||
logger.LogDebug("Added {BookmarkCount} bookmarks to the workflow execution context", context.Bookmarks.Count);
|
||||
}
|
||||
|
||||
// Mark activity as executed.
|
||||
context.IsExecuting = false;
|
||||
context.WorkflowExecutionContext.IsExecuting = false;
|
||||
|
||||
// Conditionally commit the workflow state.
|
||||
if (ShouldCommit(context, ActivityLifetimeEvent.ActivityExecuted))
|
||||
|
|
@ -151,8 +146,8 @@ public class DefaultActivityInvokerMiddleware(ActivityMiddlewareDelegate next, I
|
|||
{
|
||||
var workflowStrategyName = context.WorkflowExecutionContext.Workflow.Options.CommitStrategyName;
|
||||
var workflowStrategy = string.IsNullOrWhiteSpace(workflowStrategyName) ? null : commitStrategyRegistry.FindWorkflowStrategy(workflowStrategyName);
|
||||
|
||||
if(workflowStrategy == null)
|
||||
|
||||
if (workflowStrategy == null)
|
||||
return false;
|
||||
|
||||
var workflowLifetimeEvent = lifetimeEvent == ActivityLifetimeEvent.ActivityExecuting ? WorkflowLifetimeEvent.ActivityExecuting : WorkflowLifetimeEvent.ActivityExecuted;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
namespace Elsa.Workflows;
|
||||
|
||||
internal class WorkflowExecutionState : IDisposable
|
||||
{
|
||||
private readonly ActivityExecutionContext _activityExecutionContext;
|
||||
|
||||
public WorkflowExecutionState(ActivityExecutionContext activityExecutionContext)
|
||||
{
|
||||
_activityExecutionContext = activityExecutionContext;
|
||||
_activityExecutionContext.IsExecuting = activityExecutionContext.WorkflowExecutionContext.IsExecuting = true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_activityExecutionContext.IsExecuting = _activityExecutionContext.WorkflowExecutionContext.IsExecuting = false;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue