Skip to content

Commit 4171ba9

Browse files
committed
started undertaking adding support for cancellation in domain events but pretty much all storage API's aren't supporting cancellation....
1 parent 696f1c8 commit 4171ba9

File tree

18 files changed

+52
-35
lines changed

18 files changed

+52
-35
lines changed

src/ServiceControl.AcceptanceTests.RavenDB/Recoverability/MessageFailures/When_a_message_fails_to_import.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace ServiceControl.AcceptanceTests.RavenDB.Recoverability.MessageFailures
22
{
33
using System;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using AcceptanceTesting;
67
using AcceptanceTesting.EndpointTemplates;
@@ -75,7 +76,7 @@ public async Task It_can_be_reimported()
7576

7677
class MessageFailedHandler(MyContext scenarioContext) : IDomainHandler<MessageFailed>
7778
{
78-
public Task Handle(MessageFailed domainEvent)
79+
public Task Handle(MessageFailed domainEvent, CancellationToken cancellationToken)
7980
{
8081
scenarioContext.MessageFailedEventPublished = true;
8182
return Task.CompletedTask;

src/ServiceControl.DomainEvents/DomainEvents.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace ServiceControl.Infrastructure.DomainEvents
22
{
33
using System;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using Microsoft.Extensions.DependencyInjection;
67
using NServiceBus.Logging;
@@ -12,14 +13,14 @@ public class DomainEvents : IDomainEvents
1213
readonly IServiceProvider serviceProvider;
1314
public DomainEvents(IServiceProvider serviceProvider) => this.serviceProvider = serviceProvider;
1415

15-
public async Task Raise<T>(T domainEvent) where T : IDomainEvent
16+
public async Task Raise<T>(T domainEvent, CancellationToken cancellationToken) where T : IDomainEvent
1617
{
1718
var handlers = serviceProvider.GetServices<IDomainHandler<T>>();
1819
foreach (var handler in handlers)
1920
{
2021
try
2122
{
22-
await handler.Handle(domainEvent)
23+
await handler.Handle(domainEvent, cancellationToken)
2324
.ConfigureAwait(false);
2425
}
2526
catch (Exception e)
@@ -34,7 +35,7 @@ await handler.Handle(domainEvent)
3435
{
3536
try
3637
{
37-
await handler.Handle(domainEvent)
38+
await handler.Handle(domainEvent, cancellationToken)
3839
.ConfigureAwait(false);
3940
}
4041
catch (Exception e)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
namespace ServiceControl.Infrastructure.DomainEvents
22
{
3+
using System.Threading;
34
using System.Threading.Tasks;
45

56
public interface IDomainEvents
67
{
7-
Task Raise<T>(T domainEvent) where T : IDomainEvent;
8+
Task Raise<T>(T domainEvent, CancellationToken cancellationToken = default) where T : IDomainEvent;
89
}
910
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
namespace ServiceControl.Infrastructure.DomainEvents
22
{
3+
using System.Threading;
34
using System.Threading.Tasks;
45

56
public interface IDomainHandler<in T> where T : IDomainEvent
67
{
7-
Task Handle(T domainEvent);
8+
Task Handle(T domainEvent, CancellationToken cancellationToken = default);
89
}
910
}

src/ServiceControl.Persistence.Tests/FakeDomainEvents.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Text.Json;
55
using System.Text.Json.Serialization;
6+
using System.Threading;
67
using System.Threading.Tasks;
78
using NUnit.Framework;
89
using ServiceControl.Infrastructure.DomainEvents;
@@ -12,7 +13,7 @@ class FakeDomainEvents : IDomainEvents
1213
{
1314
public List<object> RaisedEvents { get; } = [];
1415

15-
public Task Raise<T>(T domainEvent) where T : IDomainEvent
16+
public Task Raise<T>(T domainEvent, CancellationToken cancellationToken) where T : IDomainEvent
1617
{
1718
RaisedEvents.Add(domainEvent);
1819
TestContext.Out.WriteLine($"Raised DomainEvent {typeof(T).Name}:");

src/ServiceControl.Persistence/Recoverability/Archiving/InMemoryArchive.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace ServiceControl.Recoverability
22
{
33
using System;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using Infrastructure.DomainEvents;
67

@@ -51,7 +52,7 @@ public Task Start()
5152
ArchiveType = ArchiveType,
5253
Progress = GetProgress(),
5354
StartTime = Started
54-
});
55+
}, CancellationToken.None);
5556
}
5657

5758
public Task BatchArchived(int numberOfMessagesArchivedInBatch)
@@ -68,7 +69,7 @@ public Task BatchArchived(int numberOfMessagesArchivedInBatch)
6869
Progress = GetProgress(),
6970
StartTime = Started,
7071
Last = Last.Value
71-
});
72+
}, CancellationToken.None);
7273
}
7374

7475
public Task FinalizeArchive()
@@ -84,7 +85,7 @@ public Task FinalizeArchive()
8485
Progress = GetProgress(),
8586
StartTime = Started,
8687
Last = Last.Value
87-
});
88+
}, CancellationToken.None);
8889
}
8990

9091
public Task Complete()
@@ -103,7 +104,7 @@ public Task Complete()
103104
Last = Last.Value,
104105
CompletionTime = CompletionTime.Value,
105106
GroupName = GroupName
106-
});
107+
}, CancellationToken.None);
107108
}
108109

109110
public bool NeedsAcknowledgement()

src/ServiceControl.Persistence/Recoverability/Archiving/InMemoryUnarchive.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace ServiceControl.Recoverability
22
{
33
using System;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using Infrastructure.DomainEvents;
67

@@ -51,7 +52,7 @@ public Task Start()
5152
ArchiveType = ArchiveType,
5253
Progress = GetProgress(),
5354
StartTime = Started
54-
});
55+
}, CancellationToken.None);
5556
}
5657

5758
public Task BatchUnarchived(int numberOfMessagesUnarchivedInBatch)
@@ -68,7 +69,7 @@ public Task BatchUnarchived(int numberOfMessagesUnarchivedInBatch)
6869
Progress = GetProgress(),
6970
StartTime = Started,
7071
Last = Last.Value
71-
});
72+
}, CancellationToken.None);
7273
}
7374

7475
public Task FinalizeUnarchive()
@@ -84,7 +85,7 @@ public Task FinalizeUnarchive()
8485
Progress = GetProgress(),
8586
StartTime = Started,
8687
Last = Last.Value
87-
});
88+
}, CancellationToken.None);
8889
}
8990

9091
public Task Complete()
@@ -103,7 +104,7 @@ public Task Complete()
103104
Last = Last.Value,
104105
CompletionTime = CompletionTime.Value,
105106
GroupName = GroupName
106-
});
107+
}, CancellationToken.None);
107108
}
108109

109110
internal bool NeedsAcknowledgement()

src/ServiceControl.UnitTests/FakeDomainEvents.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
namespace ServiceControl.UnitTests.Operations
22
{
33
using System.Collections.Generic;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using ServiceControl.Infrastructure.DomainEvents;
67

78
class FakeDomainEvents : IDomainEvents
89
{
910
public List<object> RaisedEvents { get; } = [];
1011

11-
public Task Raise<T>(T domainEvent) where T : IDomainEvent
12+
public Task Raise<T>(T domainEvent, CancellationToken cancellationToken = default) where T : IDomainEvent
1213
{
1314
RaisedEvents.Add(domainEvent);
1415
return Task.CompletedTask;

src/ServiceControl.UnitTests/Monitoring/EndpointInstanceMonitoringTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace ServiceControl.UnitTests.Monitoring
22
{
33
using System;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using NUnit.Framework;
67
using ServiceControl.Infrastructure.DomainEvents;
@@ -33,7 +34,7 @@ public async Task When_endpoint_removed_should_stay_removed()
3334

3435
class FakeDomainEvents : IDomainEvents
3536
{
36-
public Task Raise<T>(T domainEvent) where T : IDomainEvent
37+
public Task Raise<T>(T domainEvent, CancellationToken cancellationToken) where T : IDomainEvent
3738
{
3839
return Task.CompletedTask;
3940
}

src/ServiceControl/EventLog/AuditEventLogWriter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace ServiceControl.EventLog
22
{
3+
using System.Threading;
34
using System.Threading.Tasks;
45
using Contracts.EventLog;
56
using Infrastructure.DomainEvents;
@@ -19,7 +20,7 @@ public AuditEventLogWriter(GlobalEventHandler broadcaster, IErrorMessageDataStor
1920
this.mappings = mappings;
2021
}
2122

22-
public async Task Handle(IDomainEvent message)
23+
public async Task Handle(IDomainEvent message, CancellationToken cancellationToken)
2324
{
2425
if (!mappings.HasMapping(message))
2526
{
@@ -41,7 +42,7 @@ await broadcaster.Broadcast(new EventLogItemAdded
4142
// The reason is because this data is not useful for end users, so for now we just empty it.
4243
// At the moment too much data is being populated in this field, and this has significant down sides to the amount of data we are sending down to ServicePulse (it actually crashes it).
4344
RelatedTo = []
44-
});
45+
}, cancellationToken);
4546
}
4647

4748
readonly GlobalEventHandler broadcaster;

0 commit comments

Comments
 (0)