-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #444 from Particular/Expose_CustomChecks_Events
Added CustomCheckSucceeded and CustomCheckFailed event publishing for external integration
- Loading branch information
Showing
12 changed files
with
262 additions
and
9 deletions.
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
src/ServiceControl.AcceptanceTests/ExternalIntegrations/When_a_message_has_custom_checks.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
namespace ServiceBus.Management.AcceptanceTests.ExternalIntegrations | ||
{ | ||
using System; | ||
using Contexts; | ||
using NServiceBus; | ||
using NServiceBus.AcceptanceTesting; | ||
using NServiceBus.Config; | ||
using NServiceBus.Config.ConfigurationSource; | ||
using NServiceBus.Features; | ||
using NServiceBus.Unicast.Subscriptions; | ||
using NServiceBus.Unicast.Subscriptions.MessageDrivenSubscriptions; | ||
using NUnit.Framework; | ||
using ServiceControl.Contracts; | ||
using ServiceControl.Contracts.Operations; | ||
|
||
[TestFixture] | ||
public class When_a_message_has_custom_checks : AcceptanceTest | ||
{ | ||
[Test] | ||
public void Notification_should_be_published_on_the_bus() | ||
{ | ||
var context = new MyContext(); | ||
|
||
Scenario.Define(context) | ||
.WithEndpoint<ExternalIntegrationsManagementEndpoint>(b => b.Given((bus, c) => Subscriptions.OnEndpointSubscribed(s => | ||
{ | ||
if (s.SubscriberReturnAddress.Queue.Contains("ExternalProcessor")) | ||
{ | ||
c.ExternalProcessorSubscribed = true; | ||
} | ||
}, () => c.ExternalProcessorSubscribed = true)) | ||
.When(c => c.ExternalProcessorSubscribed, bus => | ||
{ | ||
bus.Publish(new ServiceControl.Contracts.CustomChecks.CustomCheckSucceeded | ||
{ | ||
Category = "Testing", | ||
CustomCheckId = "Success custom check", | ||
OriginatingEndpoint = new EndpointDetails | ||
{ | ||
Host = "MyHost", | ||
HostId = Guid.Empty, | ||
Name = "Testing" | ||
}, | ||
SucceededAt = DateTime.Now, | ||
}); | ||
bus.Publish(new ServiceControl.Contracts.CustomChecks.CustomCheckFailed | ||
{ | ||
Category = "Testing", | ||
CustomCheckId = "Fail custom check", | ||
OriginatingEndpoint = new EndpointDetails | ||
{ | ||
Host = "MyHost", | ||
HostId = Guid.Empty, | ||
Name = "Testing" | ||
}, | ||
FailedAt = DateTime.Now, | ||
FailureReason = "Because I can", | ||
}); | ||
}).AppConfig(PathToAppConfig)) | ||
.WithEndpoint<ExternalProcessor>(b => b.Given((bus, c) => | ||
{ | ||
bus.Subscribe<CustomCheckSucceeded>(); | ||
bus.Subscribe<CustomCheckFailed>(); | ||
})) | ||
.Done(c => c.CustomCheckFailedReceived && c.CustomCheckFailedReceived) | ||
.Run(); | ||
|
||
Assert.IsTrue(context.CustomCheckFailedReceived); | ||
Assert.IsTrue(context.CustomCheckSucceededReceived); | ||
} | ||
|
||
[Serializable] | ||
public class Subscriptions | ||
{ | ||
public static Action<Action<SubscriptionEventArgs>, Action> OnEndpointSubscribed = (actionToPerformIfMessageDrivenSubscriptions, actionToPerformIfMessageDrivenSubscriptionsNotRequired) => | ||
{ | ||
if (Feature.IsEnabled<MessageDrivenSubscriptions>()) | ||
{ | ||
Configure.Instance.Builder.Build<MessageDrivenSubscriptionManager>().ClientSubscribed += | ||
(sender, args) => | ||
{ | ||
actionToPerformIfMessageDrivenSubscriptions(args); | ||
}; | ||
|
||
return; | ||
} | ||
|
||
actionToPerformIfMessageDrivenSubscriptionsNotRequired(); | ||
}; | ||
} | ||
|
||
public class ExternalIntegrationsManagementEndpoint : EndpointConfigurationBuilder | ||
{ | ||
public ExternalIntegrationsManagementEndpoint() | ||
{ | ||
EndpointSetup<ExternalIntegrationsManagementEndpointSetup>(); | ||
} | ||
} | ||
|
||
public class ExternalProcessor : EndpointConfigurationBuilder | ||
{ | ||
public ExternalProcessor() | ||
{ | ||
EndpointSetup<JSonServer>(); | ||
} | ||
|
||
public class CustomCheckSucceededHandler : IHandleMessages<CustomCheckSucceeded> | ||
{ | ||
public MyContext Context { get; set; } | ||
|
||
public void Handle(CustomCheckSucceeded message) | ||
{ | ||
Context.CustomCheckSucceededReceived = true; | ||
} | ||
} | ||
|
||
public class CustomCheckFailedHandler : IHandleMessages<CustomCheckFailed> | ||
{ | ||
public MyContext Context { get; set; } | ||
|
||
public void Handle(CustomCheckFailed message) | ||
{ | ||
Context.CustomCheckFailedReceived = true; | ||
} | ||
} | ||
|
||
public class UnicastOverride : IProvideConfiguration<UnicastBusConfig> | ||
{ | ||
public UnicastBusConfig GetConfiguration() | ||
{ | ||
var config = new UnicastBusConfig(); | ||
var serviceControlMapping = new MessageEndpointMapping | ||
{ | ||
Messages = "ServiceControl.Contracts", | ||
Endpoint = "Particular.ServiceControl" | ||
}; | ||
config.MessageEndpointMappings.Add(serviceControlMapping); | ||
return config; | ||
} | ||
} | ||
} | ||
|
||
public class MyContext : ScenarioContext | ||
{ | ||
public bool CustomCheckSucceededReceived { get; set; } | ||
public bool CustomCheckFailedReceived { get; set; } | ||
public bool ExternalProcessorSubscribed { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/ServiceControl/ExternalIntegrations/CustomCheckFailedPublisher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
namespace ServiceControl.ExternalIntegrations | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Raven.Client; | ||
using CustomCheckFailed = ServiceControl.Contracts.CustomCheckFailed; | ||
|
||
|
||
public class CustomCheckFailedPublisher : EventPublisher<Contracts.CustomChecks.CustomCheckFailed, CustomCheckFailedPublisher.DispatchContext> | ||
{ | ||
protected override DispatchContext CreateDispatchRequest(Contracts.CustomChecks.CustomCheckFailed @event) | ||
{ | ||
return new DispatchContext | ||
{ | ||
EndpointHost = @event.OriginatingEndpoint.Host, | ||
EndpointHostId = @event.OriginatingEndpoint.HostId, | ||
EndpointName = @event.OriginatingEndpoint.Name, | ||
FailedAt = @event.FailedAt, | ||
Category = @event.Category, | ||
FailureReason = @event.FailureReason, | ||
CustomCheckId = @event.CustomCheckId, | ||
}; | ||
} | ||
|
||
protected override IEnumerable<object> PublishEvents(IEnumerable<DispatchContext> contexts, IDocumentSession session) | ||
{ | ||
return contexts.Select(r => new CustomCheckFailed | ||
{ | ||
FailedAt = r.FailedAt, | ||
Category = r.Category, | ||
FailureReason = r.FailureReason, | ||
CustomCheckId = r.CustomCheckId, | ||
Host = r.EndpointHost, | ||
HostId = r.EndpointHostId, | ||
EndpointName = r.EndpointName | ||
}); | ||
} | ||
|
||
public class DispatchContext | ||
{ | ||
public string EndpointName { get; set; } | ||
public Guid EndpointHostId { get; set; } | ||
public string EndpointHost { get; set; } | ||
public string CustomCheckId { get; set; } | ||
public string Category { get; set; } | ||
public string FailureReason { get; set; } | ||
public DateTime FailedAt { get; set; } | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/ServiceControl/ExternalIntegrations/CustomCheckSucceededPublisher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
namespace ServiceControl.ExternalIntegrations | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Raven.Client; | ||
using ServiceControl.Contracts; | ||
|
||
public class CustomCheckSucceededPublisher : EventPublisher<Contracts.CustomChecks.CustomCheckSucceeded, CustomCheckSucceededPublisher.DispatchContext> | ||
{ | ||
protected override DispatchContext CreateDispatchRequest(Contracts.CustomChecks.CustomCheckSucceeded @event) | ||
{ | ||
return new DispatchContext | ||
{ | ||
EndpointHost = @event.OriginatingEndpoint.Host, | ||
EndpointHostId = @event.OriginatingEndpoint.HostId, | ||
EndpointName = @event.OriginatingEndpoint.Name, | ||
SucceededAt = @event.SucceededAt, | ||
Category = @event.Category, | ||
CustomCheckId = @event.CustomCheckId, | ||
}; | ||
} | ||
|
||
protected override IEnumerable<object> PublishEvents(IEnumerable<DispatchContext> contexts, IDocumentSession session) | ||
{ | ||
return contexts.Select(r => new CustomCheckSucceeded | ||
{ | ||
SucceededAt = r.SucceededAt, | ||
Category = r.Category, | ||
CustomCheckId = r.CustomCheckId, | ||
Host = r.EndpointHost, | ||
HostId = r.EndpointHostId, | ||
EndpointName = r.EndpointName | ||
}); | ||
} | ||
|
||
public class DispatchContext | ||
{ | ||
public string EndpointName { get; set; } | ||
public Guid EndpointHostId { get; set; } | ||
public string EndpointHost { get; set; } | ||
public string CustomCheckId { get; set; } | ||
public string Category { get; set; } | ||
public DateTime SucceededAt { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters