Skip to content

Commit ab004c0

Browse files
authored
Merge pull request danielgerlag#884 from VKAlwaysWin/VKAlwaysWin/AllowToDisableEventsPublisher
Allow to disable events publisher
2 parents 40787a1 + 133a7fe commit ab004c0

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

src/WorkflowCore/Models/WorkflowOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public WorkflowOptions(IServiceCollection services)
3838
public bool EnableEvents { get; set; } = true;
3939
public bool EnableIndexes { get; set; } = true;
4040
public bool EnablePolling { get; set; } = true;
41+
public bool EnableLifeCycleEventsPublisher { get; set; } = true;
4142

4243
public void UsePersistence(Func<IServiceProvider, IPersistenceProvider> factory)
4344
{

src/WorkflowCore/Services/ErrorHandlers/CompensateHandler.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@ namespace WorkflowCore.Services.ErrorHandlers
88
{
99
public class CompensateHandler : IWorkflowErrorHandler
1010
{
11-
private readonly ILifeCycleEventPublisher _eventPublisher;
1211
private readonly IExecutionPointerFactory _pointerFactory;
1312
private readonly IDateTimeProvider _datetimeProvider;
1413
private readonly WorkflowOptions _options;
1514

1615
public WorkflowErrorHandling Type => WorkflowErrorHandling.Compensate;
1716

18-
public CompensateHandler(IExecutionPointerFactory pointerFactory, ILifeCycleEventPublisher eventPublisher, IDateTimeProvider datetimeProvider, WorkflowOptions options)
17+
public CompensateHandler(IExecutionPointerFactory pointerFactory, IDateTimeProvider datetimeProvider, WorkflowOptions options)
1918
{
2019
_pointerFactory = pointerFactory;
21-
_eventPublisher = eventPublisher;
2220
_datetimeProvider = datetimeProvider;
2321
_options = options;
2422
}

src/WorkflowCore/Services/LifeCycleEventPublisher.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,30 @@
33
using System.Collections.Concurrent;
44
using System.Threading.Tasks;
55
using WorkflowCore.Interface;
6+
using WorkflowCore.Models;
67
using WorkflowCore.Models.LifeCycleEvents;
78

89
namespace WorkflowCore.Services
910
{
1011
public class LifeCycleEventPublisher : ILifeCycleEventPublisher, IDisposable
1112
{
1213
private readonly ILifeCycleEventHub _eventHub;
14+
private readonly WorkflowOptions _workflowOptions;
1315
private readonly ILogger _logger;
1416
private BlockingCollection<LifeCycleEvent> _outbox;
1517
private Task _dispatchTask;
1618

17-
public LifeCycleEventPublisher(ILifeCycleEventHub eventHub, ILoggerFactory loggerFactory)
19+
public LifeCycleEventPublisher(ILifeCycleEventHub eventHub, WorkflowOptions workflowOptions, ILoggerFactory loggerFactory)
1820
{
1921
_eventHub = eventHub;
22+
_workflowOptions = workflowOptions;
2023
_outbox = new BlockingCollection<LifeCycleEvent>();
2124
_logger = loggerFactory.CreateLogger(GetType());
2225
}
2326

2427
public void PublishNotification(LifeCycleEvent evt)
2528
{
26-
if (_outbox.IsAddingCompleted)
29+
if (_outbox.IsAddingCompleted || !_workflowOptions.EnableLifeCycleEventsPublisher)
2730
return;
2831

2932
_outbox.Add(evt);

test/WorkflowCore.UnitTests/Services/LifeCycleEventPublisherTests.cs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using System;
21
using System.Threading.Tasks;
2+
using Microsoft.Extensions.DependencyInjection;
33
using Microsoft.Extensions.Logging;
44
using Moq;
55
using WorkflowCore.Interface;
6+
using WorkflowCore.Models;
67
using WorkflowCore.Models.LifeCycleEvents;
78
using WorkflowCore.Services;
89
using Xunit;
@@ -17,17 +18,25 @@ public async Task PublishNotification_WhenStarted_PublishesNotification()
1718
// Arrange
1819
var wasCalled = new TaskCompletionSource<bool>();
1920
var eventHubMock = new Mock<ILifeCycleEventHub>();
21+
var serviceCollectionMock = new Mock<IServiceCollection>();
22+
23+
var workflowOptions = new WorkflowOptions(serviceCollectionMock.Object)
24+
{
25+
EnableLifeCycleEventsPublisher = true
26+
};
27+
2028
eventHubMock
2129
.Setup(hub => hub.PublishNotification(It.IsAny<StepCompleted>()))
2230
.Callback(() => wasCalled.SetResult(true));
23-
LifeCycleEventPublisher publisher = new LifeCycleEventPublisher(eventHubMock.Object, new LoggerFactory());
31+
LifeCycleEventPublisher publisher = new LifeCycleEventPublisher(eventHubMock.Object, workflowOptions, new LoggerFactory());
2432

2533
// Act
2634
publisher.Start();
2735
publisher.PublishNotification(new StepCompleted());
2836

2937
// Assert
3038
await wasCalled.Task;
39+
eventHubMock.Verify(hub => hub.PublishNotification(It.IsAny<StepCompleted>()), Times.Once());
3140
}
3241

3342
[Fact(DisplayName = "Notifications should be published when the publisher is running")]
@@ -36,10 +45,17 @@ public async Task PublishNotification_WhenRestarted_PublishesNotification()
3645
// Arrange
3746
var wasCalled = new TaskCompletionSource<bool>();
3847
var eventHubMock = new Mock<ILifeCycleEventHub>();
48+
var serviceCollectionMock = new Mock<IServiceCollection>();
49+
50+
var workflowOptions = new WorkflowOptions(serviceCollectionMock.Object)
51+
{
52+
EnableLifeCycleEventsPublisher = true
53+
};
54+
3955
eventHubMock
4056
.Setup(hub => hub.PublishNotification(It.IsAny<StepCompleted>()))
4157
.Callback(() => wasCalled.SetResult(true));
42-
LifeCycleEventPublisher publisher = new LifeCycleEventPublisher(eventHubMock.Object, new LoggerFactory());
58+
LifeCycleEventPublisher publisher = new LifeCycleEventPublisher(eventHubMock.Object, workflowOptions, new LoggerFactory());
4359

4460
// Act
4561
publisher.Start();
@@ -49,6 +65,33 @@ public async Task PublishNotification_WhenRestarted_PublishesNotification()
4965

5066
// Assert
5167
await wasCalled.Task;
68+
eventHubMock.Verify(hub => hub.PublishNotification(It.IsAny<StepCompleted>()), Times.Once());
69+
}
70+
71+
[Fact(DisplayName = "Notifications should be disabled if option EnableLifeCycleEventsPublisher is disabled")]
72+
public void PublishNotification_Disabled()
73+
{
74+
// Arrange
75+
var eventHubMock = new Mock<ILifeCycleEventHub>();
76+
var serviceCollectionMock = new Mock<IServiceCollection>();
77+
78+
var workflowOptions = new WorkflowOptions(serviceCollectionMock.Object)
79+
{
80+
EnableLifeCycleEventsPublisher = false
81+
};
82+
83+
eventHubMock
84+
.Setup(hub => hub.PublishNotification(It.IsAny<StepCompleted>()))
85+
.Returns(Task.CompletedTask);
86+
LifeCycleEventPublisher publisher = new LifeCycleEventPublisher(eventHubMock.Object, workflowOptions, new LoggerFactory());
87+
88+
// Act
89+
publisher.Start();
90+
publisher.PublishNotification(new StepCompleted());
91+
publisher.Stop();
92+
93+
// Assert
94+
eventHubMock.Verify(hub => hub.PublishNotification(It.IsAny<StepCompleted>()), Times.Never());
5295
}
5396
}
5497
}

0 commit comments

Comments
 (0)