Skip to content

Commit

Permalink
Merge pull request #444 from Particular/Expose_CustomChecks_Events
Browse files Browse the repository at this point in the history
Added CustomCheckSucceeded and CustomCheckFailed event publishing for external integration
  • Loading branch information
Greg Bielleman committed Feb 5, 2015
2 parents 489f492 + e1f8ab7 commit a247976
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 9 deletions.
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; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
</Reference>
<Reference Include="ServiceControl.Contracts, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\ServiceControl.Contracts.1.0.0\lib\net45\ServiceControl.Contracts.dll</HintPath>
<HintPath>..\packages\ServiceControl.Contracts.1.1.0\lib\net45\ServiceControl.Contracts.dll</HintPath>
</Reference>
<Reference Include="ServiceControl.Plugin.Nsb4.CustomChecks, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down Expand Up @@ -180,7 +180,8 @@
<Compile Include="CustomChecks\When_a_custom_check_fails.cs" />
<Compile Include="CustomChecks\When_a_periodic_custom_check_fails.cs" />
<Compile Include="DebugSessions\When_an_endpoint_is_running_with_debug_sessions_enabled.cs" />
<Compile Include="ExternalIntegrations\JSonServer.cs" />
<Compile Include="ExternalIntegrations\JsonServer.cs" />
<Compile Include="ExternalIntegrations\When_a_message_has_custom_checks.cs" />
<Compile Include="ExternalIntegrations\When_a_message_has_failed.cs" />
<Compile Include="ExternalIntegrations\When_heartbeat_is_restored.cs" />
<Compile Include="ExternalIntegrations\When_encountered_an_error.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceControl.AcceptanceTests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<package id="NUnit" version="2.6.2" targetFramework="net45" />
<package id="RabbitMQ.Client" version="3.2.1" targetFramework="net45" />
<package id="RavenDB.Client" version="2.5.2916" targetFramework="net45" />
<package id="ServiceControl.Contracts" version="1.0.0" targetFramework="net45" />
<package id="ServiceControl.Contracts" version="1.1.0" targetFramework="net45" />
<package id="ServiceControl.Plugin.Nsb4.CustomChecks" version="1.0.0" targetFramework="net45" />
<package id="ServiceControl.Plugin.Nsb4.DebugSession" version="1.0.0" targetFramework="net45" />
<package id="ServiceControl.Plugin.Nsb4.Heartbeat" version="1.0.0" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</Reference>
<Reference Include="ServiceControl.Contracts, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\ServiceControl.Contracts.1.0.0\lib\net45\ServiceControl.Contracts.dll</HintPath>
<HintPath>..\packages\ServiceControl.Contracts.1.1.0\lib\net45\ServiceControl.Contracts.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceControl.IntegrationDemo/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<package id="NServiceBus" version="4.6.5" targetFramework="net45" />
<package id="NServiceBus.Host" version="4.6.5" targetFramework="net45" />
<package id="NServiceBus.Interfaces" version="4.6.5" targetFramework="net45" />
<package id="ServiceControl.Contracts" version="1.0.0" targetFramework="net45" />
<package id="ServiceControl.Contracts" version="1.1.0" targetFramework="net45" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
</Reference>
<Reference Include="ServiceControl.Contracts, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\ServiceControl.Contracts.1.0.0\lib\net45\ServiceControl.Contracts.dll</HintPath>
<HintPath>..\packages\ServiceControl.Contracts.1.1.0\lib\net45\ServiceControl.Contracts.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceControl.UnitTests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<package id="RavenDB.Client" version="2.5.2916" targetFramework="net45" />
<package id="RavenDB.Database" version="2.5.2916" targetFramework="net45" />
<package id="RavenDB.Embedded" version="2.5.2916" targetFramework="net45" />
<package id="ServiceControl.Contracts" version="1.0.0" targetFramework="net45" />
<package id="ServiceControl.Contracts" version="1.1.0" targetFramework="net45" />
<package id="System.Spatial" version="5.6.2" targetFramework="net45" />
<package id="WindowsAzure.Storage" version="3.1.0.1" targetFramework="net45" />
</packages>
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; }
}
}
}
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; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public void Init()
Configure.Component<MessageFailedPublisher>(DependencyLifecycle.SingleInstance);
Configure.Component<HeartbeatStoppedPublisher>(DependencyLifecycle.SingleInstance);
Configure.Component<HeartbeatRestoredPublisher>(DependencyLifecycle.SingleInstance);
Configure.Component<CustomCheckFailedPublisher>(DependencyLifecycle.SingleInstance);
Configure.Component<CustomCheckSucceededPublisher>(DependencyLifecycle.SingleInstance);
}
}
}
4 changes: 3 additions & 1 deletion src/ServiceControl/ServiceControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
</Reference>
<Reference Include="ServiceControl.Contracts, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\ServiceControl.Contracts.1.0.0\lib\net45\ServiceControl.Contracts.dll</HintPath>
<HintPath>..\packages\ServiceControl.Contracts.1.1.0\lib\net45\ServiceControl.Contracts.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
Expand Down Expand Up @@ -250,11 +250,13 @@
<Compile Include="CustomChecks\CustomCheckNotifications.cs" />
<Compile Include="EventLog\Definitions\MessageSubmittedForRetryDefinition.cs" />
<Compile Include="CustomChecks\CustomChecksIndex.cs" />
<Compile Include="ExternalIntegrations\CustomCheckSucceededPublisher.cs" />
<Compile Include="ExternalIntegrations\EventPublisher.cs" />
<Compile Include="ExternalIntegrations\ExternalIntegrationDispatchRequest.cs" />
<Compile Include="ExternalIntegrations\EventDispatcher.cs" />
<Compile Include="ExternalIntegrations\EventMappingHandler.cs" />
<Compile Include="ExternalIntegrations\ExternalIntegrationsInitializer.cs" />
<Compile Include="ExternalIntegrations\CustomCheckFailedPublisher.cs" />
<Compile Include="ExternalIntegrations\HeartbeatRestoredPublisher.cs" />
<Compile Include="ExternalIntegrations\HeartbeatStoppedPublisher.cs" />
<Compile Include="ExternalIntegrations\IEventPublisher.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceControl/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<package id="Rx-Linq" version="2.2.5" targetFramework="net45" />
<package id="Rx-Main" version="2.2.5" targetFramework="net45" />
<package id="Rx-PlatformServices" version="2.2.5" targetFramework="net45" />
<package id="ServiceControl.Contracts" version="1.0.0" targetFramework="net45" />
<package id="ServiceControl.Contracts" version="1.1.0" targetFramework="net45" />
<package id="SharpZipLib" version="0.86.0" targetFramework="net45" />
<package id="System.Spatial" version="5.6.3" targetFramework="net45" />
<package id="WindowsAzure.Storage" version="2.1.0.4" targetFramework="net45" />
Expand Down

0 comments on commit a247976

Please sign in to comment.