2019-08-13 18:30:04 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Tapeti.Config;
|
|
|
|
|
using Tapeti.Flow.FlowHelpers;
|
|
|
|
|
|
|
|
|
|
namespace Tapeti.Flow.Default
|
|
|
|
|
{
|
2019-08-15 09:26:55 +00:00
|
|
|
|
/// <inheritdoc cref="IControllerMessageMiddleware"/> />
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles methods marked with the Continuation attribute.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal class FlowContinuationMiddleware : IControllerFilterMiddleware, IControllerMessageMiddleware, IControllerCleanupMiddleware
|
2019-08-13 18:30:04 +00:00
|
|
|
|
{
|
|
|
|
|
public async Task Filter(IControllerMessageContext context, Func<Task> next)
|
|
|
|
|
{
|
2019-08-14 18:48:40 +00:00
|
|
|
|
var flowContext = await EnrichWithFlowContext(context);
|
2019-08-13 18:30:04 +00:00
|
|
|
|
if (flowContext?.ContinuationMetadata == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (flowContext.ContinuationMetadata.MethodName != MethodSerializer.Serialize(context.Binding.Method))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
await next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task Handle(IControllerMessageContext context, Func<Task> next)
|
|
|
|
|
{
|
|
|
|
|
if (context.Get(ContextItems.FlowContext, out FlowContext flowContext))
|
|
|
|
|
{
|
|
|
|
|
Newtonsoft.Json.JsonConvert.PopulateObject(flowContext.FlowState.Data, context.Controller);
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
2020-01-20 15:47:59 +00:00
|
|
|
|
if (converge)
|
|
|
|
|
// Indicate to the FlowBindingMiddleware that the state must not to be stored
|
|
|
|
|
context.Store(ContextItems.FlowIsConverging, null);
|
|
|
|
|
|
2019-08-13 18:30:04 +00:00
|
|
|
|
await next();
|
|
|
|
|
|
|
|
|
|
if (converge)
|
|
|
|
|
await CallConvergeMethod(context,
|
|
|
|
|
flowContext.ContinuationMetadata.ConvergeMethodName,
|
|
|
|
|
flowContext.ContinuationMetadata.ConvergeMethodSync);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
await next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-06-11 14:36:55 +00:00
|
|
|
|
public async Task Cleanup(IControllerMessageContext context, ConsumeResult consumeResult, Func<Task> next)
|
2019-08-13 18:30:04 +00:00
|
|
|
|
{
|
|
|
|
|
await next();
|
|
|
|
|
|
|
|
|
|
if (!context.Get(ContextItems.FlowContext, out FlowContext flowContext))
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-11 14:36:55 +00:00
|
|
|
|
if (flowContext.ContinuationMetadata.MethodName != MethodSerializer.Serialize(context.Binding.Method))
|
|
|
|
|
// Do not call when the controller method was filtered, if the same message has two methods
|
|
|
|
|
return;
|
|
|
|
|
|
2019-08-13 18:30:04 +00:00
|
|
|
|
if (flowContext?.FlowStateLock != null)
|
|
|
|
|
{
|
2020-01-20 12:06:55 +00:00
|
|
|
|
if (!flowContext.IsStoredOrDeleted())
|
|
|
|
|
// The exception strategy can set the consume result to Success. Instead, check if the yield point
|
|
|
|
|
// was handled. The flow provider ensures we only end up here in case of an exception.
|
2019-08-13 18:30:04 +00:00
|
|
|
|
await flowContext.FlowStateLock.DeleteFlowState();
|
2019-08-14 10:20:53 +00:00
|
|
|
|
|
2019-08-13 18:30:04 +00:00
|
|
|
|
flowContext.FlowStateLock.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-08-14 18:48:40 +00:00
|
|
|
|
private static async Task<FlowContext> EnrichWithFlowContext(IControllerMessageContext context)
|
2019-08-13 18:30:04 +00:00
|
|
|
|
{
|
|
|
|
|
if (context.Get(ContextItems.FlowContext, out FlowContext flowContext))
|
|
|
|
|
return flowContext;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (context.Properties.CorrelationId == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (!Guid.TryParse(context.Properties.CorrelationId, out var continuationID))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var flowStore = context.Config.DependencyResolver.Resolve<IFlowStore>();
|
|
|
|
|
|
|
|
|
|
var flowID = await flowStore.FindFlowID(continuationID);
|
|
|
|
|
if (!flowID.HasValue)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var flowStateLock = await flowStore.LockFlowState(flowID.Value);
|
|
|
|
|
|
|
|
|
|
var flowState = await flowStateLock.GetFlowState();
|
|
|
|
|
if (flowState == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
flowContext = new FlowContext
|
|
|
|
|
{
|
2019-08-15 10:04:03 +00:00
|
|
|
|
HandlerContext = new FlowHandlerContext(context),
|
2019-08-13 18:30:04 +00:00
|
|
|
|
|
|
|
|
|
FlowStateLock = flowStateLock,
|
|
|
|
|
FlowState = flowState,
|
|
|
|
|
|
|
|
|
|
ContinuationID = continuationID,
|
|
|
|
|
ContinuationMetadata = flowState.Continuations.TryGetValue(continuationID, out var continuation) ? continuation : null
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// IDisposable items in the IMessageContext are automatically disposed
|
|
|
|
|
context.Store(ContextItems.FlowContext, flowContext);
|
|
|
|
|
return flowContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static async Task CallConvergeMethod(IControllerMessageContext context, string methodName, bool sync)
|
|
|
|
|
{
|
|
|
|
|
IYieldPoint yieldPoint;
|
|
|
|
|
|
|
|
|
|
var method = context.Controller.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
|
|
|
|
|
if (method == null)
|
|
|
|
|
throw new ArgumentException($"Unknown converge method in controller {context.Controller.GetType().Name}: {methodName}");
|
|
|
|
|
|
|
|
|
|
if (sync)
|
|
|
|
|
yieldPoint = (IYieldPoint)method.Invoke(context.Controller, new object[] {});
|
|
|
|
|
else
|
|
|
|
|
yieldPoint = await (Task<IYieldPoint>)method.Invoke(context.Controller, new object[] { });
|
|
|
|
|
|
|
|
|
|
if (yieldPoint == null)
|
|
|
|
|
throw new YieldPointException($"Yield point is required in controller {context.Controller.GetType().Name} for converge method {methodName}");
|
|
|
|
|
|
|
|
|
|
var flowHandler = context.Config.DependencyResolver.Resolve<IFlowHandler>();
|
2019-08-15 10:04:03 +00:00
|
|
|
|
await flowHandler.Execute(new FlowHandlerContext(context), yieldPoint);
|
2019-08-13 18:30:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|