41 lines
1.7 KiB
C#
41 lines
1.7 KiB
C#
using System.Collections.Concurrent;
|
|
using MAF1.Decisions;
|
|
using MAF1.Plugins;
|
|
|
|
namespace MAF1.Web;
|
|
|
|
/// <summary>
|
|
/// 一次可暂停的运行。NextIndex 记住停在第几个节点;Pending 是当前确认请求。
|
|
/// Mutex 防止「用户点击」和「超时自动确认」同时 Continue。
|
|
/// </summary>
|
|
internal sealed class WorkflowSession
|
|
{
|
|
public required string RunId { get; init; }
|
|
public required WorkflowGraph Graph { get; init; }
|
|
public required NodeCatalogSnapshot Catalog { get; init; }
|
|
public required List<string> Order { get; init; }
|
|
public required Dictionary<string, Dictionary<string, object?>> Outputs { get; init; }
|
|
public required List<NodeRunLog> Steps { get; init; }
|
|
public int NextIndex { get; set; }
|
|
public DecisionRequest? Pending { get; set; }
|
|
public bool Completed { get; set; }
|
|
public WorkflowRunResult LastResult { get; set; } = new();
|
|
public DecisionAnswer? AppliedDecision { get; set; }
|
|
public CancellationTokenSource TimeoutCts { get; set; } = new();
|
|
public SemaphoreSlim Mutex { get; } = new(1, 1);
|
|
public object Sync { get; } = new();
|
|
}
|
|
|
|
/// <summary>进程内运行字典。服务重启后 runId 会失效,学习项目不做持久化。</summary>
|
|
public sealed class WorkflowRunStore
|
|
{
|
|
private readonly ConcurrentDictionary<string, WorkflowSession> _sessions = new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
internal bool TryAdd(WorkflowSession session) => _sessions.TryAdd(session.RunId, session);
|
|
|
|
internal bool TryGet(string runId, out WorkflowSession? session) => _sessions.TryGetValue(runId, out session);
|
|
|
|
public WorkflowRunResult? GetResult(string runId)
|
|
=> _sessions.TryGetValue(runId, out WorkflowSession? session) ? session.LastResult : null;
|
|
}
|