1
0
mirror of synced 2024-11-24 19:53:10 +01:00

Implemented adding requests to parallel flow

Fixed console logger outputting incorrect message when declaring queues
This commit is contained in:
Mark van Renswoude 2021-12-10 09:56:37 +01:00
parent 2b4dd8e251
commit bc00d476bd
7 changed files with 60 additions and 64 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using ExampleLib;
using Messaging.TapetiExample;
using Tapeti.Annotations;
@ -16,6 +17,7 @@ namespace _03_FlowRequestResponse
public string FirstQuote;
public string SecondQuote;
public string ThirdQuote;
public ParallelFlowController(IFlowProvider flowProvider, IExampleState exampleState)
@ -35,7 +37,7 @@ namespace _03_FlowRequestResponse
Amount = 1
},
HandleFirstQuoteResponse)
.AddRequestSync<QuoteRequestMessage, QuoteResponseMessage>(
.AddRequest<QuoteRequestMessage, QuoteResponseMessage>(
new QuoteRequestMessage
{
Amount = 2
@ -54,10 +56,26 @@ namespace _03_FlowRequestResponse
[Continuation]
public void HandleSecondQuoteResponse(QuoteResponseMessage message)
public async Task HandleSecondQuoteResponse(QuoteResponseMessage message, IFlowParallelRequest parallelRequest)
{
Console.WriteLine("[ParallelFlowController] Second quote response received");
SecondQuote = message.Quote;
// Example of adding a request to an ongoing parallel request
await parallelRequest.AddRequestSync<QuoteRequestMessage, QuoteResponseMessage>(
new QuoteRequestMessage
{
Amount = 3
},
HandleThirdQuoteResponse);
}
[Continuation]
public void HandleThirdQuoteResponse(QuoteResponseMessage message)
{
Console.WriteLine("[ParallelFlowController] First quote response received");
ThirdQuote = message.Quote;
}
@ -65,6 +83,7 @@ namespace _03_FlowRequestResponse
{
Console.WriteLine("[ParallelFlowController] First quote: " + FirstQuote);
Console.WriteLine("[ParallelFlowController] Second quote: " + SecondQuote);
Console.WriteLine("[ParallelFlowController] Third quote: " + ThirdQuote);
exampleState.Done();
return flowProvider.End();

View File

@ -25,8 +25,7 @@ namespace _03_FlowRequestResponse
break;
default:
// We have to return a response.
quote = null;
quote = new string('\'', message.Amount);
break;
}

View File

@ -40,16 +40,10 @@ namespace Tapeti.Flow.Default
// Remove Continuation now because the IYieldPoint result handler will store the new state
flowContext.FlowState.Continuations.Remove(flowContext.ContinuationID);
var converge = flowContext.FlowState.Continuations.Count == 0 &&
flowContext.ContinuationMetadata.ConvergeMethodName != null;
if (converge)
// Indicate to the FlowBindingMiddleware that the state must not to be stored
flowPayload.FlowIsConverging = true;
await next();
if (converge)
if (flowPayload.FlowIsConverging)
await CallConvergeMethod(context, controllerPayload,
flowContext.ContinuationMetadata.ConvergeMethodName,
flowContext.ContinuationMetadata.ConvergeMethodSync);

View File

@ -249,19 +249,19 @@ namespace Tapeti.Flow.Default
if (!context.MessageContext.TryGet<FlowMessageContextPayload>(out var flowPayload))
return null;
return new ParallelRequest(config, SendRequest, flowPayload.FlowContext);
}
private class ParallelRequestBuilder : IFlowParallelRequestBuilder
{
public delegate Task SendRequestFunc(FlowContext context,
private delegate Task SendRequestFunc(FlowContext context,
object message,
ResponseHandlerInfo responseHandlerInfo,
string convergeMethodName,
bool convergeMethodSync);
private class ParallelRequestBuilder : IFlowParallelRequestBuilder
{
private class RequestInfo
{
public object Message { get; set; }
@ -283,41 +283,23 @@ namespace Tapeti.Flow.Default
public IFlowParallelRequestBuilder AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, Task> responseHandler)
{
requests.Add(new RequestInfo
{
Message = message,
ResponseHandlerInfo = GetResponseHandlerInfo(config, message, responseHandler)
});
return this;
return InternalAddRequest(message, responseHandler);
}
public IFlowParallelRequestBuilder AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, IFlowParallelRequest, Task> responseHandler)
{
requests.Add(new RequestInfo
{
Message = message,
ResponseHandlerInfo = GetResponseHandlerInfo(config, message, responseHandler)
});
return this;
return InternalAddRequest(message, responseHandler);
}
public IFlowParallelRequestBuilder AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse> responseHandler)
{
requests.Add(new RequestInfo
{
Message = message,
ResponseHandlerInfo = GetResponseHandlerInfo(config, message, responseHandler)
});
return this;
return InternalAddRequest(message, responseHandler);
}
public IFlowParallelRequestBuilder AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse, IFlowParallelRequest> responseHandler)
public IFlowParallelRequestBuilder InternalAddRequest(object message, Delegate responseHandler)
{
requests.Add(new RequestInfo
{
@ -368,32 +350,39 @@ namespace Tapeti.Flow.Default
{
private readonly ITapetiConfig config;
private readonly SendRequestFunc sendRequest;
private readonly FlowContext flowContext;
public ParallelRequestBuilder(ITapetiConfig config, SendRequestFunc sendRequest)
public ParallelRequest(ITapetiConfig config, SendRequestFunc sendRequest, FlowContext flowContext)
{
this.config = config;
this.sendRequest = sendRequest;
this.flowContext = flowContext;
}
public IFlowParallelRequest AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, Task> responseHandler)
public Task AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, Task> responseHandler)
{
throw new NotImplementedException();
return InternalAddRequest(message, responseHandler);
}
public IFlowParallelRequest AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, IFlowParallelRequest, Task> responseHandler)
public Task AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, IFlowParallelRequest, Task> responseHandler)
{
throw new NotImplementedException();
return InternalAddRequest(message, responseHandler);
}
public IFlowParallelRequest AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse> responseHandler)
public Task AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse> responseHandler)
{
throw new NotImplementedException();
return InternalAddRequest(message, responseHandler);
}
public IFlowParallelRequest AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse, IFlowParallelRequest> responseHandler)
private Task InternalAddRequest(object message, Delegate responseHandler)
{
throw new NotImplementedException();
var responseHandlerInfo = GetResponseHandlerInfo(config, message, responseHandler);
return sendRequest(flowContext, message, responseHandlerInfo, flowContext.ContinuationMetadata.ConvergeMethodName, flowContext.ContinuationMetadata.ConvergeMethodSync);
}
}

