diff --git a/src/WorkflowEngine.Core/Action.cs b/src/WorkflowEngine.Core/Action.cs index bd6dd38..1ae767b 100644 --- a/src/WorkflowEngine.Core/Action.cs +++ b/src/WorkflowEngine.Core/Action.cs @@ -14,6 +14,7 @@ public class Action : IAction, IFormattable public int Index { get; set; } public bool ScopeMoveNext { get; set; } + public string PrincipalId { get; set; } public string ToString(string format, IFormatProvider formatProvider) { if (format == "Type") diff --git a/src/WorkflowEngine.Core/Expressions/OutputsFunction.cs b/src/WorkflowEngine.Core/Expressions/OutputsFunction.cs index dc17fc1..3c3170b 100644 --- a/src/WorkflowEngine.Core/Expressions/OutputsFunction.cs +++ b/src/WorkflowEngine.Core/Expressions/OutputsFunction.cs @@ -24,7 +24,7 @@ public override async ValueTask ExecuteFunction(params ValueCont var key = parameters[0].ToString(); if (scopeContext.Scope.Contains(".")) - { + { key = $"{scopeContext.Scope.Substring(0, scopeContext.Scope.LastIndexOf('.'))}.{key}"; } diff --git a/src/WorkflowEngine.Core/IWorkflowExecutor.cs b/src/WorkflowEngine.Core/IWorkflowExecutor.cs index 8b0e35a..693fe49 100644 --- a/src/WorkflowEngine.Core/IWorkflowExecutor.cs +++ b/src/WorkflowEngine.Core/IWorkflowExecutor.cs @@ -6,6 +6,16 @@ namespace WorkflowEngine.Core public interface IRunContext { Guid RunId { get; set; } + + string PrincipalId { get; set; } + + public T CopyTo(T other) + where T : IRunContext + { + other.RunId = RunId; + other.PrincipalId = PrincipalId; + return other; + } } public interface IWorkflowExecutor { diff --git a/src/WorkflowEngine.Core/TriggerContext.cs b/src/WorkflowEngine.Core/TriggerContext.cs index e7e2ee3..dae51be 100644 --- a/src/WorkflowEngine.Core/TriggerContext.cs +++ b/src/WorkflowEngine.Core/TriggerContext.cs @@ -6,6 +6,7 @@ public class TriggerContext : ITriggerContext, IFormattable { public IWorkflow Workflow { get; set; } public ITrigger Trigger { get; set; } + public string PrincipalId { get; set; } public Guid RunId { get; set; } diff --git a/src/WorkflowEngine.Core/WorkflowExecutor.cs b/src/WorkflowEngine.Core/WorkflowExecutor.cs index d890f47..04e957a 100644 --- a/src/WorkflowEngine.Core/WorkflowExecutor.cs +++ b/src/WorkflowEngine.Core/WorkflowExecutor.cs @@ -39,7 +39,7 @@ public ValueTask GetNextAction(IRunContext context, IWorkflow workflow, if (next.Value.ShouldRun(priorResult.Key,priorResult.Status)) // .RunAfter[priorResult.Key].Contains(priorResult.Status)) { - return new ValueTask(new Action { RunId=context.RunId, Type = next.Value.Type, Key=next.Key, ScheduledTime=DateTimeOffset.UtcNow }); + return new ValueTask(context.CopyTo(new Action { Type = next.Value.Type, Key=next.Key, ScheduledTime=DateTimeOffset.UtcNow })); } return new ValueTask(); @@ -58,7 +58,9 @@ public async ValueTask Trigger(ITriggerContext context) if (action.IsDefault()) return null; - return new Action { Type = action.Value.Type, Key=action.Key, ScheduledTime = DateTimeOffset.UtcNow, RunId = context.RunId }; + return context.CopyTo( + new Action { Type = action.Value.Type, Key=action.Key, ScheduledTime = DateTimeOffset.UtcNow } + ); } } diff --git a/src/WorkflowEngine.Hangfire/ForloopAction.cs b/src/WorkflowEngine.Hangfire/ForloopAction.cs index 1913a19..5e233e6 100644 --- a/src/WorkflowEngine.Hangfire/ForloopAction.cs +++ b/src/WorkflowEngine.Hangfire/ForloopAction.cs @@ -45,7 +45,7 @@ public async ValueTask ExecuteAsync(IRunContext context, IWorkflow workf // var nextAction = new Action { Type = action.Type, Key=action.Key, ScheduledTime = DateTimeOffset.UtcNow, RunId = context.RunId, Index = action.Index+1 }; var nextactionmetadata = loop.Actions.SingleOrDefault(c => c.Value.RunAfter?.Count == 0); - var nextaction = new Action { Type = nextactionmetadata.Value.Type, Key= $"{action.Key}.{nextactionmetadata.Key}", ScheduledTime = DateTimeOffset.UtcNow, RunId = context.RunId, Index=action.Index }; + var nextaction = context.CopyTo( new Action { Type = nextactionmetadata.Value.Type, Key= $"{action.Key}.{nextactionmetadata.Key}", ScheduledTime = DateTimeOffset.UtcNow, Index=action.Index }); var a = backgroundJobClient.ContinueJobWith(arrayContext.JobId, (executor) => executor.ExecuteAsync(context, workflow, nextaction,null)); diff --git a/src/WorkflowEngine.Hangfire/HangfireWorkflowExecutor.cs b/src/WorkflowEngine.Hangfire/HangfireWorkflowExecutor.cs index 586fb63..e9f87b4 100644 --- a/src/WorkflowEngine.Hangfire/HangfireWorkflowExecutor.cs +++ b/src/WorkflowEngine.Hangfire/HangfireWorkflowExecutor.cs @@ -57,7 +57,7 @@ public async ValueTask ExecuteAsync(IRunContext run, IWorkflow workflow, }else if(workflow.Manifest.Actions.FindParentAction(action.Key) is ForLoopActionMetadata scope) { - var scopeaction= new Action {ScopeMoveNext=true, RunId=run.RunId, Type = scope.Type, Key=action.Key.Substring(0, action.Key.LastIndexOf('.')), ScheduledTime=DateTimeOffset.UtcNow }; + var scopeaction= run.CopyTo( new Action {ScopeMoveNext=true, Type = scope.Type, Key=action.Key.Substring(0, action.Key.LastIndexOf('.')), ScheduledTime=DateTimeOffset.UtcNow }); var a = backgroundJobClient.Enqueue( @@ -76,7 +76,9 @@ public async ValueTask ExecuteAsync(IRunContext run, IWorkflow workflow, /// public async ValueTask TriggerAsync(ITriggerContext context) { + context.RunId = context.RunId == Guid.Empty? Guid.NewGuid() : context.RunId; + runContextAccessor.RunContext = context; var action = await executor.Trigger(context); diff --git a/src/WorkflowEngine.Hangfire/WorkflowStarterBackgroundJob.cs b/src/WorkflowEngine.Hangfire/WorkflowStarterBackgroundJob.cs index 9226b07..6849f32 100644 --- a/src/WorkflowEngine.Hangfire/WorkflowStarterBackgroundJob.cs +++ b/src/WorkflowEngine.Hangfire/WorkflowStarterBackgroundJob.cs @@ -36,7 +36,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) if (!trigger.Equals(default(KeyValuePair))) { - + jobs.AddOrUpdate(workflow.Id.ToString(), (executor) => executor.TriggerAsync(new TriggerContext {