Skip to content

Commit

Permalink
fix: Added PrincipalId to the runcontext to hold an id of the user th…
Browse files Browse the repository at this point in the history
…at executed the workflow
  • Loading branch information
pksorensen committed Aug 3, 2022
1 parent 98eb6f4 commit 52cfb7f
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/WorkflowEngine.Core/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/WorkflowEngine.Core/Expressions/OutputsFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override async ValueTask<ValueContainer> ExecuteFunction(params ValueCont

var key = parameters[0].ToString();
if (scopeContext.Scope.Contains("."))
{
{
key = $"{scopeContext.Scope.Substring(0, scopeContext.Scope.LastIndexOf('.'))}.{key}";
}

Expand Down
10 changes: 10 additions & 0 deletions src/WorkflowEngine.Core/IWorkflowExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ namespace WorkflowEngine.Core
public interface IRunContext
{
Guid RunId { get; set; }

string PrincipalId { get; set; }

public T CopyTo<T>(T other)
where T : IRunContext
{
other.RunId = RunId;
other.PrincipalId = PrincipalId;
return other;
}
}
public interface IWorkflowExecutor
{
Expand Down
1 change: 1 addition & 0 deletions src/WorkflowEngine.Core/TriggerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
6 changes: 4 additions & 2 deletions src/WorkflowEngine.Core/WorkflowExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ValueTask<IAction> GetNextAction(IRunContext context, IWorkflow workflow,

if (next.Value.ShouldRun(priorResult.Key,priorResult.Status)) // .RunAfter[priorResult.Key].Contains(priorResult.Status))
{
return new ValueTask<IAction>(new Action { RunId=context.RunId, Type = next.Value.Type, Key=next.Key, ScheduledTime=DateTimeOffset.UtcNow });
return new ValueTask<IAction>(context.CopyTo(new Action { Type = next.Value.Type, Key=next.Key, ScheduledTime=DateTimeOffset.UtcNow }));
}

return new ValueTask<IAction>();
Expand All @@ -58,7 +58,9 @@ public async ValueTask<IAction> 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 }
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/WorkflowEngine.Hangfire/ForloopAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async ValueTask<object> 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<IHangfireActionExecutor>(arrayContext.JobId,
(executor) => executor.ExecuteAsync(context, workflow, nextaction,null));
Expand Down
4 changes: 3 additions & 1 deletion src/WorkflowEngine.Hangfire/HangfireWorkflowExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async ValueTask<object> 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<IHangfireActionExecutor>(
Expand All @@ -76,7 +76,9 @@ public async ValueTask<object> ExecuteAsync(IRunContext run, IWorkflow workflow,
/// <returns></returns>
public async ValueTask<object> TriggerAsync(ITriggerContext context)
{

context.RunId = context.RunId == Guid.Empty? Guid.NewGuid() : context.RunId;

runContextAccessor.RunContext = context;
var action = await executor.Trigger(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

if (!trigger.Equals(default(KeyValuePair<string, TriggerMetadata>)))
{

jobs.AddOrUpdate<IHangfireWorkflowExecutor>(workflow.Id.ToString(),
(executor) => executor.TriggerAsync(new TriggerContext
{
Expand Down

0 comments on commit 52cfb7f

Please sign in to comment.