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