parent
a092d6cbe8
commit
238a7172b4
|
|
@ -1,12 +1,6 @@
|
|||
|
||||
using Hopetry.Provider;
|
||||
using Newtonsoft.Json;
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Hopetry.Provider;
|
||||
|
||||
namespace Hopetry.Services
|
||||
{
|
||||
|
|
@ -26,7 +20,7 @@ namespace Hopetry.Services
|
|||
}
|
||||
|
||||
// 存储客户端信息(自动过期30天)
|
||||
public async Task StoreClientInfoAsync(Hopetry.Provider.SystemInfo info)
|
||||
public async Task StoreClientInfoAsync(SystemInfo info)
|
||||
{
|
||||
var key = $"{Prefix}{info.MachineId}";
|
||||
var json = JsonConvert.SerializeObject(info);
|
||||
|
|
|
|||
|
|
@ -210,7 +210,7 @@ public class DownViewModel : MvcViewModelBase
|
|||
// 启动任务处理线程
|
||||
_maxConcurrent = DownloadSetting.Instance.MaxConcurrent;
|
||||
_semaphore = new SemaphoreSlim(DownloadSetting.Instance.MaxConcurrent);
|
||||
|
||||
Console.WriteLine($"初始化信号量,信号源数量{_semaphore.CurrentCount}");
|
||||
DownloadSetting.Instance.PropertyChanged += (sender, args) =>
|
||||
{
|
||||
Console.WriteLine($"当前最大下载任务并发数:{MaxConcurrent}");
|
||||
|
|
@ -355,27 +355,6 @@ public class DownViewModel : MvcViewModelBase
|
|||
FinishedTaskHeader = $"已完成({FinishedTasks.Count})";
|
||||
}
|
||||
|
||||
private async void ProcessTasksLoop()
|
||||
{
|
||||
while (!_processingCts.IsCancellationRequested)
|
||||
{
|
||||
await _semaphore.WaitAsync();
|
||||
if (_taskQueue.TryDequeue(out var task))
|
||||
{
|
||||
Console.WriteLine("申请下载许可成功!马上开启下载");
|
||||
// 启动新线程执行下载任务
|
||||
// _ = Task.Run(() => ExecuteTaskAsync(task));
|
||||
_ = ExecuteTaskAsync(task).ContinueWith(_ => _semaphore.Release());
|
||||
}
|
||||
else
|
||||
{
|
||||
_semaphore.Release();
|
||||
}
|
||||
// 无任务时,进入低功耗轮询
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddTask(string bucketName, string objectKey, string size, string downDir)
|
||||
{
|
||||
var task = new MinioDownloadTask(_minioService, bucketName, objectKey, downDir, size);
|
||||
|
|
@ -390,6 +369,32 @@ public class DownViewModel : MvcViewModelBase
|
|||
_taskQueue.Enqueue(task);
|
||||
}
|
||||
|
||||
private async void ProcessTasksLoop()
|
||||
{
|
||||
while (!_processingCts.IsCancellationRequested)
|
||||
{
|
||||
//Console.WriteLine($"申请前信号源数量:{_semaphore.CurrentCount}");
|
||||
await _semaphore.WaitAsync(); // 应该是在这里堵塞了
|
||||
//Console.WriteLine($"当前信号源数量:{_semaphore.CurrentCount}");
|
||||
// 不应该卡到这里吗???
|
||||
if (_taskQueue.TryDequeue(out var task))
|
||||
{
|
||||
Console.WriteLine("申请下载许可成功!马上开启下载");
|
||||
// 启动新线程执行下载任务
|
||||
// _ = Task.Run(() => ExecuteTaskAsync(task));
|
||||
_ = ExecuteTaskAsync(task).ContinueWith(_ => _semaphore.Release());
|
||||
}
|
||||
else
|
||||
{
|
||||
// 队列无内容,马上释放信号源
|
||||
_semaphore.Release();
|
||||
}
|
||||
|
||||
// 无任务时,进入低功耗轮询
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private async Task ExecuteTaskAsync(MinioDownloadTask task)
|
||||
{
|
||||
|
|
@ -450,17 +455,31 @@ public class DownViewModel : MvcViewModelBase
|
|||
var runTask = _runningTasks.Where(task => task is { Status: "下载中", StopDownTs: not null }).ToList();
|
||||
Console.WriteLine($"正在下载任务数:{runTask.Count}");
|
||||
var num = -delta;
|
||||
var temp = new List<MinioDownloadTask>();
|
||||
while (_taskQueue.TryDequeue(out var task))
|
||||
{
|
||||
temp.Add(task);
|
||||
}
|
||||
|
||||
_taskQueue.Clear(); // 清空任务队列
|
||||
foreach (var item in runTask)
|
||||
{
|
||||
_semaphore.WaitAsync();
|
||||
item.StopDownTs.Cancel();
|
||||
//Console.WriteLine($"当前可用许可:{_semaphore.CurrentCount}");
|
||||
item.StopDownTs.Cancel(); // 暂停任务 释放一个许可
|
||||
item.Status = "等待中";
|
||||
item.Speed = "等待中";
|
||||
// 重新加入下载队列
|
||||
temp.Add(item);
|
||||
}
|
||||
|
||||
while (num-- > 0)
|
||||
{
|
||||
_semaphore.Wait();
|
||||
}
|
||||
|
||||
foreach (var item in temp)
|
||||
{
|
||||
_taskQueue.Enqueue(item);
|
||||
if (num-- == 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue