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

Add the ability to cancel scheduled messages #746

Merged
merged 21 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion src/Common/Helpers/ServiceBusHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5337,7 +5337,7 @@ public string GetAddressRelativeToNamespace(string address)

public ServiceBusHelper2 GetServiceBusHelper2()
{
var serviceBusHelper2 = new ServiceBusHelper2();
var serviceBusHelper2 = new ServiceBusHelper2(writeToLog);
serviceBusHelper2.ConnectionString = ConnectionString;
serviceBusHelper2.TransportType = UseAmqpWebSockets
? Azure.Messaging.ServiceBus.ServiceBusTransportType.AmqpWebSockets
Expand Down
85 changes: 85 additions & 0 deletions src/ServiceBus/Helpers/CancelScheduledMessagesHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

using Azure.Messaging.ServiceBus;

using ServiceBusExplorer.Utilities.Helpers;

namespace ServiceBusExplorer.ServiceBus.Helpers
{
public static class CancelScheduledMessagesHelper
{
class OperationStatus
{
public int Successes;
public int Failures;
}

public static async Task CancelScheduledMessages(ServiceBusHelper2 serviceBusHelper,
string queueName, List<long> sequenceNumbersToCancel)
{
var client = serviceBusHelper.CreateServiceBusClient();

try
{
var sender = client.CreateSender(queueName);

serviceBusHelper.WriteToLog($"Starting cancellation of scheduled messages on queue {queueName}.");

var operationStatus = new OperationStatus();

var stopwatch = Stopwatch.StartNew();
var semaphore = new SemaphoreSlim(40); // As recommended by https://learn.microsoft.com/en-us/azure/service-bus-messaging/message-transfers-locks-settlement#settling-send-operations
var tasks = new List<Task>(sequenceNumbersToCancel.Count);

foreach (long sequenceNumber in sequenceNumbersToCancel)
{
await semaphore.WaitAsync();
tasks.Add(CancelScheduledMessageWithLog(sender, sequenceNumber, serviceBusHelper.WriteToLog, operationStatus)
.ContinueWith((t, state) => ((SemaphoreSlim)state)?.Release(), semaphore));
}

await Task.WhenAll(tasks);
stopwatch.Stop();

Func<int, string> singleOrPlural = (c) => c > 1 ? "messages" : "message";

serviceBusHelper.WriteToLog($"Successfully cancelled {operationStatus.Successes} " +
$"scheduled {singleOrPlural(operationStatus.Successes)} in {stopwatch.Elapsed}.");

if (operationStatus.Failures > 0)
{
serviceBusHelper.WriteToLog($"Failed to cancel {operationStatus.Failures} " +
$"{singleOrPlural(operationStatus.Failures)}.");
}
}
finally
{
await client.DisposeAsync();
}
}

static Task CancelScheduledMessageWithLog(ServiceBusSender sender,
long sequenceNumber, WriteToLogDelegate writeToLog, OperationStatus operationStatus)
{
Task task = sender.CancelScheduledMessageAsync(sequenceNumber)
.ContinueWith(_ =>
{
writeToLog($"Cancelled scheduled message with sequence number {sequenceNumber}.");
Interlocked.Increment(ref operationStatus.Successes);
},
TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously)
.ContinueWith(_ =>
{
writeToLog($"Failed to cancel scheduled message with sequence number {sequenceNumber}.");
Interlocked.Increment(ref operationStatus.Failures);
},
TaskContinuationOptions.NotOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously);

return task;
}
}
}
31 changes: 30 additions & 1 deletion src/ServiceBus/Helpers/ServiceBusHelper2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,40 @@
#endregion

using System.Threading.Tasks;

using Azure.Messaging.ServiceBus;
using Azure.Messaging.ServiceBus.Administration;

using ServiceBusExplorer.Utilities.Helpers;

// ReSharper disable CheckNamespace
namespace ServiceBusExplorer.ServiceBus.Helpers
// ReSharper restore CheckNamespace
{
public class ServiceBusHelper2
{
readonly WriteToLogDelegate writeToLog;

public string ConnectionString { get; set; }
public ServiceBusTransportType TransportType { get; set; }

public WriteToLogDelegate WriteToLog
{
get
{
return writeToLog;
}
}

public ServiceBusHelper2(WriteToLogDelegate writeToLog)
{
this.writeToLog = writeToLog;
}

public bool ConnectionStringContainsEntityPath()
{
var connectionStringProperties = ServiceBusConnectionStringProperties.Parse(ConnectionString);

if (connectionStringProperties?.EntityPath != null)
{
return true;
Expand All @@ -44,6 +62,17 @@ public bool ConnectionStringContainsEntityPath()
return false;
}

/// <summary>
/// Dispose of the returned ServiceBusClient object by calling DisposeAsync().
/// </summary>
/// <returns>An Azure.Messaging.ServiceBus.ServiceBusClient</returns>
public ServiceBusClient CreateServiceBusClient()
{
return new ServiceBusClient(
ConnectionString,
new ServiceBusClientOptions { TransportType = this.TransportType });
}

public async Task<bool> IsPremiumNamespace()
{
var administrationClient = new ServiceBusAdministrationClient(ConnectionString);
Expand Down
35 changes: 34 additions & 1 deletion src/ServiceBusExplorer/Controls/HandleQueueControl.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading