MES-ETL/MesETL.App/Services/ProcessContext.cs

70 lines
2.2 KiB
C#
Raw Normal View History

2024-01-29 09:29:16 +08:00
using System.Collections.Concurrent;
namespace MesETL.App.Services;
2023-12-29 16:16:05 +08:00
2024-01-04 09:00:44 +08:00
/// <summary>
/// 处理上下文类,标识处理进度
/// </summary>
2023-12-29 16:16:05 +08:00
public class ProcessContext
{
2024-01-29 09:29:16 +08:00
private bool _hasException;
private long _inputCount;
private long _transformCount;
private long _outputCount;
private readonly ConcurrentDictionary<string, long> _tableProgress = new();
public bool HasException => _hasException;
2023-12-29 16:16:05 +08:00
public bool IsInputCompleted { get; private set; }
public bool IsTransformCompleted { get; private set; }
public bool IsOutputCompleted { get; private set; }
2024-01-29 09:29:16 +08:00
public long InputCount
2023-12-29 16:16:05 +08:00
{
get => _inputCount;
2024-01-29 09:29:16 +08:00
set => Interlocked.Exchange(ref _inputCount, value);
2023-12-29 16:16:05 +08:00
}
2024-01-29 09:29:16 +08:00
public long TransformCount
2023-12-29 16:16:05 +08:00
{
get => _transformCount;
2024-01-29 09:29:16 +08:00
set => Interlocked.Exchange(ref _transformCount, value);
2023-12-29 16:16:05 +08:00
}
2024-01-29 09:29:16 +08:00
public long OutputCount
2023-12-29 16:16:05 +08:00
{
get => _outputCount;
2024-01-29 09:29:16 +08:00
set => Interlocked.Exchange(ref _outputCount, value);
2023-12-29 16:16:05 +08:00
}
2024-01-29 09:29:16 +08:00
// TableName -> Count
public IReadOnlyDictionary<string, long> TableProgress => _tableProgress;
2023-12-29 16:16:05 +08:00
public void CompleteInput() => IsInputCompleted = true;
public void CompleteTransform() => IsTransformCompleted = true;
public void CompleteOutput() => IsOutputCompleted = true;
2024-01-29 09:29:16 +08:00
public bool AddException(Exception e) => _hasException = true;
2023-12-29 16:16:05 +08:00
public void AddInput() => Interlocked.Increment(ref _inputCount);
public void AddInput(int count) => Interlocked.Add(ref _inputCount, count);
public void AddTransform() => Interlocked.Increment(ref _transformCount);
public void AddTransform(int count) => Interlocked.Add(ref _transformCount, count);
public void AddOutput() => Interlocked.Increment(ref _outputCount);
public void AddOutput(int count) => Interlocked.Add(ref _outputCount, count);
2024-01-29 09:29:16 +08:00
public void AddTableOutput(string table, int count)
{
_tableProgress.AddOrUpdate(table, count, (k, v) => v + count);
AddOutput(count);
}
public long GetTableOutput(string table)
{
if(!_tableProgress.TryGetValue(table, out var count))
throw new ApplicationException($"未找到表{table}输出记录");
return count;
}
2023-12-29 16:16:05 +08:00
}