2017-01-31 12:01:08 +01:00
|
|
|
|
using System;
|
2021-12-10 11:45:09 +01:00
|
|
|
|
using System.Diagnostics;
|
2017-02-16 23:03:07 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
|
|
|
|
namespace Tapeti.Flow.Default
|
|
|
|
|
{
|
|
|
|
|
internal class FlowContext : IDisposable
|
|
|
|
|
{
|
2022-11-23 09:13:38 +01:00
|
|
|
|
private readonly IFlowHandlerContext? handlerContext;
|
|
|
|
|
private IFlowStateLock? flowStateLock;
|
|
|
|
|
private FlowState? flowState;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IFlowHandlerContext HandlerContext => handlerContext ?? throw new InvalidOperationException("FlowContext does not have a HandlerContext");
|
|
|
|
|
public IFlowStateLock FlowStateLock => flowStateLock ?? throw new InvalidOperationException("FlowContext does not have a FlowStateLock");
|
|
|
|
|
public FlowState FlowState => flowState ?? throw new InvalidOperationException("FlowContext does not have a FlowState");
|
|
|
|
|
|
|
|
|
|
public bool HasFlowStateAndLock => flowState != null && flowStateLock != null;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2017-02-05 23:22:34 +01:00
|
|
|
|
public Guid ContinuationID { get; set; }
|
2022-11-23 09:13:38 +01:00
|
|
|
|
public ContinuationMetadata? ContinuationMetadata { get; set; }
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2021-12-10 11:45:09 +01:00
|
|
|
|
private int storeCalled;
|
|
|
|
|
private int deleteCalled;
|
2017-02-16 23:03:07 +01:00
|
|
|
|
|
|
|
|
|
|
2022-11-23 09:13:38 +01:00
|
|
|
|
public FlowContext(IFlowHandlerContext handlerContext, FlowState flowState, IFlowStateLock flowStateLock)
|
|
|
|
|
{
|
|
|
|
|
this.flowState = flowState;
|
|
|
|
|
this.flowStateLock = flowStateLock;
|
|
|
|
|
this.handlerContext = handlerContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public FlowContext(IFlowHandlerContext handlerContext)
|
|
|
|
|
{
|
|
|
|
|
this.handlerContext = handlerContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void SetFlowState(FlowState newFlowState, IFlowStateLock newFlowStateLock)
|
|
|
|
|
{
|
|
|
|
|
flowState = newFlowState;
|
|
|
|
|
flowStateLock = newFlowStateLock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-02-09 11:26:56 +01:00
|
|
|
|
public ValueTask Store(bool persistent)
|
2017-02-16 23:03:07 +01:00
|
|
|
|
{
|
2021-12-10 11:45:09 +01:00
|
|
|
|
storeCalled++;
|
2017-02-16 23:03:07 +01:00
|
|
|
|
|
2019-08-15 12:04:03 +02:00
|
|
|
|
FlowState.Data = Newtonsoft.Json.JsonConvert.SerializeObject(HandlerContext.Controller);
|
2022-02-09 11:26:56 +01:00
|
|
|
|
return FlowStateLock.StoreFlowState(FlowState, persistent);
|
2017-10-17 10:34:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-09 11:26:56 +01:00
|
|
|
|
public ValueTask Delete()
|
2017-10-17 10:34:07 +02:00
|
|
|
|
{
|
2021-12-10 11:45:09 +01:00
|
|
|
|
deleteCalled++;
|
2022-11-23 09:13:38 +01:00
|
|
|
|
return flowStateLock?.DeleteFlowState() ?? default;
|
2017-10-17 10:34:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 13:06:55 +01:00
|
|
|
|
public bool IsStoredOrDeleted()
|
|
|
|
|
{
|
2021-12-10 11:45:09 +01:00
|
|
|
|
return storeCalled > 0 || deleteCalled > 0;
|
2020-01-20 13:06:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-17 10:34:07 +02:00
|
|
|
|
public void EnsureStoreOrDeleteIsCalled()
|
|
|
|
|
{
|
2020-01-20 13:06:55 +01:00
|
|
|
|
if (!IsStoredOrDeleted())
|
2022-11-23 09:13:38 +01:00
|
|
|
|
throw new InvalidProgramException("Neither Store nor Delete are called for the state of the current flow. FlowID = " + flowStateLock?.FlowID);
|
2021-12-10 11:45:09 +01:00
|
|
|
|
|
|
|
|
|
Debug.Assert(storeCalled <= 1, "Store called more than once!");
|
|
|
|
|
Debug.Assert(deleteCalled <= 1, "Delete called more than once!");
|
2017-02-16 23:03:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-31 12:01:08 +01:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2022-11-23 09:13:38 +01:00
|
|
|
|
flowStateLock?.Dispose();
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|