Skip to content

Commit 0a6d6c0

Browse files
committed
fix: skipped workflow finished eventwhen action is scoped and has more and added queue for loops handling to keep on same queue
1 parent b9a269f commit 0a6d6c0

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

src/WorkflowEngine.Hangfire/ForloopAction.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ namespace WorkflowEngine.Core.Actions
1010
{
1111
public interface IArrayContext
1212
{
13-
public string JobId { get; set; }
13+
string JobId { get; set; }
14+
string Queue { get; set; }
15+
bool HasMore { get; set; }
1416
}
1517
public class ArrayContext : IArrayContext
1618
{
1719
public string JobId { get; set; }
20+
public string Queue { get; set; }
21+
public bool HasMore { get; set; }
1822
}
1923

2024
public class ForeachAction : IActionImplementation
@@ -35,7 +39,9 @@ public async ValueTask<object> ExecuteAsync(IRunContext context, IWorkflow workf
3539
{
3640

3741
var loop = workflow.Manifest.Actions.FindAction(action.Key) as ForLoopActionMetadata;
38-
42+
43+
arrayContext.HasMore = true;
44+
3945
if (loop.ForEach is string str && str.Contains("@"))
4046
{
4147
var items = await expressionEngine.ParseToValueContainer(str);
@@ -64,6 +70,7 @@ public async ValueTask<object> ExecuteAsync(IRunContext context, IWorkflow workf
6470
}
6571
else if (action.Index < itemsToRunover.Count)
6672
{
73+
6774
// var nextAction = new Action { Type = action.Type, Key=action.Key, ScheduledTime = DateTimeOffset.UtcNow, RunId = context.RunId, Index = action.Index+1 };
6875

6976
var nextactionmetadata = loop.Actions.SingleOrDefault(c => c.Value.RunAfter?.Count == 0);
@@ -73,17 +80,18 @@ public async ValueTask<object> ExecuteAsync(IRunContext context, IWorkflow workf
7380
var nextaction = context.CopyTo(new Action { Type = nextactionmetadata.Value.Type, Key = $"{action.Key}.{nextactionmetadata.Key}", ScheduledTime = DateTimeOffset.UtcNow, Index = action.Index+1 });
7481

7582

76-
var a = backgroundJobClient.ContinueJobWith<IHangfireActionExecutor>(arrayContext.JobId,
83+
var a = backgroundJobClient.ContinueJobWith<IHangfireActionExecutor>(arrayContext.JobId, arrayContext.Queue,
7784
(executor) => executor.ExecuteAsync(context, workflow, nextaction, null));
7885

7986
return new { item = itemsToRunover[action.Index] };
80-
}
87+
}
8188

8289
}
8390
}
8491

8592

8693

94+
arrayContext.HasMore = false;
8795

8896
return new { };
8997
}

src/WorkflowEngine.Hangfire/HangfireWorkflowExecutor.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ public async ValueTask<object> ExecuteAsync(IRunContext run, IWorkflow workflow,
8888
try
8989
{
9090
runContextAccessor.RunContext = run;
91-
arrayContext.JobId = context.BackgroundJob.Id;
91+
9292
var queue = context.BackgroundJob.Job.Queue ?? "default";
93+
arrayContext.JobId = context.BackgroundJob.Id;
94+
arrayContext.Queue = queue;
9395

9496
var result = await actionExecutor.ExecuteAsync(run, workflow, action);
9597

@@ -149,6 +151,10 @@ public async ValueTask<object> ExecuteAsync(IRunContext run, IWorkflow workflow,
149151
{
150152
await outputRepository.AddEvent(run, workflow, action, WorkflowEvent.CreateFinishedEvent(context.BackgroundJob.Id, result));
151153
throw new InvalidOperationException("Action failed: " + result.FailedReason) { Data = { ["ActionResult"] = result } };
154+
}
155+
else if ( workflow.Manifest.Actions.FindAction(action.Key) is IScopedActionMetadata scopedAction && arrayContext.HasMore)
156+
{
157+
152158
}
153159
else
154160
{

0 commit comments

Comments
 (0)