View File

@ -16,7 +16,9 @@ namespace Tapeti.Flow
/// parallel flow is done and the convergeMethod will be called.
/// Temporarily disables storing the flow state.
/// </summary>
public bool FlowIsConverging { get; set; }
public bool FlowIsConverging => FlowContext != null &&
FlowContext.FlowState.Continuations.Count == 0 &&
FlowContext.ContinuationMetadata.ConvergeMethodName != null;
public FlowMessageContextPayload(FlowContext flowContext)

View File

@ -192,14 +192,14 @@ namespace Tapeti.Flow
/// </summary>
/// <param name="message"></param>
/// <param name="responseHandler"></param>
Task<IFlowParallelRequest> AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, Task> responseHandler);
Task AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, Task> responseHandler);
/// <remarks>
/// This overload allows the response handler access to the IFlowParallelRequest interface, which
/// can be used to add additional requests to the parallel request before the continuation method passed to the Yield method is called.
/// </remarks>
/// <inheritdoc cref="AddRequest{TRequest,TResponse}(TRequest,Func{TResponse,Task})"/>
Task<IFlowParallelRequest> AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, IFlowParallelRequest, Task> responseHandler);
Task AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, IFlowParallelRequest, Task> responseHandler);
/// <summary>
/// Publish a request message and continue the flow when the response arrives.
@ -209,14 +209,7 @@ namespace Tapeti.Flow
/// </summary>
/// <param name="message"></param>
/// <param name="responseHandler"></param>
Task<IFlowParallelRequest> AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse> responseHandler);
/// <remarks>
/// This overload allows the response handler access to the IFlowParallelRequest interface, which
/// can be used to add additional requests to the parallel request before the continuation method passed to the Yield method is called.
/// </remarks>
/// <inheritdoc cref="AddRequestSync{TRequest,TResponse}(TRequest,Action{TResponse})"/>
Task<IFlowParallelRequest> AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse, IFlowParallelRequest> responseHandler);
Task AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse> responseHandler);
}

View File

@ -76,8 +76,8 @@ namespace Tapeti.Default
public void QueueDeclare(string queueName, bool durable, bool passive)
{
Console.WriteLine(passive
? $"[Tapeti] Declaring {(durable ? "durable" : "dynamic")} queue {queueName}"
: $"[Tapeti] Verifying durable queue {queueName}");
? $"[Tapeti] Verifying durable queue {queueName}"
: $"[Tapeti] Declaring {(durable ? "durable" : "dynamic")} queue {queueName}");
}
/// <inheritdoc />