fix: 修复进度条的问题
This commit is contained in:
@@ -1,17 +1,20 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using TypedSignalR.Client;
|
||||
using Tapper;
|
||||
using server.Services;
|
||||
|
||||
#pragma warning disable 1998
|
||||
|
||||
namespace server.Hubs;
|
||||
|
||||
[Hub]
|
||||
public interface IProgressHub
|
||||
{
|
||||
Task<bool> Join(string taskId);
|
||||
Task<bool> Leave(string taskId);
|
||||
Task<ProgressInfo?> GetProgress(string taskId);
|
||||
}
|
||||
|
||||
[Receiver]
|
||||
@@ -23,8 +26,7 @@ public interface IProgressReceiver
|
||||
[TranspilationSource]
|
||||
public enum ProgressStatus
|
||||
{
|
||||
Pending,
|
||||
InProgress,
|
||||
Running,
|
||||
Completed,
|
||||
Canceled,
|
||||
Failed
|
||||
@@ -33,10 +35,10 @@ public enum ProgressStatus
|
||||
[TranspilationSource]
|
||||
public class ProgressInfo
|
||||
{
|
||||
public virtual string TaskId { get; } = string.Empty;
|
||||
public virtual ProgressStatus Status { get; }
|
||||
public virtual int ProgressPercent { get; } = 0;
|
||||
public virtual string ErrorMessage { get; } = string.Empty;
|
||||
public required string TaskId { get; set; }
|
||||
public required ProgressStatus Status { get; set; }
|
||||
public required int ProgressPercent { get; set; }
|
||||
public required string ErrorMessage { get; set; }
|
||||
};
|
||||
|
||||
[Authorize]
|
||||
@@ -44,18 +46,32 @@ public class ProgressInfo
|
||||
public class ProgressHub : Hub<IProgressReceiver>, IProgressHub
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
private readonly IHubContext<ProgressHub, IProgressReceiver> _hubContext;
|
||||
private readonly ProgressTrackerService _tracker;
|
||||
|
||||
public ProgressHub(IHubContext<ProgressHub, IProgressReceiver> hubContext, ProgressTrackerService tracker)
|
||||
{
|
||||
_hubContext = hubContext;
|
||||
_tracker = tracker;
|
||||
}
|
||||
private readonly ProgressTracker _progressTracker = MsgBus.ProgressTracker;
|
||||
|
||||
public async Task<bool> Join(string taskId)
|
||||
{
|
||||
return await Task.Run(() => _tracker.BindTask(taskId, Context.ConnectionId));
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, taskId);
|
||||
|
||||
// 发送当前状态(如果存在)
|
||||
var task = _progressTracker.GetTask(taskId);
|
||||
if (task != null)
|
||||
{
|
||||
await Clients.Caller.OnReceiveProgress(task.Value.ToProgressInfo());
|
||||
}
|
||||
|
||||
logger.Info($"Client {Context.ConnectionId} joined task {taskId}");
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> Leave(string taskId)
|
||||
{
|
||||
await Groups.RemoveFromGroupAsync(Context.ConnectionId, taskId);
|
||||
logger.Info($"Client {Context.ConnectionId} left task {taskId}");
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<ProgressInfo?> GetProgress(string taskId)
|
||||
{
|
||||
return _progressTracker.GetTask(taskId)?.ToProgressInfo();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user