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

Send retry confirmation to ServiceControl #6094

Merged
merged 21 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace NServiceBus
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Pipeline;
using Routing;
using Transport;

class ManualRetryNotificationBehavior : IForkConnector<IIncomingLogicalMessageContext, IIncomingLogicalMessageContext, IRoutingContext>
{
const string RetryUniqueMessageIdHeader = "ServiceControl.Retry.UniqueMessageId";

readonly string errorQueue;

public ManualRetryNotificationBehavior(string errorQueue)
{
this.errorQueue = errorQueue;
}

public async Task Invoke(IIncomingLogicalMessageContext context, Func<IIncomingLogicalMessageContext, Task> next)
{
await next(context).ConfigureAwait(false);

if (IsRetriedMessage(out var id))
{
await ConfirmSuccessfulRetry().ConfigureAwait(false);
}

async Task ConfirmSuccessfulRetry()
{
var messageToDispatch = new OutgoingMessage(
CombGuid.Generate().ToString(),
new Dictionary<string, string>
{
{ "ServiceControl.Retry.Successful", DateTimeOffset.UtcNow.ToString("O") },
{ RetryUniqueMessageIdHeader, id }
},
new byte[0]);
var routingContext = new RoutingContext(messageToDispatch, new UnicastRoutingStrategy(errorQueue), context);
await this.Fork(routingContext).ConfigureAwait(false);
}

bool IsRetriedMessage(out string retryUniqueMessageId)
{
// check if the message is coming from a manual retry attempt
if (context.Headers.TryGetValue(RetryUniqueMessageIdHeader, out var uniqueMessageId) &&
// The SC version that supports the confirmation message also started to add the SC version header
context.Headers.ContainsKey("ServiceControl.Version"))
{
retryUniqueMessageId = uniqueMessageId;
return true;
}

retryUniqueMessageId = null;
return false;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace NServiceBus
{
using Features;

/// <summary>
/// Provides notifications to ServiceControl about successfully retried messages
/// </summary>
public class PlatformRetryNotifications : Feature
{
PlatformRetryNotifications() => EnableByDefault();

/// <inheritdoc />
protected internal override void Setup(FeatureConfigurationContext context)
{
var errorQueueAddress = context.Settings.ErrorQueueAddress();
var forkBehavior = new ManualRetryNotificationBehavior(errorQueueAddress);
context.Pipeline.Register(forkBehavior, "Provides retry notifications to ServiceControl");
}
}
}