This commit is contained in:
@@ -1577,23 +1577,54 @@
|
||||
<param name="inQuery"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="T:IRaCIS.Core.Application.Service.FileSyncQueue">
|
||||
<member name="M:IRaCIS.Core.Application.Service.FileUploadRecordService.BatchAddSyncFileTask(IRaCIS.Core.Application.ViewModel.BatchAddSyncFileCommand)">
|
||||
<summary>
|
||||
同步队列 信号量
|
||||
批量设置为需要同步,并且设置优先级
|
||||
</summary>
|
||||
<param name="inComand"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="F:IRaCIS.Core.Application.Service.FileSyncQueue._queue">
|
||||
<summary>
|
||||
优先级队列(仅负责排序)
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:IRaCIS.Core.Application.Service.FileSyncQueue._waiting">
|
||||
<summary>
|
||||
当前等待中的任务(唯一真实数据)
|
||||
key = Guid
|
||||
value = 最新 priority
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:IRaCIS.Core.Application.Service.FileSyncQueue._running">
|
||||
<summary>
|
||||
正在执行的任务(防止重复执行)
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:IRaCIS.Core.Application.Service.FileSyncQueue._signal">
|
||||
<summary>
|
||||
worker 等待信号
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:IRaCIS.Core.Application.Service.FileSyncQueue.Enqueue(System.Guid,System.Int32)">
|
||||
<summary>
|
||||
入队(同 Guid 会覆盖优先级)
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:IRaCIS.Core.Application.Service.FileSyncQueue.DequeueAsync(System.Threading.CancellationToken)">
|
||||
<summary>
|
||||
如果没有任务 → 挂起等待 有任务 → 被唤醒并返回
|
||||
获取一个待执行任务(无任务时自动等待)
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:IRaCIS.Core.Application.Service.FileSyncQueue.Complete(System.Guid)">
|
||||
<summary>
|
||||
任务执行完成(必须调用)
|
||||
</summary>
|
||||
<param name="ct"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:IRaCIS.Core.Application.Service.FileSyncQueue.Snapshot">
|
||||
<summary>
|
||||
获取队列任务Id
|
||||
当前等待中的任务快照
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:IRaCIS.Core.Application.Service.SyncQueueUseChannel.Enqueue(System.Guid,System.Int32)">
|
||||
<summary>
|
||||
|
||||
@@ -228,4 +228,6 @@ public class UploadFileSyncRecordQuery : PageInput
|
||||
public class BatchAddSyncFileCommand
|
||||
{
|
||||
public List<Guid> FileUploadRecordIdList { get; set; }
|
||||
|
||||
public int? Priority { get; set; }
|
||||
}
|
||||
|
||||
@@ -150,12 +150,25 @@ public class FileUploadRecordService(IRepository<FileUploadRecord> _fileUploadRe
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 批量设置为需要同步,并且设置优先级
|
||||
/// </summary>
|
||||
/// <param name="inComand"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IResponseOutput> BatchAddSyncFileTask(BatchAddSyncFileCommand inComand)
|
||||
{
|
||||
|
||||
|
||||
await _fileUploadRecordRepository.BatchUpdateNoTrackingAsync(t => inComand.FileUploadRecordIdList.Contains(t.Id), u => new FileUploadRecord() { IsNeedSync = true, Priority = inComand.Priority });
|
||||
|
||||
foreach (var item in inComand.FileUploadRecordIdList)
|
||||
{
|
||||
_fileSyncQueue.Enqueue(item, inComand.Priority ?? 0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
await _fileUploadRecordRepository.SaveChangesAsync();
|
||||
|
||||
return ResponseOutput.Ok();
|
||||
}
|
||||
@@ -253,59 +266,206 @@ public class FileUploadRecordService(IRepository<FileUploadRecord> _fileUploadRe
|
||||
#region 同步队列
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 同步队列 信号量
|
||||
/// </summary>
|
||||
public class FileSyncQueue
|
||||
public sealed class FileSyncQueue
|
||||
{
|
||||
/// <summary>
|
||||
/// 优先级队列(仅负责排序)
|
||||
/// </summary>
|
||||
private readonly PriorityQueue<Guid, int> _queue = new();
|
||||
|
||||
/// <summary>
|
||||
/// 当前等待中的任务(唯一真实数据)
|
||||
/// key = Guid
|
||||
/// value = 最新 priority
|
||||
/// </summary>
|
||||
private readonly Dictionary<Guid, int> _waiting = new();
|
||||
|
||||
/// <summary>
|
||||
/// 正在执行的任务(防止重复执行)
|
||||
/// </summary>
|
||||
private readonly HashSet<Guid> _running = new();
|
||||
|
||||
/// <summary>
|
||||
/// worker 等待信号
|
||||
/// </summary>
|
||||
private readonly SemaphoreSlim _signal = new(0);
|
||||
|
||||
|
||||
private readonly object _lock = new();
|
||||
|
||||
// ============================================================
|
||||
// Enqueue
|
||||
// ============================================================
|
||||
|
||||
/// <summary>
|
||||
/// 入队(同 Guid 会覆盖优先级)
|
||||
/// </summary>
|
||||
public void Enqueue(Guid id, int priority)
|
||||
{
|
||||
bool needSignal = false;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
// priority 越大越优先
|
||||
// 如果正在执行,忽略(防止重复)
|
||||
if (_running.Contains(id))
|
||||
return;
|
||||
|
||||
// 是否新任务(用于减少 signal 风暴)
|
||||
if (!_waiting.ContainsKey(id))
|
||||
needSignal = true;
|
||||
|
||||
// 更新为最新优先级(最后一次为准)
|
||||
_waiting[id] = priority; //等价于添加或者更新
|
||||
|
||||
// PriorityQueue 无法更新节点
|
||||
// 允许旧节点存在,Dequeue 时过滤
|
||||
_queue.Enqueue(id, -priority);
|
||||
}
|
||||
|
||||
//类似于计数器,不会产生通知风暴,可消费资源 +1
|
||||
//if (有等待线程) 唤醒一个 else 仅增加计数
|
||||
_signal.Release(); // 唤醒一个 worker
|
||||
// 只有新增任务才唤醒 worker
|
||||
if (needSignal)
|
||||
_signal.Release();
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Dequeue
|
||||
// ============================================================
|
||||
|
||||
/// <summary>
|
||||
/// 如果没有任务 → 挂起等待 有任务 → 被唤醒并返回
|
||||
/// 获取一个待执行任务(无任务时自动等待)
|
||||
/// </summary>
|
||||
/// <param name="ct"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Guid> DequeueAsync(CancellationToken ct)
|
||||
{
|
||||
await _signal.WaitAsync(ct);
|
||||
|
||||
lock (_lock)
|
||||
while (true)
|
||||
{
|
||||
return _queue.Dequeue();
|
||||
await _signal.WaitAsync(ct);
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
while (_queue.Count > 0)
|
||||
{
|
||||
var id = _queue.Dequeue();
|
||||
|
||||
// 已被覆盖或取消 如果这个任务已经不是“当前最新版任务”,那它只是 PriorityQueue 里的垃圾数据,直接跳过。
|
||||
if (!_waiting.TryGetValue(id, out _)) //能从等待任务中取到,那么就是有效的,不能取到那么就是覆盖的
|
||||
continue;
|
||||
|
||||
// 标记为运行中
|
||||
_waiting.Remove(id);
|
||||
_running.Add(id);
|
||||
|
||||
return id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Complete
|
||||
// ============================================================
|
||||
|
||||
/// <summary>
|
||||
/// 获取队列任务Id
|
||||
/// 任务执行完成(必须调用)
|
||||
/// </summary>
|
||||
public void Complete(Guid id)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_running.Remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Snapshot
|
||||
// ============================================================
|
||||
|
||||
/// <summary>
|
||||
/// 当前等待中的任务快照
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Guid[] Snapshot()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return _queue.UnorderedItems
|
||||
.Select(x => x.Element)
|
||||
.ToArray();
|
||||
return _waiting.Keys.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 状态信息(调试用)
|
||||
// ============================================================
|
||||
|
||||
public int WaitingCount
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_lock)
|
||||
return _waiting.Count;
|
||||
}
|
||||
}
|
||||
|
||||
public int RunningCount
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_lock)
|
||||
return _running.Count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///// <summary>
|
||||
///// 同步队列 信号量
|
||||
///// </summary>
|
||||
//public class FileSyncQueue
|
||||
//{
|
||||
// private readonly PriorityQueue<Guid, int> _queue = new();
|
||||
// private readonly SemaphoreSlim _signal = new(0);
|
||||
// private readonly object _lock = new();
|
||||
|
||||
// public void Enqueue(Guid id, int priority)
|
||||
// {
|
||||
// lock (_lock)
|
||||
// {
|
||||
// // priority 越大越优先
|
||||
// _queue.Enqueue(id, -priority);
|
||||
// }
|
||||
|
||||
// //类似于计数器,不会产生通知风暴,可消费资源 +1
|
||||
// //if (有等待线程) 唤醒一个 else 仅增加计数
|
||||
// _signal.Release(); // 唤醒一个 worker
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 如果没有任务 → 挂起等待 有任务 → 被唤醒并返回
|
||||
// /// </summary>
|
||||
// /// <param name="ct"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<Guid> DequeueAsync(CancellationToken ct)
|
||||
// {
|
||||
// await _signal.WaitAsync(ct);
|
||||
|
||||
// lock (_lock)
|
||||
// {
|
||||
// return _queue.Dequeue();
|
||||
// }
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取队列任务Id
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// public Guid[] Snapshot()
|
||||
// {
|
||||
// lock (_lock)
|
||||
// {
|
||||
// return _queue.UnorderedItems
|
||||
// .Select(x => x.Element)
|
||||
// .ToArray();
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
#region 这里不用 SyncQueueUseChannel 和调度器 SyncScheduler
|
||||
public class SyncQueueUseChannel
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user