-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ServiceBusSessionMessageActions management APIs: worker changes (#2404)
* initial changes * removing extra code * more changes * addressing comments * adding unit tests * updated package * adding proj ref * fixing unit test * addressing comments * changes * changing error message
- Loading branch information
1 parent
dd8398e
commit be0b8b3
Showing
8 changed files
with
351 additions
and
4 deletions.
There are no files selected for viewing
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
98 changes: 98 additions & 0 deletions
98
extensions/Worker.Extensions.ServiceBus/src/ServiceBusSessionMessageActions.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,98 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Messaging.ServiceBus; | ||
using Google.Protobuf; | ||
using Microsoft.Azure.Functions.Worker.Converters; | ||
using Microsoft.Azure.ServiceBus.Grpc; | ||
|
||
namespace Microsoft.Azure.Functions.Worker | ||
{ | ||
/// <summary> | ||
/// Converter to bind to <see cref="ServiceBusSessionMessageActions" /> type parameters. | ||
/// </summary> | ||
[InputConverter(typeof(ServiceBusSessionMessageActionsConverter))] | ||
public class ServiceBusSessionMessageActions | ||
{ | ||
private readonly Settlement.SettlementClient _settlement; | ||
private readonly string _sessionId; | ||
|
||
internal ServiceBusSessionMessageActions(Settlement.SettlementClient settlement, string sessionId, DateTimeOffset sessionLockedUntil) | ||
{ | ||
_settlement = settlement ?? throw new ArgumentNullException(nameof(settlement)); | ||
_sessionId = sessionId ?? throw new ArgumentNullException(nameof(sessionId)); | ||
SessionLockedUntil = sessionLockedUntil; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ServiceBusMessageActions"/> class for mocking use in testing. | ||
/// </summary> | ||
/// <remarks> | ||
/// This constructor exists only to support mocking. When used, class state is not fully initialized, and | ||
/// will not function correctly; virtual members are meant to be mocked. | ||
///</remarks> | ||
protected ServiceBusSessionMessageActions() | ||
{ | ||
_settlement = null!; // not expected to be used during mocking. | ||
_sessionId = null!; // not expected to be used during mocking. | ||
} | ||
|
||
public virtual DateTimeOffset SessionLockedUntil { get; protected set; } | ||
|
||
///<inheritdoc cref="ServiceBusReceiver.CompleteMessageAsync(ServiceBusReceivedMessage, CancellationToken)"/> | ||
public virtual async Task<BinaryData> GetSessionStateAsync( | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var request = new GetSessionStateRequest() | ||
{ | ||
SessionId = _sessionId, | ||
}; | ||
|
||
GetSessionStateResponse result = await _settlement.GetSessionStateAsync(request, cancellationToken: cancellationToken); | ||
BinaryData binaryData = new BinaryData(result.SessionState.Memory); | ||
return binaryData; | ||
} | ||
|
||
///<inheritdoc cref="ServiceBusReceiver.CompleteMessageAsync(ServiceBusReceivedMessage, CancellationToken)"/> | ||
public virtual async Task SetSessionStateAsync( | ||
BinaryData sessionState, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var request = new SetSessionStateRequest() | ||
{ | ||
SessionId = _sessionId, | ||
SessionState = ByteString.CopyFrom(sessionState.ToMemory().Span), | ||
}; | ||
|
||
await _settlement.SetSessionStateAsync(request, cancellationToken: cancellationToken); | ||
} | ||
|
||
///<inheritdoc cref="ServiceBusReceiver.CompleteMessageAsync(ServiceBusReceivedMessage, CancellationToken)"/> | ||
public virtual async Task ReleaseSession( | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var request = new ReleaseSessionRequest() | ||
{ | ||
SessionId = _sessionId, | ||
}; | ||
|
||
await _settlement.ReleaseSessionAsync(request, cancellationToken: cancellationToken); | ||
} | ||
|
||
///<inheritdoc cref="ServiceBusReceiver.CompleteMessageAsync(ServiceBusReceivedMessage, CancellationToken)"/> | ||
public virtual async Task RenewSessionLockAsync( | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var request = new RenewSessionLockRequest() | ||
{ | ||
SessionId = _sessionId, | ||
}; | ||
|
||
var result = await _settlement.RenewSessionLockAsync(request, cancellationToken: cancellationToken); | ||
SessionLockedUntil = result.LockedUntil.ToDateTimeOffset(); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
extensions/Worker.Extensions.ServiceBus/src/ServiceBusSessionMessageActionsConverter.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,62 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Functions.Worker.Converters; | ||
using Microsoft.Azure.Functions.Worker.Extensions.Abstractions; | ||
using Microsoft.Azure.ServiceBus.Grpc; | ||
using System.Text.Json; | ||
|
||
namespace Microsoft.Azure.Functions.Worker | ||
{ | ||
/// <summary> | ||
/// Converter to bind to <see cref="ServiceBusSessionMessageActions" /> or <see cref="ServiceBusSessionMessageActions{}" /> type parameters. | ||
/// </summary> | ||
[SupportsDeferredBinding] | ||
[SupportedTargetType(typeof(ServiceBusSessionMessageActions))] | ||
[SupportedTargetType(typeof(ServiceBusSessionMessageActions[]))] | ||
internal class ServiceBusSessionMessageActionsConverter : IInputConverter | ||
{ | ||
private readonly Settlement.SettlementClient _settlement; | ||
|
||
public ServiceBusSessionMessageActionsConverter(Settlement.SettlementClient settlement) | ||
{ | ||
_settlement = settlement; | ||
} | ||
|
||
public ValueTask<ConversionResult> ConvertAsync(ConverterContext context) | ||
{ | ||
try | ||
{ | ||
var foundSessionId = context.FunctionContext.BindingContext.BindingData.TryGetValue("SessionId", out object? sessionId); | ||
if (!foundSessionId) | ||
{ | ||
throw new InvalidOperationException($"Expecting SessionId within binding data and value was not present. Sessions must be enabled when binding to {nameof(ServiceBusSessionMessageActions)}."); | ||
} | ||
|
||
// Get the sessionLockedUntil property from the SessionActions binding data | ||
var foundSessionActions = context.FunctionContext.BindingContext.BindingData.TryGetValue("SessionActions", out object? sessionActions); | ||
if (!foundSessionActions) | ||
{ | ||
throw new InvalidOperationException("Expecting SessionActions within binding data and value was not present."); | ||
} | ||
|
||
JsonDocument jsonDocument = JsonDocument.Parse(sessionActions!.ToString()); | ||
var foundSessionLockedUntil = jsonDocument.RootElement.TryGetProperty("SessionLockedUntil", out JsonElement sessionLockedUntil); | ||
if (!foundSessionLockedUntil) | ||
{ | ||
throw new InvalidOperationException("Expecting SessionLockedUntil within binding data of session actions and value was not present."); | ||
} | ||
|
||
var sessionActionResult = new ServiceBusSessionMessageActions(_settlement, sessionId!.ToString(), sessionLockedUntil.GetDateTimeOffset()); | ||
var result = ConversionResult.Success(sessionActionResult); | ||
return new ValueTask<ConversionResult>(result); | ||
} | ||
catch (Exception exception) | ||
{ | ||
return new ValueTask<ConversionResult>(ConversionResult.Failed(exception)); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.