Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick fix for sending WorkflowCompleted lifecycle event when EndWorkflow in workflow definition is reached. #912

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 31 additions & 33 deletions src/WorkflowCore/Services/WorkflowExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,50 +218,48 @@ private async Task DetermineNextExecutionTime(WorkflowInstance workflow, Workflo
//TODO: move to own class
workflow.NextExecution = null;

if (workflow.Status == WorkflowStatus.Complete)
if (workflow.Status != WorkflowStatus.Complete)
{
return;
}

foreach (var pointer in workflow.ExecutionPointers.Where(x => x.Active && (x.Children ?? new List<string>()).Count == 0))
{
if (!pointer.SleepUntil.HasValue)
foreach (var pointer in workflow.ExecutionPointers.Where(x => x.Active && (x.Children ?? new List<string>()).Count == 0))
{
workflow.NextExecution = 0;
return;
if (!pointer.SleepUntil.HasValue)
{
workflow.NextExecution = 0;
return;
}

var pointerSleep = pointer.SleepUntil.Value.ToUniversalTime().Ticks;
workflow.NextExecution = Math.Min(pointerSleep, workflow.NextExecution ?? pointerSleep);
}

var pointerSleep = pointer.SleepUntil.Value.ToUniversalTime().Ticks;
workflow.NextExecution = Math.Min(pointerSleep, workflow.NextExecution ?? pointerSleep);
}
foreach (var pointer in workflow.ExecutionPointers.Where(x => x.Active && (x.Children ?? new List<string>()).Count > 0))
{
if (!workflow.ExecutionPointers.FindByScope(pointer.Id).All(x => x.EndTime.HasValue))
continue;

foreach (var pointer in workflow.ExecutionPointers.Where(x => x.Active && (x.Children ?? new List<string>()).Count > 0))
{
if (!workflow.ExecutionPointers.FindByScope(pointer.Id).All(x => x.EndTime.HasValue))
continue;
if (!pointer.SleepUntil.HasValue)
{
workflow.NextExecution = 0;
return;
}

var pointerSleep = pointer.SleepUntil.Value.ToUniversalTime().Ticks;
workflow.NextExecution = Math.Min(pointerSleep, workflow.NextExecution ?? pointerSleep);
}

if (!pointer.SleepUntil.HasValue)
if ((workflow.NextExecution != null) || (workflow.ExecutionPointers.Any(x => x.EndTime == null)))
{
workflow.NextExecution = 0;
return;
}

var pointerSleep = pointer.SleepUntil.Value.ToUniversalTime().Ticks;
workflow.NextExecution = Math.Min(pointerSleep, workflow.NextExecution ?? pointerSleep);
}

if ((workflow.NextExecution != null) || (workflow.ExecutionPointers.Any(x => x.EndTime == null)))
{
return;
}
workflow.Status = WorkflowStatus.Complete;
workflow.CompleteTime = _datetimeProvider.UtcNow;

workflow.Status = WorkflowStatus.Complete;
workflow.CompleteTime = _datetimeProvider.UtcNow;

using (var scope = _serviceProvider.CreateScope())
{
var middlewareRunner = scope.ServiceProvider.GetRequiredService<IWorkflowMiddlewareRunner>();
await middlewareRunner.RunPostMiddleware(workflow, def);
using (var scope = _serviceProvider.CreateScope())
{
var middlewareRunner = scope.ServiceProvider.GetRequiredService<IWorkflowMiddlewareRunner>();
await middlewareRunner.RunPostMiddleware(workflow, def);
}
}

_publisher.PublishNotification(new WorkflowCompleted
Expand Down
11 changes: 11 additions & 0 deletions test/WorkflowCore.IntegrationTests/Scenarios/EndStepScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using WorkflowCore.Interface;
using WorkflowCore.Models;
using WorkflowCore.Models.LifeCycleEvents;
using WorkflowCore.Testing;
using Xunit;

Expand All @@ -13,6 +14,8 @@ public class EndStepScenario : WorkflowTest<EndStepScenario.ScenarioWorkflow, Ob
internal static int MidStepCounter = 0;
internal static int EndStepCounter = 0;

private int workflowCompletedEventTriggered = 0;

public class ScenarioWorkflow : IWorkflow
{
public string Id => "EndStepScenario";
Expand Down Expand Up @@ -49,13 +52,21 @@ public EndStepScenario()
[Fact]
public void Scenario()
{
Host.OnLifeCycleEvent += (e) =>
{
if (e is WorkflowCompleted)
{
workflowCompletedEventTriggered++;
}
};
var workflowId = StartWorkflow(null);
WaitForWorkflowToComplete(workflowId, TimeSpan.FromSeconds(30));

GetStatus(workflowId).Should().Be(WorkflowStatus.Complete);
StartStepCounter.Should().Be(1);
MidStepCounter.Should().Be(1);
EndStepCounter.Should().Be(0);
workflowCompletedEventTriggered.Should().Be(1);
}
}
}