Skip to content

Commit

Permalink
Remove multiple execution scopes (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfmskywalker authored Oct 26, 2020
1 parent 16611e2 commit d5095f7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,13 @@ protected override async Task<ActivityExecutionResult> OnExecuteAsync(

if (index >= collection.Count)
{
context.EndScope();
CurrentIndex = 0;
return Done();
}

var value = collection[index];
CurrentIndex++;

if (index == 0)
{
context.BeginScope();
}

context.CurrentScope.SetVariable(IteratorName, value);

return Outcome(OutcomeNames.Iterate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,10 @@ protected override async Task<ActivityExecutionResult> OnExecuteAsync(
{
var loop = await expressionEvaluator.EvaluateAsync(ConditionExpression, context, cancellationToken);

if (HasStarted)
context.EndScope();

if (loop)
{
HasStarted = true;

context.BeginScope();

return Outcome(OutcomeNames.Iterate);
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/Elsa.Abstractions/Models/WorkflowInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class WorkflowInstance
public Instant? FaultedAt { get; set; }
public Instant? AbortedAt { get; set; }
public IDictionary<string, ActivityInstance> Activities { get; set; } = new Dictionary<string, ActivityInstance>();
public Stack<WorkflowExecutionScope> Scopes { get; set; }
public WorkflowExecutionScope Scope { get; set; }
public Variables Input { get; set; }
public HashSet<BlockingActivity> BlockingActivities { get; set; }
public ICollection<LogEntry> ExecutionLog { get; set; }
Expand Down
13 changes: 5 additions & 8 deletions src/core/Elsa.Abstractions/Services/Models/Workflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ public Workflow(
Input = new Variables(input ?? Variables.Empty);
}



public Workflow()
{
Scopes = new Stack<WorkflowExecutionScope>(new[] { new WorkflowExecutionScope() });
Scope = new WorkflowExecutionScope();
BlockingActivities = new HashSet<IActivity>();
ExecutionLog = new List<LogEntry>();
}
Expand All @@ -48,7 +46,7 @@ public Workflow()
public Instant? AbortedAt { get; set; }
public ICollection<IActivity> Activities { get; } = new List<IActivity>();
public IList<Connection> Connections { get; } = new List<Connection>();
public Stack<WorkflowExecutionScope> Scopes { get; set; }
public WorkflowExecutionScope Scope { get; set; }
public HashSet<IActivity> BlockingActivities { get; set; }
public IList<LogEntry> ExecutionLog { get; set; }
public WorkflowFault Fault { get; set; }
Expand All @@ -72,7 +70,7 @@ public WorkflowInstance ToInstance()
FaultedAt = FaultedAt,
AbortedAt = AbortedAt,
Activities = activities,
Scopes = new Stack<WorkflowExecutionScope>(Scopes),
Scope = Scope,

BlockingActivities = new HashSet<BlockingActivity>(
BlockingActivities.Select(x => new BlockingActivity(x.Id, x.Type)),
Expand Down Expand Up @@ -100,12 +98,11 @@ public void Initialize(WorkflowInstance instance)
FaultedAt = instance.FaultedAt;
AbortedAt = instance.AbortedAt;
ExecutionLog = instance.ExecutionLog.ToList();
Scope = instance.Scope;

BlockingActivities =
new HashSet<IActivity>(instance.BlockingActivities.Select(x => activityLookup[x.ActivityId]));

Scopes = new Stack<WorkflowExecutionScope>(instance.Scopes);


foreach (var activity in Activities)
{
activity.State = new JObject(instance.Activities[activity.Id].State);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public WorkflowExecutionContext(Workflow workflowInstance, IClock clock, IServic
public IEnumerable<IActivity> ScheduledActivities => scheduledActivities;
public bool IsFirstPass { get; set; }
public LogEntry CurrentLogEntry => Workflow.ExecutionLog.LastOrDefault();
public WorkflowExecutionScope CurrentScope => Workflow.Scopes.Peek();
public WorkflowExecutionScope CurrentScope => Workflow.Scope;
public Variables TransientState { get; } = new Variables();
public IActivity CurrentActivity { get; private set; }
public void ScheduleActivities(params IActivity[] activities) => ScheduleActivities((IEnumerable<IActivity>)activities);
Expand All @@ -48,9 +48,6 @@ public void ScheduleActivities(IEnumerable<IActivity> activities)
}
}

public void BeginScope() => Workflow.Scopes.Push(new WorkflowExecutionScope());
public void EndScope() => Workflow.Scopes.Pop();

public void ScheduleActivity(IActivity activity)
{
scheduledActivities.Push(activity);
Expand All @@ -62,21 +59,9 @@ public void ScheduleActivity(IActivity activity)
public IActivity PopScheduledHaltingActivity() => scheduledHaltingActivities.Pop();
public IWorkflowExpressionEvaluator ExpressionEvaluator { get; }

public void SetVariable(string name, object value)
{
// Get the first scope (starting from the oldest one) containing the variable (existing variable). Otherwise use the current scope (new variable declaration)
var scope = Workflow.Scopes.Reverse().FirstOrDefault(x => x.Variables.ContainsKey(name)) ?? CurrentScope;
scope.SetVariable(name, value);
}

public void SetVariable(string name, object value) => CurrentScope.SetVariable(name, value);
public T GetVariable<T>(string name) => (T) GetVariable(name);

public object GetVariable(string name)
{
// Get the first scope (starting from the newest one) containing the variable.
var scope = Workflow.Scopes.FirstOrDefault(x => x.Variables.ContainsKey(name)) ?? CurrentScope;
return scope.GetVariable(name);
}
public object GetVariable(string name) => CurrentScope.GetVariable(name);

public Task<T> EvaluateAsync<T>(IWorkflowExpression<T> expression, CancellationToken cancellationToken) =>
ExpressionEvaluator.EvaluateAsync(expression, this, cancellationToken);
Expand Down Expand Up @@ -123,9 +108,6 @@ public void Abort()
Workflow.Status = WorkflowStatus.Aborted;
}

public Variables GetVariables() => Workflow.Scopes
.Reverse()
.Select(x => x.Variables)
.Aggregate(Variables.Empty, (x, y) => new Variables(x.Union(y)));
public Variables GetVariables() => CurrentScope.Variables;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ string connectionStringName
)
{
NodaTimeSerializers.Register();
RegisterEnumAsStringConvention();
RegisterConventions();
BsonSerializer.RegisterSerializer(new JObjectSerializer());
BsonSerializer.RegisterSerializer(new WorkflowExecutionScopeSerializer());

Expand Down Expand Up @@ -90,11 +90,17 @@ private static IMongoClient CreateDbClient(IConfiguration configuration, string
return new MongoClient(connectionString);
}

private static void RegisterEnumAsStringConvention()
private static void RegisterConventions()
{
var pack = new ConventionPack { new EnumRepresentationConvention(BsonType.String) };

ConventionRegistry.Register("EnumStringConvention", pack, _ => true);

BsonClassMap.RegisterClassMap<WorkflowInstance>(cm =>
{
cm.AutoMap();
cm.SetIgnoreExtraElements(true);
});
}
}
}

0 comments on commit d5095f7

Please sign in to comment.