refactor: 将async void方法改为async Task并优化异步操作

This commit is contained in:
写意 2025-07-21 21:44:55 +08:00
parent a73b136f3c
commit aad540a7bc
4 changed files with 18 additions and 15 deletions

View File

@ -37,7 +37,7 @@ public class TestService : IDynamicApiController
/// Redis事件测试 - Payload 🔖
/// </summary>
/// <returns></returns>
public async void EventTestAsync()
public async Task EventTestAsync()
{
await _eventPublisher.PublishAsync(CommonConst.SendErrorMail, "Admin.NET");
}

View File

@ -98,6 +98,7 @@ public sealed class RedisEventSourceStorer : IEventSourceStorer, IDisposable
Console.WriteLine($"有消息要处理{ces.EventId},{ces.Payload}");
Console.ForegroundColor = oriColor;
}
await _channel.Writer.WriteAsync(ces, cancel);
}
@ -151,9 +152,9 @@ public sealed class RedisEventSourceStorer : IEventSourceStorer, IDisposable
/// <summary>
/// 释放非托管资源
/// </summary>
public async void Dispose()
public void Dispose()
{
await _eventConsumer.Stop();
_eventConsumer.Stop().GetAwaiter().GetResult();
GC.SuppressFinalize(this);
}
}

View File

@ -344,7 +344,7 @@ public class SysAuthService : IDynamicApiController, ITransient
/// 退出系统 🔖
/// </summary>
[DisplayName("退出系统")]
public async void Logout()
public async Task Logout()
{
var httpContext = _httpContextAccessor.HttpContext ?? throw Oops.Oh(ErrorCodeEnum.D1016);

View File

@ -23,19 +23,19 @@ public class JobClusterServer : IJobClusterServer
/// 当前作业调度器启动通知
/// </summary>
/// <param name="context">作业集群服务上下文</param>
public async void Start(JobClusterContext context)
public void Start(JobClusterContext context)
{
using var scope = _serviceScopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
// 在作业集群表中,如果 clusterId 不存在,则新增一条(否则更新一条),并设置 status 为 ClusterStatus.Waiting
if (await db.Queryable<SysJobCluster>().AnyAsync(u => u.ClusterId == context.ClusterId))
if (db.Queryable<SysJobCluster>().Any(u => u.ClusterId == context.ClusterId))
{
await db.Updateable<SysJobCluster>().SetColumns(u => u.Status == ClusterStatus.Waiting).Where(u => u.ClusterId == context.ClusterId).ExecuteCommandAsync();
db.Updateable<SysJobCluster>().SetColumns(u => u.Status == ClusterStatus.Waiting).Where(u => u.ClusterId == context.ClusterId).ExecuteCommand();
}
else
{
await db.Insertable(new SysJobCluster { ClusterId = context.ClusterId, Status = ClusterStatus.Waiting }).ExecuteCommandAsync();
db.Insertable(new SysJobCluster { ClusterId = context.ClusterId, Status = ClusterStatus.Waiting }).ExecuteCommand();
}
}
@ -72,7 +72,9 @@ public class JobClusterServer : IJobClusterServer
return;
}
}
catch { }
catch
{
}
}
}
@ -80,24 +82,24 @@ public class JobClusterServer : IJobClusterServer
/// 当前作业调度器停止通知
/// </summary>
/// <param name="context">作业集群服务上下文</param>
public async void Stop(JobClusterContext context)
public void Stop(JobClusterContext context)
{
// 在作业集群表中,更新 clusterId 的 status 为 ClusterStatus.Crashed
using var scope = _serviceScopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
await db.Updateable(new SysJobCluster { Status = ClusterStatus.Crashed }).Where(u => u.ClusterId == context.ClusterId).ExecuteCommandAsync();
db.Updateable(new SysJobCluster { Status = ClusterStatus.Crashed }).Where(u => u.ClusterId == context.ClusterId).ExecuteCommand();
}
/// <summary>
/// 当前作业调度器宕机
/// </summary>
/// <param name="context">作业集群服务上下文</param>
public async void Crash(JobClusterContext context)
public void Crash(JobClusterContext context)
{
// 在作业集群表中,更新 clusterId 的 status 为 ClusterStatus.Crashed
using var scope = _serviceScopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
await db.Updateable(new SysJobCluster { Status = ClusterStatus.Crashed }).Where(u => u.ClusterId == context.ClusterId).ExecuteCommandAsync();
db.Updateable(new SysJobCluster { Status = ClusterStatus.Crashed }).Where(u => u.ClusterId == context.ClusterId).ExecuteCommand();
}
/// <summary>